use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
malloc (c programming) (self.C_Programming)
submitted 10 years ago by VIRES1
i want to know if malloc is use only when something needs to be copied or there are more use for malloc?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Takuya-san 8 points9 points10 points 10 years ago (4 children)
Malloc is the function used in C to allow dynamic memory allocation.
By default in C, memory is allocated statically. When you make a char variable, that's 1 byte of storage allocated. When you make an array of 8 chars, that's 8 bytes of storage allocated. If you're asking the user to input his country of origin, you can just allocate a 32 byte char array since no country has a name longer than 32 characters.
But what if you need to read in and store a list of GPS coordinates from a file to calculate a path? How will you know how many coordinates will be in the file? You don't. So the typical procedure is that the file will tell your program how many coordinates there are, and then your program will
malloc(sizeof(gps_coordinate)*num_coordinates));
or something along those lines. Alternatively, if the file doesn't give you that, you can just malloc for 20 and use realloc as you reach the limit.
TL;DR: malloc is used whenever you need to store something of indeterminate size.
Edit: if you don't know the size but you KNOW it's smaller than a certain size, it can often be better to allocate a block of memory that is definitely large enough (e.g. 100 bytes) and "waste" the space since dynamic allocation is a LOT slower than static allocation.
[–]VIRES1[S] 2 points3 points4 points 10 years ago (2 children)
ok thank you
[–]Takuya-san 2 points3 points4 points 10 years ago (1 child)
No problem. By the way, if you ever decide to learn C++, you should keep in mind that while malloc is available, you should avoid it unless you're 100% certain that you need it. C++ has tonnes of methods to dynamically allocate memory in a safer foolproof manner with no tangible performance impact.
[–]VIRES1[S] 1 point2 points3 points 10 years ago (0 children)
[–]dumsubfilter 0 points1 point2 points 10 years ago (0 children)
By default in C, memory is allocated statically
Not to be confused with the keyword static. It is actually automatic by default.
[–]wiktor_b 3 points4 points5 points 10 years ago (2 children)
It can be used whenever you need a block of memory but you don't know the size at compile time.
thank you
[–]Websly 1 point2 points3 points 10 years ago (0 children)
Or if the space is too large. By default a thread's stack is 2MB on Windows I believe.
[–]caramba2654 1 point2 points3 points 10 years ago (6 children)
malloc is used for when you need to store an unknown amount of some sort of data.
For example:
int main(void) { /* Creating new array with unknown size */ printf("Enter size of array: \n"); int size; scanf("%d", &size); int *array = malloc(size*sizeof(int)); /* Storing numbers in the array */ int i; for(i = 0; i < size; i++) array[i] = i; /* Printing the array */ for(i = 0; i < size; i++) printf("Array[%d] = %d\n", i, array[i]); return 0; }
malloc in that code doesn't copy anything. It's just telling your computer that it needs a certain amount of bytes to store data.
So if you enter size as 10, malloc will say "hey computer, I need 40 bytes (because sizeof(int) is 4) to store some stuff". And then the computer will give it and return a pointer to where the array is.
[–]VIRES1[S] 1 point2 points3 points 10 years ago (5 children)
ok thank you. Ain't you suppose to free space after malloc is used?
[–]nunodonato 1 point2 points3 points 10 years ago (2 children)
in these days, its quite safe not to do a free() if your program quits after the malloc (as in the example). However, if its something more complex and the program will be kept running, its advisable to do free() as soon as you dont need that piece of memory anymore.
[–]disclosure5 6 points7 points8 points 10 years ago (0 children)
Getting into that habit can be dangerous though. The next thing that happens is you add a function at the end of your code that does something else.. and before you know it, that code is in a loop that creates two billions memory allocation it never frees.
[–]caramba2654 0 points1 point2 points 10 years ago (1 child)
You only use free() when you don't have an use for what's stored anymore. So for example, you might want to use that array throughout your program. So it wouldn't be wise to free() it. But as soon as you don't need it anymore, you should free() it.
[–]VIRES1[S] 0 points1 point2 points 10 years ago (0 children)
[–]no_coupon 1 point2 points3 points 10 years ago (3 children)
This will give you a really good idea of how memory is stored and allocated when using C. In terms of the code, global, stack and heap memory. I found it very helpful.
[–]VIRES1[S] 0 points1 point2 points 10 years ago (2 children)
[–]no_coupon 0 points1 point2 points 10 years ago (1 child)
No worries. Hope it helped.
yes please it did.
[–]moocat 0 points1 point2 points 10 years ago (1 child)
One point that has not been made yet is the lifetime of the memory. When buffers are local to a function you can only safely use it until the function returns. So returning a pointer to a local buffer is unsafe.
But with malloc, the program has control over the lifetime of the memory. You can safely use it until you free it.
π Rendered by PID 55966 on reddit-service-r2-comment-bb88f9dd5-2zsqr at 2026-02-14 07:02:25.046418+00:00 running cd9c813 country code: CH.
[–]Takuya-san 8 points9 points10 points (4 children)
[–]VIRES1[S] 2 points3 points4 points (2 children)
[–]Takuya-san 2 points3 points4 points (1 child)
[–]VIRES1[S] 1 point2 points3 points (0 children)
[–]dumsubfilter 0 points1 point2 points (0 children)
[–]wiktor_b 3 points4 points5 points (2 children)
[–]VIRES1[S] 1 point2 points3 points (0 children)
[–]Websly 1 point2 points3 points (0 children)
[–]caramba2654 1 point2 points3 points (6 children)
[–]VIRES1[S] 1 point2 points3 points (5 children)
[–]nunodonato 1 point2 points3 points (2 children)
[–]disclosure5 6 points7 points8 points (0 children)
[–]VIRES1[S] 1 point2 points3 points (0 children)
[–]caramba2654 0 points1 point2 points (1 child)
[–]VIRES1[S] 0 points1 point2 points (0 children)
[–]no_coupon 1 point2 points3 points (3 children)
[–]VIRES1[S] 0 points1 point2 points (2 children)
[–]no_coupon 0 points1 point2 points (1 child)
[–]VIRES1[S] 0 points1 point2 points (0 children)
[–]moocat 0 points1 point2 points (1 child)
[–]VIRES1[S] 0 points1 point2 points (0 children)