I am not understanding malloc very well, are those created with malloc just created on the heap?
-
3Yes they are created on heap memory. – ani627 Oct 27 '14 at 04:32
5 Answers
Yes when you malloc you are allocating in the heap.
The malloc function does a request for a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory that you asked for.
You should use malloc when you have data that must have a lifetime different than the code scope where it was allocated. For example: you malloc in a function, you store/keep a pointer to that allocated space and then you can use it in another function.
Another advantage, in contrast to stack allocation, is that you can check if malloc failed, when let's say you don't have enough free memory.
Side note: don't forget to free
what you malloc
.

- 7,696
- 7
- 30
- 41
-
2"Yes when you malloc you are allocating in the heap." - at least when the dynamic memory allocation system is implemented using a heap. It may not be (e. g. on AVR microcontrollers, it isn't). It's pointless to use inexact, implementation-detail-related terminology when using the correct wording ("dynamic allocation") wouldn't be any harder. – The Paramagnetic Croissant Oct 27 '14 at 05:35
One more difference (not yet mentioned in other answers) is that arrays created by malloc()
are anonymous — you have a pointer to the data, but no name for the data.

- 730,956
- 141
- 904
- 1,278
The arrays in stack and heap are no different regarding usage. Both are memory--they are just different in terms of allocation. malloc
internally calls brk()
or sbrk()
to alter the "program break", i.e the program address space as per your malloced size requirement.
You use malloc when you don't exactly know how much memory to allocate. Or if you need to reuse the memory, because you cannot constantly allocate 1000 arrays of size 10 when using only one or two at a time. Then go for malloc, because you can free memory once you have finished.
Note: Don't ever try to free()
statically allocated variables!

- 32,904
- 11
- 98
- 167

- 50
- 6
-
3Welcome to StackOverflow. But as per [Rhythm 'n' Grammar](http://www.hrwiki.org/wiki/local_news#Easter_Eggs): *"And we don't care how they spell things on the Internet. When you answer on SO, you [spell the whole word out](http://stackoverflow.com/revisions/26581453/1). And we don't care that your cell phone has a camera in it."*. Scalawag. :-) – HostileFork says dont trust SE Oct 27 '14 at 06:43
-
2
Allocates requested size of bytes and returns a pointer first byte of allocated space
ptr=(cast-type*)malloc(byte-size)
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.
Example
ptr=(int*)malloc(100*sizeof(int));
It is used when you want to allocated the memory dynamically(during Run-time). usually every program is associated with heap memory when you run. Hence you should return it back when done using memory by using free().

- 1,365
- 11
- 22
-
2[Casting the return value of standard allocators is harmful.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – The Paramagnetic Croissant Oct 27 '14 at 05:34
Your question is "How are arrays that are generated via malloc different from those that are not?"
Answer: there is no difference as far as the C language is concerned. C takes an array address and does indexing relative to it, so it doesn't matter if the array was allocated on the heap or was statically declared.
int a[10];
int *aa;
int *b;
b= a;
b[3]= 4;
aa= calloc(10, sizeof(int));
b= aa;
b[3]= 4;
Note: malloc returns non zeroed memory; calloc returns zeroed memory AND ensuers the returned block is suitably aligned.

- 25,048
- 4
- 23
- 41