Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 998
c_anirudh Avatar asked Aug 03 '20 12:08

c_anirudh


People also ask

Does malloc dynamically allocate memory?

C malloc() method The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.

What is DMA and malloc?

DMA functions malloc(), calloc(), free() The process of allocating memory at runtime is known as dynamic memory allocation. Library routines known as memory management functions are used for allocating and freeing memory during execution of a program.

Which is considered as dynamic memory allocation?

Dynamic memory allocation is the process of assigning the memory space during the execution time or the run time. Reasons and Advantage of allocating memory dynamically: When we do not know how much amount of memory would be needed for the program beforehand.

Which is not dynamic memory allocation function?

Correct option - (C) alloc. Explanation:- Three dynamic memory allocation functions are: malloc, calloc and free.


4 Answers

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?

like image 95
klutt Avatar answered Oct 19 '22 03:10

klutt


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" (i.e. 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, i.e. misunderstood what was meant by "dynamic". Perhaps your teacher talked about the size of the allocated object, i.e.:

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, i.e. (int*), is not needed in C.

like image 21
Support Ukraine Avatar answered Oct 19 '22 03:10

Support Ukraine


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 C1. 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.
like image 4
John Bode Avatar answered Oct 19 '22 02:10

John Bode


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 i.e.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.

like image 3
anastaciu Avatar answered Oct 19 '22 03:10

anastaciu