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
112
votes
8 answers

Malloc vs new -- different padding

I'm reviewing someone else's C++ code for our project that uses MPI for high-performance computing (10^5 - 10^6 cores). The code is intended to allow for communications between (potentially) different machines on different architectures. He's…
hcarver
  • 7,126
  • 4
  • 41
  • 67
111
votes
8 answers

Why do I get a C malloc assertion failure?

I am implementing a divide and conquer polynomial algorithm so I can benchmark it against an OpenCL implementation, but I can't get malloc to work. When I run the program, it allocates a bunch of stuff, checks some things, then sends the size/2 to…
Chris
  • 4,425
  • 5
  • 34
  • 49
102
votes
2 answers

Is using malloc for int undefined behavior until C++20

I was told that the following code has undefined behavior until C++20: int *p = (int*)malloc(sizeof(int)); *p = 10; Is that true? The argument was that the lifetime of the int object is not started before assigning the value to it (P0593R6). To fix…
anton_rh
  • 8,226
  • 7
  • 45
  • 73
96
votes
4 answers

How does jemalloc work? What are the benefits?

Firefox 3 came with a new allocator: jemalloc. I have heard at several places that this new allocator is better. The top Google results don't gave any further information though and I am interested in how exactly it works.
Albert
  • 65,406
  • 61
  • 242
  • 386
93
votes
12 answers

Why does `free` in C not take the number of bytes to be freed?

Just to be clear: I do know that malloc and free are implemented in the C library, which usually allocates chunks of memory from the OS and does its own management to parcel out smaller lots of memory to the application and keeps track of the number…
91
votes
6 answers

When and why to use malloc

Well, I can't understand when and why it is needed to allocate memory using malloc. Here is my code: #include int main(int argc, const char *argv[]) { typedef struct { char *name; char *sex; int age; } student; //…
pr1m3x
  • 1,947
  • 6
  • 21
  • 19
87
votes
10 answers

Why does malloc initialize the values to 0 in gcc?

Maybe it is different from platform to platform, but when I compile using gcc and run the code below, I get 0 every time in my ubuntu 11.10. #include #include int main() { double *a = malloc(sizeof(double)*100) …
SHH
  • 3,226
  • 1
  • 28
  • 41
84
votes
13 answers

How to find the cause of a malloc "double free" error?

I'm programming an application in Objective-C and I'm getting this error: MyApp(2121,0xb0185000) malloc: *** error for object 0x1068310: double free *** set a breakpoint in malloc_error_break to debug It is happening when I release an…
gonso
  • 2,065
  • 5
  • 27
  • 35
84
votes
12 answers

Is malloc thread-safe?

Is the malloc() function re-entrant?
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
83
votes
9 answers

C Programming: malloc() inside another function

I need help with malloc() inside another function. I'm passing a pointer and size to the function from my main() and I would like to allocate memory for that pointer dynamically using malloc() from inside that called function, but what I see is…
HaggarTheHorrible
  • 7,083
  • 20
  • 70
  • 81
81
votes
8 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,345
  • 2
  • 17
  • 23
80
votes
2 answers

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
  • 12,187
  • 25
  • 96
  • 148
76
votes
6 answers

Does malloc lazily create the backing pages for an allocation on Linux (and other platforms)?

On Linux if I were to malloc(1024 * 1024 * 1024), what does malloc actually do? I'm sure it assigns a virtual address to the allocation (by walking the free list and creating a new mapping if necessary), but does it actually create 1 GiB worth of…
Aaron Maenpaa
  • 119,832
  • 11
  • 95
  • 108
76
votes
9 answers

Difference between array type and array allocated with malloc

Today I was helping a friend of mine with some C code, and I've found some strange behavior that I couldn't explain him why it was happening. We had TSV file with a list of integers, with an int each line. The first line was the number of lines the…
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121
74
votes
4 answers

invalid conversion from `void*' to `char*' when using malloc?

I'm having trouble with the code below with the error on line 5: error: invalid conversion from void* to char* I'm using g++ with codeblocks and I tried to compile this file as a cpp file. Does it matter? #include int main() { …
pandoragami
  • 5,387
  • 15
  • 68
  • 116