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

alloc, malloc, and alloca — What's the difference?

I was under the impression that alloc in Objective-C (when we invoke [anyObject alloc] is actually implementing C function malloc and the memory getting allocated in heap, but could not find anywhere the answer for this. Also, while searching for…
Anindya Sengupta
  • 2,539
  • 2
  • 21
  • 27
40
votes
5 answers

When to use malloc for char pointers

I'm specifically focused on when to use malloc on char pointers char *ptr; ptr = "something"; ...code... ...code... ptr = "something else"; Would a malloc be in order for something as trivial as this? If yes, why? If not, then when is it necessary…
ZPS
  • 1,566
  • 4
  • 16
  • 20
40
votes
1 answer

malloced array VS. variable-length-array

There are two ways to allocate memory to an array, of which the size is unknown at the beginning. The most common way is using malloc like this int * array; ... // when we know the size array = malloc(size*sizeof(int)); But it's valid too in C99 to…
linusz
  • 743
  • 1
  • 14
  • 26
40
votes
11 answers

How can I allocate memory and return it (via a pointer-parameter) to the calling function?

I have some code in a couple of different functions that looks something like this: void someFunction (int *data) { data = (int *) malloc (sizeof (data)); } void useData (int *data) { printf ("%p", data); } int main () { int *data = NULL; …
a_m0d
  • 12,034
  • 15
  • 57
  • 79
39
votes
5 answers

Can I assume that calling realloc with a smaller size will free the remainder?

Let’s consider this very short snippet of code: #include int main() { char* a = malloc(20000); char* b = realloc(a, 5); free(b); return 0; } After reading the man page for realloc, I was not entirely sure that the…
qdii
  • 12,505
  • 10
  • 59
  • 116
39
votes
4 answers

Explain this implementation of malloc from the K&R book

This is an excerpt from the book on C by Kernighan and Ritchie. It shows how to implement a version of malloc. Although well commented, I am having great difficulty in understanding it. Can somebody please explain it? typedef long Align; /* for…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
39
votes
3 answers

What is the cause of flexible array member not at end of struct error?

I am wondering why I keep getting error: flexible array member not at end of struct error when I call malloc. I have a struct with a variable length array, and I keep getting this error. The struct is, typedef struct { size_t N; double data[]; …
csta
  • 2,423
  • 5
  • 26
  • 34
38
votes
1 answer

What is the name of the header file that contains the declaration of malloc?

What is the name of the header file that contains the declaration of the malloc() function in C and C++?
Peter
  • 663
  • 1
  • 7
  • 15
38
votes
2 answers

Segfaults in malloc() and malloc_consolidate()

My application segfaults sometimes and mainly in malloc() and malloc_consolidate() when I look at the backtrace in gdb. I verified that the machine has enough memory available, it didn't even start swapping. I checked ulimits for data segement and…
Gene Vincent
  • 5,237
  • 9
  • 50
  • 86
38
votes
3 answers

An alternative for the deprecated __malloc_hook functionality of glibc

I am writing a memory profiler for C and for that am intercepting calls to the malloc, realloc and free functions via malloc_hooks. Unfortunately, these are deprecated because of their poor behavior in multi threaded environments. I could not find a…
Andreas Grapentin
  • 5,499
  • 4
  • 39
  • 57
38
votes
8 answers

Multithreaded Memory Allocators for C/C++

I currently have heavily multi-threaded server application, and I'm shopping around for a good multi-threaded memory allocator. So far I'm torn between: Sun's umem Google's tcmalloc Intel's threading building blocks allocator Emery Berger's…
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
38
votes
3 answers

How to use realloc in a function in C

Building on what I learned here: Manipulating dynamic array through functions in C. void test(int data[]) { data[0] = 1; } int main(void) { int *data = malloc(4 * sizeof *data); test(data); return 0; } This works…
Legendre
  • 3,108
  • 7
  • 31
  • 46
37
votes
11 answers

malloc & placement new vs. new

I've been looking into this for the past few days, and so far I haven't really found anything convincing other than dogmatic arguments or appeals to tradition (i.e. "it's the C++ way!"). If I'm creating an array of objects, what is the compelling…
37
votes
7 answers

Allocating memory for a Structure in C

I'm tasked to create a program which dynamically allocates memory for a structure. normally we would use x=malloc(sizeof(int)*y); However, what do I use for a structure variable? I don't think its possible to do struct st x =…
Blackbinary
  • 3,936
  • 18
  • 49
  • 62
36
votes
3 answers

Why, or when, do you need to dynamically allocate memory in C?

Dynamic memory allocation is a very important topic in C programming. However, I've been unable to find a good explanation of what this enables us to do, or why it is required. Can't we just declare variables and structs and never have to use…
user2517777
  • 741
  • 2
  • 7
  • 9