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

Proper Way to Free Memory of a Returned Variable

I created a function designed to get user input. It requires that memory be allocated to the variable holding the user input; however, that variable is returned at the end of the function. What is the proper method to free the allocated…
w m
  • 493
  • 1
  • 5
  • 7
35
votes
4 answers

there is no heap in c?

I just started reading The C Programming Language by Brian Kernighan and Dennis Ritchie, and I found this statement: The language does not define any storage allocation facility other than static definition and the stack discipline provided by the…
user1171901
  • 395
  • 3
  • 5
34
votes
3 answers

malloc implementation?

I'm trying to implement malloc and free for C, and I am not sure how to reuse memory. I currently have a struct that looks like this: typedef struct _mem_dictionary { void *addr; size_t size; int freed; } mem_dictionary; My malloc looks…
user675257
  • 341
  • 1
  • 3
  • 3
34
votes
3 answers

Difference between global non-throwing ::operator new and std::malloc

C++ has several functions to acquire dynamic storage, most of which differ in some fundamental way. Several more are usually added by the OS. Two of these are of special interest due to their portability and similarity: malloc and ::operator…
danielschemmel
  • 10,885
  • 1
  • 36
  • 58
33
votes
5 answers

LinkedList - How to free the memory allocated using malloc

I have a very simple C code for constructing a Singly Linked list as below, in which I allocate memory for each node dynamically using malloc. At the end of code, I want to free the memory for each node allocated, was wondering how to go about it -…
goldenmean
  • 18,376
  • 54
  • 154
  • 211
33
votes
5 answers

In malloc, why use brk at all? Why not just use mmap?

Typical implementations of malloc use brk/sbrk as the primary means of claiming memory from the OS. However, they also use mmap to get chunks for large allocations. Is there a real benefit to using brk instead of mmap, or is it just tradition?…
Nate C-K
  • 5,744
  • 2
  • 29
  • 45
33
votes
2 answers

Is a malloc() needed before a realloc()?

Since I had read realloc will act as malloc if the size pointed is 0, I was using it without malloc(), provided the pointer was static, global, or explicitly set to NULL if automatic. However, I notice a lot of programmers try to set it or set it to…
j riv
  • 3,593
  • 6
  • 39
  • 54
33
votes
5 answers

I'm very confused about malloc() and calloc() on C

I've always programmed in Java, which is probably why I'm so confused about this: In Java I declare a pointer: int[] array and initialize it or assign it some memory: int[] array = {0,1,0} int[] array = new int[3] Now, in C, it's all so confusing.…
bluehallu
  • 10,205
  • 9
  • 44
  • 61
33
votes
4 answers

C - calloc() v. malloc()

Possible Duplicate: c difference between malloc and calloc Please explain the significance of this statement, Another difference between the malloc() and calloc() functions is that the memory allocated by malloc( ) function contains…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
33
votes
3 answers

Does malloc() use brk() or mmap()?

c code: // program break mechanism // TLPI exercise 7-1 #include #include void program_break_test() { printf("%10p\n", sbrk(0)); char *bl = malloc(1024 * 1024); printf("%x\n", sbrk(0)); free(bl); …
Eric
  • 22,183
  • 20
  • 145
  • 196
33
votes
2 answers

Examining C/C++ Heap memory statistics in gdb

I'm trying to investigate the state of the C/C++ heap from within gdb on Linux amd64, is there a nice way to do this? One approach I've tried is to "call mallinfo()" but unfortunately I can't then extract the values I want since gdb doesn't deal…
Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
33
votes
5 answers

Is there a need to check for NULL after allocating memory, when kernel uses overcommit memory

It is general practice to check for NULL (whether memory is successfully allocated) after a malloc(), some thing like void *ptr = malloc(10); if (ptr != NULL) { // do some thing usefull } else { // no memory. safely return/throw ... }…
FL4SOF
  • 4,161
  • 6
  • 28
  • 24
32
votes
6 answers

How to initialize const in a struct in C (with malloc)

I have tried; void *malloc(unsigned int); struct deneme { const int a = 15; const int b = 16; }; int main(int argc, const char *argv[]) { struct deneme *mydeneme = malloc(sizeof(struct deneme)); return 0; } And this is the…
yasar
  • 13,158
  • 28
  • 95
  • 160
32
votes
2 answers

How does Intel TBB's scalable_allocator work?

What does the tbb::scalable_allocator in Intel Threading Building Blocks actually do under the hood ? It can certainly be effective. I've just used it to take 25% off an apps' execution time (and see an increase in CPU utilization from ~200% to…
timday
  • 24,582
  • 12
  • 83
  • 135
32
votes
3 answers

Does malloc reserve more space while allocating memory?

I am observing the following behavior in my test program: I am doing malloc() for 1 MB and then free() it after sleep(10). I am doing this five times. I am observing memory consumption in top while the program is running. Once free()-d, I am…
user1228352
  • 569
  • 6
  • 21