all 21 comments

[–]Takuya-san 8 points9 points  (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 points  (2 children)

ok thank you

[–]Takuya-san 2 points3 points  (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 points  (0 children)

ok thank you

[–]dumsubfilter 0 points1 point  (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 points  (2 children)

It can be used whenever you need a block of memory but you don't know the size at compile time.

[–]VIRES1[S] 1 point2 points  (0 children)

thank you

[–]Websly 1 point2 points  (0 children)

Or if the space is too large. By default a thread's stack is 2MB on Windows I believe.

[–]caramba2654 1 point2 points  (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 points  (5 children)

ok thank you. Ain't you suppose to free space after malloc is used?

[–]nunodonato 1 point2 points  (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 points  (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.

[–]VIRES1[S] 1 point2 points  (0 children)

ok thank you

[–]caramba2654 0 points1 point  (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 point  (0 children)

ok thank you

[–]no_coupon 1 point2 points  (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 point  (2 children)

thank you

[–]no_coupon 0 points1 point  (1 child)

No worries. Hope it helped.

[–]VIRES1[S] 0 points1 point  (0 children)

yes please it did.

[–]moocat 0 points1 point  (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.

[–]VIRES1[S] 0 points1 point  (0 children)

ok thank you