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

8189 questions
2554
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(int) * length); rather than: int *sieve = (int *) malloc(sizeof(int) * length); Why would this be the…
Patrick McDonald
  • 59,808
  • 14
  • 95
  • 115
827
votes
14 answers

Difference between malloc and calloc?

What is the difference between doing: ptr = (char **) malloc (MAXELEMS * sizeof(char *)); or: ptr = (char **) calloc (MAXELEMS, sizeof(char*)); When is it a good idea to use calloc over malloc or vice versa?
user105033
  • 16,546
  • 15
  • 55
  • 66
576
votes
18 answers

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

This has been something that has bothered me for ages now. We are all taught in school (at least, I was) 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…
Scott
  • 9,201
  • 12
  • 35
  • 35
528
votes
19 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
437
votes
22 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,189
  • 3
  • 19
  • 18
288
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); //…
Namratha Patil
  • 9,772
  • 18
  • 54
  • 70
268
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,399
  • 3
  • 16
  • 13
196
votes
7 answers

What is a Memory Heap?

What is a memory heap ?
H4cKL0rD
  • 4,775
  • 15
  • 47
  • 72
168
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
  • 10,931
  • 20
  • 65
  • 87
162
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
  • 4,759
  • 6
  • 29
  • 51
161
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,087
  • 3
  • 16
  • 16
129
votes
17 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
  • 82,560
  • 49
  • 190
  • 305
118
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
  • 63,809
  • 83
  • 206
  • 303
114
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
  • 34,183
  • 49
  • 147
  • 200
112
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
  • 6,231
  • 5
  • 34
  • 75
1
2 3
99 100