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
57
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
  • 48,526
  • 41
  • 139
  • 208
57
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,628
  • 3
  • 36
  • 67
57
votes
7 answers

malloc vs mmap in C

I built two programs, one using malloc and other one using mmap. The execution time using mmap is much less than using malloc. I know for example that when you're using mmap you avoid read/writes calls to the system. And the memory access are…
Peter
  • 985
  • 3
  • 12
  • 14
57
votes
3 answers

How does malloc work in a multithreaded environment?

Does the typical malloc (for x86-64 platform and Linux OS) naively lock a mutex at the beginning and release it when done, or does it lock a mutex in a more clever way at a finer level, so that lock contention is reduced for concurrent calls? If it…
pythonic
  • 20,589
  • 43
  • 136
  • 219
56
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,975
  • 11
  • 57
  • 91
54
votes
5 answers

Use of cudamalloc(). Why the double pointer?

I am currently going through the tutorial examples on http://code.google.com/p/stanford-cs193g-sp2010/ to learn CUDA. The code which demostrates __global__ functions is given below. It simply creates two arrays, one on the CPU and one on the GPU,…
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
54
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
  • 11,136
  • 23
  • 74
  • 97
54
votes
5 answers

malloc: *** error: incorrect checksum for freed object - object was probably modified after being freed

I have a big problem with my iOS App: it crashes sometimes without detailed debug error. The stack trace is empty. These are the only two lines in the stack trace: crash start in UIApplicationMain at "symbol stub for: -[_UIHostedTextServiceSession…
user2776543
54
votes
3 answers

Why is it safer to use sizeof(*pointer) in malloc

Given struct node { int a; struct node * next; }; To malloc a new structure, struct node *p = malloc(sizeof(*p)); is safer than struct node *p = malloc(sizeof(struct node)); Why? I thought they are the same.
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
52
votes
12 answers

Memory Allocation/Deallocation Bottleneck?

How much of a bottleneck is memory allocation/deallocation in typical real-world programs? Answers from any type of program where performance typically matters are welcome. Are decent implementations of malloc/free/garbage collection fast enough…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
52
votes
8 answers

sprintf() with automatic memory allocation?

I'm searching for a sprintf()-like implementation of a function that automatically allocates required memory. So I want to say char *my_str = dynamic_sprintf("Hello %s, this is a %.*s nice %05d string", a, b, c, d); and my_str receives the address…
the-shamen
  • 521
  • 1
  • 4
  • 3
52
votes
4 answers

Is there a way to mark a chunk of allocated memory readonly?

if I allocate some memory using malloc() is there a way to mark it readonly. So memcpy() fails if someone attempt to write to it? This is connected to a faulty api design where users are miss-using a const pointer returned by a method GetValue()…
particle
  • 3,280
  • 4
  • 27
  • 39
51
votes
1 answer

Understanding "corrupted size vs. prev_size" glibc error

I have implemented a JNA bridge to FDK-AAC. Source code can be found in here When bench-marking my code, I can get hundreds of successful runs on the same input, and then occasionally a C-level crash that'll kill the entire process, causing a…
Sheinbergon
  • 2,875
  • 1
  • 15
  • 26
51
votes
6 answers

difference between and

When I use malloc in a C program, I get a warning: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default] I can then include or to get rid of the warning although it works without it as…
none
  • 11,793
  • 9
  • 51
  • 87
50
votes
5 answers

Overriding 'malloc' using the LD_PRELOAD mechanism

I'm trying to write a simple shared library that would log malloc calls to stderr (a sort of 'mtrace' if you will). However, this is not working. Here's what I do: /* mtrace.c */ #include #include static void*…
Dany Zatuchna
  • 1,015
  • 1
  • 10
  • 13