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
66
votes
3 answers

Free memory allocated in a different function?

I'm trying to learn C and I'm currently trying to write a basic stack data structure, but I can't seem to get basic malloc/free right. Here's the code I've been using (I'm just posting a small part here to illustrate a specific problem, not the…
Jeff Tratner
  • 13,792
  • 4
  • 41
  • 63
66
votes
11 answers

What happens to memory after '\0' in a C string?

Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the user of my function a C-string, whose length I do not know at the beginning of the function. I can place only an upper bound on the length at the outset,…
Erika Electra
  • 1,789
  • 3
  • 18
  • 30
65
votes
1 answer

What are the differences between (and reasons to choose) tcmalloc/jemalloc and memory pools?

tcmalloc/jemalloc are improved memory allocators, and memory pool is also introduced for better memory allocation. So what are the differences between them and how to choose them in my application?
Mickey Shine
  • 11,559
  • 22
  • 84
  • 142
65
votes
7 answers

C - freeing structs

Let's say I have this struct typedef struct person{ char firstName[100], surName[51] } PERSON; and I am allocating space by malloc and filling it with some values PERSON *testPerson = (PERSON*)…
user10099
  • 1,128
  • 2
  • 15
  • 21
63
votes
8 answers

malloc() vs. HeapAlloc()

What is the difference between malloc() and HeapAlloc()? As far as I understand malloc allocates memory from the heap, just as HeapAlloc, right? So what is the difference? Thanks!
TCS
  • 5,350
  • 4
  • 39
  • 75
62
votes
8 answers

Will malloc implementations return free-ed memory back to the system?

I have a long-living application with frequent memory allocation-deallocation. Will any malloc implementation return freed memory back to the system? What is, in this respect, the behavior of: ptmalloc 1, 2 (glibc default) or 3 dlmalloc tcmalloc…
osgx
  • 80,853
  • 42
  • 303
  • 470
62
votes
8 answers

How do free and malloc work in C?

I'm trying to figure out what would happened if I try to free a pointer "from the middle" for example, look at the following code: char *ptr = (char*)malloc(10*sizeof(char)); for (char i=0 ; i<10 ; ++i) { ptr[i] =…
user238082
  • 623
  • 1
  • 6
  • 7
61
votes
3 answers

Why does the speed of memcpy() drop dramatically every 4KB?

I tested the speed of memcpy() noticing the speed drops dramatically at i*4KB. The result is as follow: the Y-axis is the speed(MB/second) and the X-axis is the size of buffer for memcpy(), increasing from 1KB to 2MB. Subfigure 2 and Subfigure 3…
foool
  • 1,291
  • 1
  • 12
  • 25
60
votes
6 answers

In C, how would I choose whether to return a struct or a pointer to a struct?

Working on my C muscle lately and looking through the many libraries I've been working with its certainly gave me a good idea of what is good practice. One thing that I have NOT seen is a function that returns a struct: something_t make_something()…
Dellowar
  • 2,764
  • 11
  • 30
59
votes
4 answers

Xcode Guard Malloc and on Device Debugging: 'libgmalloc.dylib' image not found

I enabled memory checking in Xcode (Edit Scheme -> Options). I'm now getting the following when I perform on device debugging: dyld: could not load inserted library '/usr/lib/libgmalloc.dylib' because image not found. The measure was taken…
jww
  • 83,594
  • 69
  • 338
  • 732
55
votes
7 answers

What is the difference between "new" and "malloc" and "calloc" in C++?

What is the difference between "new" and "malloc" and "calloc" and others in family? (When) Do I need anything other than "new" ? Is one of them implemented using any other?
Łukasz Lew
  • 43,526
  • 39
  • 134
  • 202
55
votes
11 answers

C API design: Who should allocate?

What is the proper/preferred way to allocate memory in a C API? I can see, at first, two options: 1) Let the caller do all the (outer) memory handling: myStruct *s =…
Tordek
  • 10,075
  • 3
  • 31
  • 63
53
votes
8 answers

Maximum memory which malloc can allocate

I was trying to figure out how much memory I can malloc to maximum extent on my machine (1 Gb RAM 160 Gb HD Windows platform). I read that the maximum memory malloc can allocate is limited to physical memory (on heap). Also when a program exceeds…
Vikas
  • 1,280
  • 1
  • 10
  • 15
53
votes
9 answers

What does malloc(0) return?

What does malloc(0) return? Would the answer be same for realloc(malloc(0),0)? #include #include int main() { printf("%p\n", malloc(0)); printf("%p\n", realloc(malloc(0), 0)); return 0; } Output from Linux…
manav m-n
  • 10,236
  • 21
  • 66
  • 95
52
votes
7 answers

zero size malloc

Very simple question, I made the following program : #include int main(int argc, char ** argv) { void * ptr; ptr = malloc(0); free(ptr); } And it does not segfault on my machine. Is it a portable behaviour of stdlib malloc…
shodanex
  • 14,098
  • 10
  • 54
  • 86