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

Why does scanf and malloc to char pointer work even if the size is not specified?

I am refreshing my C skills. I am using a char *s and using malloc to allocate memory to the s. Then using scanf, I read the input to s. But my question is I haven't specified a size for the memory chunk. But the program works. How does the memory…
user235273
3
votes
1 answer

Can we use doubly linked list in C without dynamic memory allocation?

I am trying to use a doubly linked list data structure to implement a replacement policy in a buffer manager. But my C program doesn't have a linked list library so I just defined the data structure by myself. The problem is: Can I avoid doing any…
runcode
  • 3,533
  • 9
  • 35
  • 52
3
votes
3 answers

Efficient allocation of dynamic arrays within mmap'ed memory

I have a very large (fixed at runtime, around 10 - 30 million) number of arrays. Each array is of between 0 and 128 elements that are each 6 bytes. I need to store all the arrays in mmap'ed memory (so I can't use malloc), and the arrays need to be…
Max
  • 2,760
  • 1
  • 28
  • 47
3
votes
3 answers

How to malloc a 2d jagged array using a pointer passed by reference to a function in C

I have a 2D jagged array declared in my main() block. This is to be passed to a function to have memory allocated to it. The following is the most reduced case which compiles but crashes when it runs. Where am I going wrong? #include…
Brendan
  • 18,771
  • 17
  • 83
  • 114
3
votes
1 answer

zlib: thread safe zalloc and zfree in C?

I am using the zlib C library to decompress data received from a network stream, and I have two streams running in two separate NSThreads. As per zlib documentation, decompressing two different zlib streams in two threads requires zalloc and zfree…
R.S
  • 321
  • 3
  • 15
3
votes
5 answers

Malloc with structs in C

I have a struct: struct numbers_struct { char numbers_array[1000]; }; struct numbers_struct numbers[some_size]; After creating struct, there is an integer number as an input: scanf("%d",&size); I need to use malloc(size) and specify the size of…
Jack Jackson
  • 77
  • 1
  • 3
  • 11
3
votes
4 answers

Allocating memory and freeing them. Should we set them to NULL?

gcc (GCC) 4.7.0 c89 Hello, I am wondering if I am thinking correctly here. When I allocate memory using malloc. malloc will return a pointer to a size in memory. So before I allocate my memory all pointers will have the value NULL. Using this code…
ant2009
  • 27,094
  • 154
  • 411
  • 609
3
votes
6 answers

C how to free malloc'ed memory when program can encounter error and exit at different places?

I'm struggling to come up with a clean way to handle my allocated memory in C. Suppose I have something like this: void function(int arg) { char *foo; foo = (char *)malloc(sizeof(char) * 100); int i = func1(arg, &foo); char *bar; bar =…
danqing
  • 3,348
  • 2
  • 27
  • 43
3
votes
2 answers

memory error using valgrind, strcpy

I've been using valgrind, but for some reason I keep getting a memory error using a simple string copy with two strings that are the same size, in C. The Code effected is: node->entry =…
Cail Demetri
  • 2,138
  • 3
  • 22
  • 24
3
votes
3 answers

How to reallocate memory for a 2D float array?

I tried the following to reallocate a 2D float array whose size chages from 2X2 to 3X3. The code throws a segfault while trying to realloc memory for weights[2]. num_vertices = 2; float **weights = malloc(num_vertices*sizeof(float *)); //…
stressed_geek
  • 2,118
  • 8
  • 33
  • 45
3
votes
5 answers

Difficulty understanding behavior of free()

int main() { int *ptr, **ptr1; ptr = (int*)malloc(sizeof(int)); ptr1 = (int**)malloc(sizeof(int)); free(ptr); *ptr = 12345; ptr1 = &ptr; //free(ptr); //**ptr1 = 23456; printf("%d \n", **ptr1); …
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
3
votes
2 answers

Allocate memory for 2D array with minimum number of malloc calls

I am using the below code snippet to allocate memory for 2D array using minimum number of malloc() calls. I want to access the array using subscripts, p[i][j]. #define ROW 3 #define COL 2 int main() { void **ptr = malloc( ROW*COL* sizeof(int)…
Green goblin
  • 9,898
  • 13
  • 71
  • 100
3
votes
1 answer

Free/malloc functions in windows kernel

Where i can find free and malloc functions in kernel? I do include stdlib.h but free and malloc functions are not in stdlib.h. Where i can find these functions? Thanks!
RomanKarpuk
  • 57
  • 1
  • 7
3
votes
2 answers

Implement a user-defined malloc() function?

How do you create a new malloc() function defined in C language ? I don't even have an elflike hint as to how to do this , how to map virtual space address with physical space , what algorithm to follow ? I do know that malloc() is not so…
h4ck3d
  • 6,134
  • 15
  • 51
  • 74
3
votes
1 answer

Does stl library use malloc while copying pointers?

I have a question regarding copying pointers in the stl library. Say I define: struct A{ int x; } std::map map1; I then populate map1 using memory from the heap using malloc for the pointer to A. Then I do std::map