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
21
votes
7 answers

C API design: what to do when malloc returns NULL?

Let's say I'm writing a little library in C -- some data structure, say. What should I do if I'm unable to allocate memory? It might be pretty important, e.g. I need some memory to initialize the data structure in the first place, or I'm inserting…
Ismail Badawi
  • 36,054
  • 7
  • 85
  • 97
21
votes
7 answers

How bad it is to keep calling malloc() and free()?

I'm sending a text file - client-server breakup the text into packets each of 512 bytes but some packets contain text less than max size so on the servers side when receiving each packet I'm calling malloc() to build a string again , is this a bad…
cap10ibrahim
  • 587
  • 1
  • 6
  • 16
21
votes
3 answers

g++ error: ‘malloc’ was not declared in this scope

I'm using g++ under Fedora to compile an openGL project, which has the line: textureImage = (GLubyte**)malloc(sizeof(GLubyte*)*RESOURCE_LENGTH); When compiling, g++ error says: error: ‘malloc’ was not declared in this scope Adding #include…
Ovilia
  • 7,066
  • 12
  • 47
  • 70
21
votes
3 answers

Does fprintf use malloc() under the hood?

I want a minimal o-damn-malloc-just-failed handler, which writes some info to a file (probably just standard error). I would prefer to use fprintf() rather than write(), but this will fail badly if fprintf() itself tries to malloc(). Is there some…
Adrian Ratnapala
  • 5,485
  • 2
  • 29
  • 39
21
votes
7 answers

Aligned memory management?

I have a few related questions about managing aligned memory blocks. Cross-platform answers would be ideal. However, as I'm pretty sure a cross-platform solution does not exist, I'm mainly interested in Windows and Linux and to a (much) lesser…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
21
votes
1 answer

Dynamic memory access only works inside function

This question is meant to be used as a canonical duplicate for this FAQ: I am allocating data dynamically inside a function and everything works well, but only inside the function where the allocation takes place. When I attempt to use the same data…
Lundin
  • 195,001
  • 40
  • 254
  • 396
21
votes
4 answers

Calling free on a pointer twice

I have been taught in lectures, that calling free() on a pointer twice is really, really bad. I know that it is good practice, to set a pointer to NULL, right after having freed it. However, I still have never heard any explanation as to why that…
Joe
  • 213
  • 1
  • 2
  • 7
21
votes
2 answers

Memory allocation fails. But why does it crash? Or does it?

I was experimenting with realloc, giving it larger and larger sizes and checking whether the same block was reused or not: int main ( void ) { char * newstr, * prevstr = NULL; size_t newsize, prevsize = 0; printf ("We play with realloc\n"); …
phs
  • 541
  • 3
  • 18
21
votes
2 answers

Include source code of malloc.c in gdb?

How can I include/view the source code of malloc in gdb? I want to do a step by step execution in gdb, and step into malloc.c source code when any of the malloc functions is called. Currently what gdb says is: malloc.c: No such file or…
Paschalis
  • 11,929
  • 9
  • 52
  • 82
21
votes
2 answers

How to run valgrind with basic c example?

Installation: bzip2 -d valgrind-3.10.1.tar.bz2 tar -xf valgrind-3.10.1.tar then: ./configure make make install or simplier sudo apt-get install valgrind How to run valgrind on that simple program example1.c #include int main() { …
andrew
  • 3,083
  • 4
  • 24
  • 29
21
votes
8 answers

Assigning memory to double pointer?

I am having trouble understanding how to assign memory to a double pointer. I want to read an array of strings and store it. char **ptr; fp = fopen("file.txt","r"); ptr = (char**)malloc(sizeof(char*)*50); for(int i=0; i<20; i++) …
lazy_hack
  • 301
  • 1
  • 2
  • 6
21
votes
13 answers

What are alternatives to malloc() in C?

I am writing C for an MPC 555 board and need to figure out how to allocate dynamic memory without using malloc.
patrick
  • 1,022
  • 5
  • 17
  • 29
21
votes
8 answers

What's a good C memory allocator for embedded systems?

I have an single threaded, embedded application that allocates and deallocates lots and lots of small blocks (32-64b). The perfect scenario for a cache based allocator. And although I could TRY to write one it'll likely be a waste of time, and not…
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
21
votes
2 answers

what does "malloc_trim(0)" really mean?

The manual page told me so much and through it I know lots of the background knowledge of memory management of "glibc". But I still get confused. does "malloc_trim(0)"(note zero as the parameter) mean (1.)all the memory in the "heap" section will be…
lookof
  • 363
  • 1
  • 3
  • 8
21
votes
4 answers

Why does malloc not work sometimes?

I'm porting a C project from Linux to Windows. On Linux it is completely stable. On Windows, it's working well most times, but sometimes I got a segmentation fault. I'm using Microsoft Visual Studio 2010 to compile and debug and looks like sometimes…
Pedro Alves
  • 1,667
  • 4
  • 17
  • 37