Top "Malloc" questions

The malloc function performs dynamic memory allocation in C and is part of the standard library.

Difference between malloc and calloc?

What is the difference between doing: ptr = (char **) malloc (MAXELEMS * sizeof(char *)); or: ptr = (char **) calloc (MAXELEMS, sizeof(char*)); When …

c malloc calloc
In what cases do I use malloc and/or new?

I see in C++ there are multiple ways to allocate and free data and I understand that when you call …

c++ memory-management malloc new-operator
Do I cast the result of malloc?

In this question, someone suggested in a comment that I should not cast the result of malloc, i.e. int *…

c malloc casting
Using malloc for allocation of multi-dimensional arrays with different row lengths

I have the following C code : int *a; size_t size = 2000*sizeof(int); a = (int *) malloc(size); which works fine. …

c arrays malloc
Incompatible implicit declaration of built-in function ‘malloc’

I'm getting this error: warning: incompatible implicit declaration of built-in function ‘malloc’ I am trying to do this: fileinfo_list* …

c struct malloc
How do malloc() and free() work?

I want to know how malloc and free work. int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); …

c++ c memory-management malloc free
How to dynamically allocate memory space for a string and get that string from user?

I want to read input from user using C program. I don't want to use array like, char names[50]; because …

c string memory-management malloc dynamic
When to use malloc for char pointers

I'm specifically focused on when to use malloc on char pointers char *ptr; ptr = "something"; ...code... ...code... ptr = "something else"; …

c pointers malloc char
Dynamically create an array of strings with malloc

I am trying to create an array of strings in C using malloc. The number of strings that the array …

c arrays malloc
Why is the use of alloca() not considered good practice?

alloca() allocates memory on the stack rather than on the heap, as in the case of malloc(). So, when I …

c stack malloc allocation alloca