Questions tagged [malloc]

The malloc function performs dynamic memory allocation in C and is part of the standard library. Use this tag for questions about usage, behavior and implementations of malloc.

The tag is used for questions related to the use of the malloc() function, part of the C standard library.

malloc(), along with free(), calloc(), and realloc(), is used to explicitly manage memory in C.

malloc() returns the address of a newly allocated block of memory, or NULL if memory allocation fails. The memory returned by malloc is uninitialized, and should be assumed to be filled with random data.

Wikipedia

References

Related Tags

9145 questions
2766
votes
29 answers

Do I cast the result of malloc?

In this question, someone suggested in a comment that I should not cast the result of malloc. i.e., I should do this: int *sieve = malloc(sizeof(*sieve) * length); rather than: int *sieve = (int *) malloc(sizeof(*sieve) * length); Why would this…
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
931
votes
14 answers

Difference between malloc and calloc?

What is the difference between doing: ptr = malloc(MAXELEMS * sizeof(char *)); And: ptr = calloc(MAXELEMS, sizeof(char*)); When is it a good idea to use calloc over malloc or vice versa?
user105033
  • 18,800
  • 19
  • 58
  • 69
651
votes
20 answers

What REALLY happens when you don't free after malloc before program termination?

We are all taught that you MUST free every pointer that is allocated. I'm a bit curious, though, about the real cost of not freeing memory. In some obvious cases, like when malloc() is called inside a loop or part of a thread execution, it's very…
Scott
  • 10,407
  • 13
  • 37
  • 35
589
votes
20 answers

In what cases do I use malloc and/or new?

I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the new operator you should pair with delete and it is a mistake to mix the two (e.g. Calling free() on…
Ralph Burgess
492
votes
24 answers

Why is the use of alloca() not considered good practice?

alloca() allocates memory on the stack rather than on the heap, as in the case of malloc(). So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up dynamically allocated memory. Freeing of memory…
Vaibhav
  • 5,749
  • 3
  • 22
  • 18
315
votes
13 answers

How do malloc() and free() work?

I want to know how malloc and free work. int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); memset(p,0,4); strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes** cout << p; free(p); //…
Priyanka Mishra
  • 10,400
  • 18
  • 62
  • 75
295
votes
3 answers

Why malloc+memset is slower than calloc?

It's known that calloc is different than malloc in that it initializes the memory allocated. With calloc, the memory is set to zero. With malloc, the memory is not cleared. So in everyday work, I regard calloc as malloc+memset. Incidentally, for…
kingkai
  • 3,669
  • 3
  • 19
  • 13
248
votes
8 answers

What is a Memory Heap?

What is a memory heap ?
H4cKL0rD
  • 5,421
  • 15
  • 53
  • 74
192
votes
23 answers

Setting variable to NULL after free

In my company there is a coding rule that says, after freeing any memory, reset the variable to NULL. For example ... void some_func () { int *nPtr; nPtr = malloc (100); free (nPtr); nPtr = NULL; return; } I feel that, in…
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
179
votes
5 answers

Incompatible implicit declaration of built-in function ‘malloc’

I'm getting this error: warning: incompatible implicit declaration of built-in function ‘malloc’ I am trying to do this: fileinfo_list* tempList = malloc(sizeof(fileinfo_list)); Just for the reference the struct used at hand is: typedef struct { …
SGE
  • 2,317
  • 3
  • 19
  • 16
169
votes
1 answer

Why does "The C Programming Language" book say I must cast malloc?

Today I reached page 167 of The C Programming Language (second edition Brian W. Kernighan & Dennis M. Ritchie) and found that the author says I must cast malloc. Here is the part from the book: 7.8.5 Storage Management The functions malloc and…
Michi
  • 5,175
  • 7
  • 33
  • 58
156
votes
18 answers

What's the point of malloc(0)?

I just saw this code: artist = (char *) malloc(0); ...and I was wondering why would one do this?
jldupont
  • 93,734
  • 56
  • 203
  • 318
132
votes
3 answers

How is malloc() implemented internally?

Can anyone explain how malloc() works internally? I have sometimes done strace program and I see a lot of sbrk system calls, doing man sbrk talks about it being used in malloc() but not much more.
bodacydo
  • 75,521
  • 93
  • 229
  • 319
120
votes
3 answers

Freaky way of allocating two-dimensional array?

In a project, somebody pushed this line: double (*e)[n+1] = malloc((n+1) * sizeof(*e)); Which supposedly creates a two-dimensional array of (n+1)*(n+1) doubles. Supposedly, I say, because so far, nobody I asked could tell me what this does,…
User1291
  • 7,664
  • 8
  • 51
  • 108
115
votes
5 answers

Why do I get a warning every time I use malloc?

If I use malloc in my code: int *x = malloc(sizeof(int)); I get this warning from gcc: new.c:7: warning: implicit declaration of function ‘malloc’ new.c:7: warning: incompatible implicit declaration of built-in function ‘malloc’
Kredns
  • 36,461
  • 52
  • 152
  • 203
1
2 3
99 100