Wednesday 9 March 2011

Using using malloc() to dynamically declare variables

If you're like me and used to using arrays to store primitive data types (or other data types for that matter), then you'll realize that there are some situations in which you do not initially know how big of an array you will need to store the data your user may enter. To get around this, we will dynamically declare data which will allow your program to effectively store an infinite number of variables (or until your computer runs out of memory).
To do this, we will be using a function called malloc()  (memory allocation)
The syntax for malloc is as follows:

malloc((type*)sizeof(type));

This may seem like a lot at first, but I'll break it down a bit starting from the right:
sizeof(type) will return the number of bytes which malloc will allocate to your program. For example, sizeof(char) will return 1 byte. Instead of using sizeof(), you could always just enter a number, but that's really bad practice, so I don't recommend it.
When you run malloc, it goes and asks the operating system "Can I get this much memory for my program?" and in general, it will return unspecified memory. This means that we have to cast it using (type*) because we know what this memory is being used for, but malloc() does not!
So now that I've explained the basics of malloc(), I'll give some examples:


int *p;
p = malloc((int*)sizeof(int));


See, we are starting off with a pointer to an integer and we are setting it equal to the memory allocated by malloc(). Useful isn't it?
This is great, but there are even more applications. Take a look at this code:


int *p;
p = malloc(int*)10*sizeof(int));


Have you caught on? Basically, malloc() went and asked the operating system "Can I get 10*sizeof(int) bytes to use?". We have gotten to enough memory for 10 integers which are all right beside each other in memory. What does this mean? We have dynamically created an array of integers. The multiplier can also be a variable. For example:


int x=42;
int *p;

p = malloc(int*)x*sizeof(int));

There we go! An array of 42 integers. This can also be applied so that the user is asked to enter the size of the array and then an array of that size will be created.

I hope this first lesson has been informative! Keep on coding and be creative!

17 comments:

  1. awesome blog, im on a C++ course in (sheffield) UK

    ReplyDelete
  2. I am almost computer-illiterate, but I'm down to at least try to learn something here. Consider yourself followed

    ReplyDelete
  3. What is the benefit of using C++ as apposed to other options?

    ReplyDelete
  4. Good lesson on code. It's been a little while since I've taken a computer programming course, but this post is ringing some bells.

    ReplyDelete
  5. I have to show this to my friend, hes been having some problems, maybe this could help! Following u for further tips too!

    ReplyDelete
  6. I've been wanting to expand my computer knowledge. Thanks friend!

    ReplyDelete
  7. Man, I haven't looked at cdoing since college. Fun stuff.

    ReplyDelete
  8. I think I'm gonna learn Python; it looks so much more simple (speaking as an english major)

    ReplyDelete
  9. This helps so F-in much, just started C++. Thanks.

    ReplyDelete
  10. Nice quick tutorial, you should mention the use of the free() command though. It's not a big problem when you're just playing around with learning code, or in a IDE with a decent debug mode, but nothing annoys computer science teachers more than memory leaks.

    Also, technically malloc() is a C command, C++ allows you to use the new operator, which is also worth a mention.

    Good start to a blog, I wish you luck, and plan to check back.

    ReplyDelete
  11. Nice first post. I will be back ;)

    ReplyDelete
  12. Enjoy your null pointer dereferences.

    ReplyDelete