简体   繁体   中英

Is malloc dynamic memory allocation?

I was told by an instructor that p = (int*)malloc(5 * sizeof(int)) is NOT dynamic memory allocation and that p=(int*)malloc(n * sizeof(int)) is dynamic memory allocation.

The instructor was talking about basic data structures and was teaching arrays. He had told us the traditional way to create arrays using the int arr[100] syntax, but then he introduced us with malloc.

According to him as the memory size doesn't change it's not dynamic I guess.

From what I could gather from the internet, malloc assigns memory at runtime, and when memory is assigned at runtime its dynamic memory allocation. So I think both the malloc statements are dynamic memory allocations. Is there something wrong with my reasoning?

Usually, we do refer to calls to malloc as dynamic allocation, irregardless if you're using a variable or constant. Even the man page for malloc calls it like that:

malloc, free, calloc, realloc - allocate and free dynamic memory

So for your instructors claim:

The instructor was talking about basic data structures and was teaching arrays. He had told us the traditional way to create arrays using the int arr[100] syntax, but then he introduced us with malloc.

According to him as the memory size doesn't change it's not dynamic I guess.

Well, in some sense he has a point if you look strictly at what "dynamic" means in a more general way. Right now we have a convention that calls all malloc dynamic allocation. This convention could have been the way your teacher claims without any problems. But it is not.

Furthermore, according to your teachers reasoning, using VLA:s (variable length array) or alloca with a variable would be considered dynamic allocation, but it is not. A VLA can be declared like this: int arr[n] , or it's alloca equivalent: int *arr = alloca(n*sizeof(*arr)) .

So even if you could argue that your teacher has a point, it would only cause confusion since it goes against the convention.

Also, the most dynamic thing about using malloc is that the allocation can be resized later. You cannot do that with arrays, not even VLA:s. And you cannot do it to memory you have allocated with alloca .

But as a sidenote, I do question your teachers competence if they teach you to write

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

instead of

p = malloc(n * sizeof(*p))
  1. The cast is not necessary and just adds clutter
  2. Using sizeof(*p) instead of sizeof(int) is safer

Related: Do I cast the result of malloc?

The C standard does not define the term "dynamic memory allocation". So we can't take the C standard and lookup what dynamic memory allocation is.

The C standard talks about "Memory management functions" (ie aligned_alloc, calloc, malloc, realloc and free). When these functions are used it's commonly called dynamic memory allocation but - just to repeat - it's not a term from the standard.

The standard talks about "life time of objects". An objected created using one of the above memory management function is said to have "allocated storage duration" (which means that it exists until your code frees it).

Both code lines in the question makes p point to an object that has "allocated storage duration".

My guess is that you have misunderstood your teacher, ie misunderstood what was meant by "dynamic". Perhaps your teacher talked about the size of the allocated object, ie:

p = (int*)malloc(5 * sizeof(int));   // Here the size is static - always 5 ints

p = (int*)malloc(n * sizeof(int));   // Here the size is dynamic (aka depends on n)

Note: The cast, ie (int*) , is not needed in C.

Either you're misunderstanding the point your instructor was trying to make, or your instructor was making his point very, very badly (which, frankly, is not uncommon, especially when it comes to teaching C).

Both of the malloc calls in your question are dynamic memory allocation. The only difference is that the first form allocates a known, fixed amount of memory every time it is executed, whereas the second can allocate a different amount each time it's executed. That doesn't make the first form not dynamic allocation.

The memory in both cases can be resized with a call to realloc .

And as a stylistic note, you don't have to cast the result of malloc in C 1 . It's much less eye-stabby to write

p = malloc( 5 * sizeof *p ); 

or

p = malloc( n * sizeof *p );

sizeof *p is the same as sizeof (int) (assuming p was declared as int *p ). This makes maintenance easier, since you don't have to repeat type information multiple times.


  1. That's not the case in C++, but if you're writing C++ you shouldn't be using malloc anyway.

I was told by an instructor that p = (int*)malloc(5 * sizeof(int)); is NOT dynamic memory allocation and that p = (int*)malloc(n * sizeof(int)); is dynamic memory allocation

This is a somewhat opinion based issue in the sense that there is no obligation to refer to it as one or the other, this is largely based on convention. That said, I don't agree at all with the opinion that there is some correctness to the statement, even assuming the reference may be to the size of the memory block being deppendant on a constant value.

Both expressions should only be qualified as dynamic memory allocations either if you use a constant or a variable value. Stating otherwise can only be qualified as wrong, in my opinion.

Both memory block assignments can still be changed later on, in a runtime environment, they are therefore dynamic, whereas in an array declaration ie int arr[100] the assigned memory is fixed, it cannot be changed, thus it's not dynamic.

There are, however, differences in using a constant or a variable, for the obvious one, being able to assing a value to the variable that will determine the size of the memory block at runtime. And as @cmaster-reinstatemonica very accurately pointed out, using a constant as a size determinant for the memory block allows for compiler optimization of malloc in certain circumstances, which is meaningful given the fact that it can be an expensive function.

Other than that, the statements are largely similar. In both cases you can resize the memory blocks at runtime after the allocation.

Some good considerations regarding the correct usage of malloc are made by @JohnBode , I strongly recommend you follow them, in fact the entire answer is very good and should be called to the attention of your instructor if you feel comfortable with it, you'll be able to clarify the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM