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
32
votes
5 answers

malloc behaviour on an embedded system

I'm currently working on an embedded project (STM32F103RB, CooCox CoIDE v.1.7.6 with arm-none-eabi-gcc 4.8 2013q4) and I'm trying to understand how malloc() behaves on plain C when the RAM is full. My STM32 has 20kB = 0x5000Bytes of RAM, 0x200 are…
Boern
  • 7,233
  • 5
  • 55
  • 86
32
votes
4 answers

What is C++ version of realloc(), to allocate the new buffer and copy the contents from the old one?

In C we used malloc(), free(), but in C++ youare using new, delete, but in C we also have realloc, which will alloc the new block and copy the old data (common minimum) and then free the old data bock. So what is the C++ version of that? I can write…
exebook
  • 32,014
  • 33
  • 141
  • 226
32
votes
3 answers

Behaviour of malloc with delete in C++

int *p=(int * )malloc(sizeof(int)); delete p; When we allocate memory using malloc then we should release it using free and when we allocate using new in C++ then we should release it using delete. But if we allocate memory using malloc and then…
Luv
  • 5,381
  • 9
  • 48
  • 61
31
votes
6 answers

C++ memory allocation mechanism performance comparison (tcmalloc vs. jemalloc)

I have an application which allocates lots of memory and I am considering using a better memory allocation mechanism than malloc. My main options are: jemalloc and tcmalloc. Is there any benefits in using any of them over the other? There is a good…
Shayan Pooya
  • 1,049
  • 1
  • 13
  • 22
31
votes
6 answers

How big can a malloc be in C?

I have a malloc in C that is 26901^2*sizeof(double) This got me thinking what the largest value can be here? Also, would I have any problems defining a macro to access this 2D array? #define DN(i,j) ((int)i * ny + (int)j) Because this seems to…
Derek
  • 11,715
  • 32
  • 127
  • 228
31
votes
3 answers

Dynamic array in C — Is my understanding of malloc and realloc correct?

I am learning how to create dynamic 1D arrays in C. The code below tries to do the following: Using malloc, create a dynamic array of length 10, that holds values of type double. Set each entry of the array to j/100 for j = 0, 1,..., 9. Then print…
Legendre
  • 3,108
  • 7
  • 31
  • 46
30
votes
6 answers

Allocate memory and save string in c

I was wondering why the following code isnt't working int main(int argc, char **argv) { char *test = (char*) malloc(12*sizeof(char)); test = "testingonly"; free(test); } After thinking about it my assumption was that first i allocate…
pluckyDuck
  • 1,099
  • 3
  • 13
  • 14
30
votes
10 answers

Is it possible to use a C++ smart pointers together with C's malloc?

Some of my code still uses malloc instead of new. The reason is because I am afraid to use new because it throws exception, rather than returning NULL, which I can easily check for. Wrapping every call to new in a try{}catch(){} also doesn't look…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
30
votes
8 answers

Minimizing the amount of malloc() calls improves performance?

Consider two applications: one (num. 1) that invokes malloc() many times, and the other (num. 2) that invokes malloc() few times. Both applications allocate the same amount of memory (assume 100MB). For which application the next malloc() call will…
Dor
  • 7,344
  • 4
  • 32
  • 45
30
votes
2 answers

Why type cast a void pointer?

Being new to C, the only practical usage I have gotten out of void pointers is for versatile functions that may store different data types in a given pointer. Therefore I did not type-cast my pointer when doing memory allocation. I have seen some…
Ann
  • 693
  • 3
  • 12
  • 17
29
votes
5 answers

dynamically allocated memory after program termination

When a C/C++ program containing the dynamically allocated memory(using malloc/new) without free/delete calls is terminated, what happens to that dynamically allocated memory? Does the operating system takes back the memory or does that memory…
justin waugh
  • 885
  • 3
  • 12
  • 22
29
votes
2 answers

Why new std::nothrow version is not widely used

According to C++ reference, you can new an object by: MyClass * p1 = new MyClass; or by MyClass * p2 = new (std::nothrow) MyClass; The second one will return a null pointer instead of throwing an exception. However, I hardly see this version in my…
Deqing
  • 14,098
  • 15
  • 84
  • 131
28
votes
7 answers

How do I create an array in C++ which is on the heap instead of the stack?

I have a very large array which must be 262144 elements in length (and potentially much larger in future). I have tried allocating the array on the stack like so: #define SIZE 262144 int myArray[SIZE]; However, it appears that when I try and add…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
28
votes
4 answers

Expanding an array with malloc

I'm a bit new to malloc and C in general. I wanted to know how I can, if needed, extend the size of an otherwise fixed-size array with malloc. Example: #define SIZE 1000 struct mystruct { int a; int b; char c; }; mystruct myarray[ SIZE ]; int…
Mal Ock
  • 283
  • 1
  • 3
  • 4
28
votes
10 answers

What if malloc fails?

If a malloc allocation fails, should we try it again? In something like this: char* mystrdup(const char *s) { char *ab = NULL; while(ab == NULL) { ab=(char*)malloc(strlen(s)+1); } strcpy(ab, s); return ab; } Is the…
sam32
  • 325
  • 1
  • 4
  • 11