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

Error: Conversion to non-scalar type requested

I'm having a small problem trying to malloc this struct. Here is the code for the structure: typedef struct stats { int strength; int wisdom; int agility; }…
atb
  • 943
  • 4
  • 14
  • 30
20
votes
3 answers

Can memset() be called with a null pointer if the size is 0?

For one reason or another, I want to hand-roll a zeroing version of malloc(). To minimize algorithmic complexity, I want to write: void * my_calloc(size_t size) { return memset(malloc(size), 0, size); } Is this well-defined when size == 0? It…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
20
votes
2 answers

How does mmap work?

I am working on programs in Linux which needs mmap file from harddrive, but i have a question, what can make it fail. Like if all the memories are fragmented, which has only 200M each, but i want to mmap a file to a memory of 1000M, will it…
StevenWang
  • 3,625
  • 4
  • 30
  • 40
20
votes
1 answer

Is there really no version of realloc() supporting alignment?

There exist several aligned versions of the venerable malloc(), e.g.: #include int posix_memalign(void **memptr, size_t alignment, size_t size); void *aligned_alloc(size_t alignment, size_t size); #include void…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
20
votes
4 answers

explanation to aligned malloc implementation

This is not homework, this is purely for my own personal education. I couldn't figure out how to implement an aligned malloc so looked online and found this website. For the ease of reading I will post the code below: #include #include…
flashburn
  • 4,180
  • 7
  • 54
  • 109
20
votes
3 answers

Allocating char array using malloc

Considering the following line: char *p = malloc( sizeof(char) * ( len + 1 ) ); Why is sizeof(char) used? It's not necessary, is it? Or Is it just a matter of style? What advantages does it have?
Nyan
  • 2,360
  • 3
  • 25
  • 38
20
votes
5 answers

How to update other pointers when realloc moves the memory block?

The realloc reference says: The function may move the memory block to a new location, in which case the new location is returned. Does it mean that if I do this: void foo() { void* ptr = malloc( 1024 ); unsigned char* cptr =…
zajcev
  • 303
  • 2
  • 7
20
votes
3 answers

What is the "correct" way to reconcile malloc and new in a mixed C/C++ program?

I have a mixed C/C++ program. It contains a flex/bison parser which targets C, while the remainder is C++. Being C, the generated parser and scanner manage their memory with malloc, realloc and free. They are good enough to expose hooks allowing…
phs
  • 10,687
  • 4
  • 58
  • 84
19
votes
9 answers

64 bit large mallocs

What are the reasons a malloc() would fail, especially in 64 bit? My specific problem is trying to malloc a huge 10GB chunk of RAM on a 64 bit system. The machine has 12GB of RAM, and 32 GB of swap. Yes, the malloc is extreme, but why would it be a…
SPWorley
  • 11,550
  • 9
  • 43
  • 63
19
votes
3 answers

Code for malloc and free

Where can I find the code for malloc my gcc compiler is using at the moment? I actually want to write my own malloc function which will be a little different from the original one. I know I can use hooks et all, but I want to see the real code.
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
19
votes
9 answers

Is malloc() initializing allocated array to zero?

Here is the code I'm using: #include #include int main() { int *arr; int sz = 100000; arr = (int *)malloc(sz * sizeof(int)); int i; for (i = 0; i < sz; ++i) { if (arr[i] != 0) { …
devil0150
  • 1,350
  • 3
  • 13
  • 36
19
votes
4 answers

Initialize array of structs in C

I am having a problem initializing an array of structs. I'm not sure if I am doing it right because I get "initialization from incompatible pointer type" & "assignment from incompatible pointer type". I added in the code where I get these warnings,…
user445338
19
votes
2 answers

How much memory does int x[10] allocate?

Is there any difference in the memory usage of these two code lines? int *a = malloc( 10 * sizeof(int) ); int b[10]; The first line should allocate memory for 10 ints and 1 pointer. But I'm not sure about the second. Will that also allocate memory…
Supernormal
  • 962
  • 8
  • 15
19
votes
2 answers

Why can we allocate a 1 PB (10^15) array and get access to the last element, but can't free it?

As known: http://linux.die.net/man/3/malloc By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. In case it turns out that…
Alex
  • 12,578
  • 15
  • 99
  • 195
19
votes
3 answers

Malloc a 2D array in C

Every time I allocate the memory for a 2D array first I create an array of int** and then with a for I allocate the memory for each element. For example: int ** arr = malloc(N*sizeof(int *)); for(i=0; i< N; i++) arr[i] =…
ninazzo
  • 547
  • 1
  • 6
  • 17