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

Heap allocate a 2D array (not array of pointers)

I am writing C code and I would like to heap allocate 512*256 bytes. For my own convenience I would like to be able to access the elements with the syntax array[a][b]; no arithmetic to find the right index. Every tutorial I see online tells me to…
Paul
  • 849
  • 1
  • 7
  • 18
27
votes
3 answers

Malloc error "can't allocate region" failed with error code 12. Any idea how to resolve this?

i am getting this error and dont know what to do with that: AppName(3786,0xa0810540) malloc: *** mmap(size=16777216) failed (error code=12) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug If i set a breakpoint…
brush51
  • 5,691
  • 6
  • 39
  • 73
27
votes
5 answers

Malloc vs New for Primitives

I understand the benefits of using new against malloc in C++. But for specific cases such as primitive data types (non array) - int, float etc., is it faster to use malloc than new? Although, it is always advisable to use new even for primitives, if…
Rajeev Mehta
  • 649
  • 9
  • 18
27
votes
2 answers

How to properly malloc for array of struct in C

I will read in two set of char* (or strings) using strtok, and since those two set of chars are related, (address : command\n) I decided to use a structure. struct line* array = (struct line*)malloc(sizeof(file) * sizeof(struct line*)); This line…
o0tomato0o
  • 321
  • 1
  • 3
  • 7
26
votes
3 answers

What if I allocate memory using mmap instead of malloc?

What are the disadvantages of allocating memory using mmap (with MAP_PRIVATE and MAP_ANONYMOUS) than using malloc? For data in function scope, I would use stack memory anyway and therefore not malloc. One disadvantage that comes to mind is for…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
26
votes
7 answers

Is malloc deterministic?

Is malloc deterministic? Say If I have a forked process, that is, a replica of another process, and at some point both of them call the malloc function. Would the address allocated be the same in both processes? Assuming that other parts of…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
26
votes
4 answers

Have you ever obtained a significant speedup by using boost::pool?

I've played with boost::pool a few times in places where it seemed to me I was seriously hammering the heap with a lot of object "churn". Generally I've used boost::object_pool, or boost::pool_alloc as an STL template parameter. However the result…
timday
  • 24,582
  • 12
  • 83
  • 135
26
votes
4 answers

In multithreaded C/C++, does malloc/new lock the heap when allocating memory

I'm curious as to whether there is a lock on memory allocation if two threads simultaneously request to allocate memory. I am using OpenMP to do multithreading, C++ code. OS's: mostly linux, but would like to know for Windows and Mac as well.
Janik Zikovsky
  • 3,086
  • 7
  • 26
  • 34
26
votes
4 answers

What is aligned memory allocation?

I also want to know whether glibc malloc() does this.
Anonymous
  • 1,287
  • 6
  • 20
  • 21
25
votes
8 answers

Is freeing allocated memory needed when exiting a program in C

If I allocated memory in my C program using malloc and now I want to exit, do I have to free the allocated memory, or can I assume that since my entire program terminates, it will be freed by the OS? I run in Linux environment.
SIMEL
  • 8,745
  • 28
  • 84
  • 130
25
votes
4 answers

What happens if I use malloc twice on the same pointer (C)?

Say for instance I created a pointer newPtr and I use malloc(some size) and then later I use malloc(some size) again with the same pointer. What happens? Am i then creating a second block of memory the same size of the first one? Does newPtr point…
Ace
  • 462
  • 4
  • 10
  • 17
25
votes
8 answers

returning string from function without malloc

Is it possible to return string from a function without calling malloc? I have a function as below: char* getString(){ char tempStr[20] // open file, read char by char and assign to tempStr // .... char* str =…
blenzcoffee
  • 851
  • 1
  • 11
  • 35
25
votes
2 answers

malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))

I acknowledge that all three of these have a different meaning. But, I don't understand on what particular instances would each of these apply. Can anyone share an example for each of these? Thank you. malloc(sizeof(int)) …
rajagrawal
  • 622
  • 4
  • 14
  • 26
25
votes
5 answers

How do I correctly set up, access, and free a multidimensional array in C?

I have seen dozens of questions about “what’s wrong with my code” regarding multidimensional arrays in C. For some reason people can’t seem to wrap their head around what is happening here, so I decided to answer this question as a reference to…
Mike
  • 47,263
  • 29
  • 113
  • 177
24
votes
4 answers

Windows malloc replacement (e.g., tcmalloc) and dynamic crt linking

A C++ program that uses several DLLs and QT should be equipped with a malloc replacement (like tcmalloc) for performance problems that can be verified to be caused by Windows malloc. With linux, there is no problem, but with windows, there are…
Weidenrinde
  • 2,152
  • 1
  • 20
  • 21