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
50
votes
4 answers

Why does C++ require a cast for malloc() but C doesn't?

I have always been curious about this - why do in C++ I have to cast return value from malloc but not in C? Here is the example in C++ that works: int *int_ptr = (int *)malloc(sizeof(int*)); And here is the example in C++ that doesn't work (no…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
50
votes
9 answers

If free() knows the length of my array, why can't I ask for it in my own code?

I know that it's a common convention to pass the length of dynamically allocated arrays to functions that manipulate them: void initializeAndFree(int* anArray, size_t length); int main(){ size_t arrayLength = 0; scanf("%d", &arrayLength); …
Chris Cooper
  • 17,276
  • 9
  • 52
  • 70
47
votes
4 answers

Is it well-defined to use a pointer pointing to one-past-malloc?

In C, it is perfectly well to make a pointer that points to one past the last element of an array and use it in pointer arithmetics, as long as you don't dereference it: int a[5], *p = a+5, diff = p-a; // Well-defined However, these are UBs: p =…
iBug
  • 35,554
  • 7
  • 89
  • 134
45
votes
10 answers

Create a wrapper function for malloc and free in C

I am trying to create wrapper functions for free and malloc in C to help notify me of memory leaks. Does anyone know how to declare these functions so when I call malloc() and free() it will call my custom functions and not the standards lib…
Dan Snyder
44
votes
2 answers

If `malloc(0)` returns a non-null pointer, can I pass that to `free`?

I've been reading over discussions of how malloc behaves when you request a zero sized block. I understand that the behavior of malloc(0) is implementation-defined, and it is supposed to either return a null pointer, or a non-null pointer that I am…
pnkfelix
  • 3,770
  • 29
  • 45
43
votes
4 answers

Specifically, how does fork() handle dynamically allocated memory from malloc() in Linux?

I have a program with a parent and a child process. Before the fork(), the parent process called malloc() and filled in an array with some data. After the fork(), the child needs that data. I know that I could use a pipe, but the following code…
pcd6623
  • 706
  • 2
  • 9
  • 15
43
votes
10 answers

Can you allocate a very large single chunk of memory ( > 4GB ) in c or c++?

With very large amounts of ram these days I was wondering, it is possible to allocate a single chunk of memory that is larger than 4GB? Or would I need to allocate a bunch of smaller chunks and handle switching between them? Why??? I'm working on…
KPexEA
  • 16,560
  • 16
  • 61
  • 78
43
votes
5 answers

C: Correctly freeing memory of a multi-dimensional array

Say you have the following ANSI C code that initializes a multi-dimensional array : int main() { int i, m = 5, n = 20; int **a = malloc(m * sizeof(int *)); //Initialize the arrays for (i = 0; i < m; i++) { …
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
42
votes
5 answers

Should I free memory before exit?

Should I free all my mallocated memory when I am exiting program in the due of error? something = (char**) malloc (x * sizeof(char*)); for (i = 0; i < x; i++) something[i] = (char*) malloc (y + 1); ... if (anything == NULL) { printf("Your…
Lucfia
  • 621
  • 1
  • 7
  • 14
42
votes
11 answers

Is it better to allocate memory in the power of two?

When we use malloc() to allocate memory, should we give the size which is in power of two? Or we just give the exact size that we need? Like //char *ptr= malloc( 200 ); char *ptr= malloc( 256 );//instead of 200 we use 256 If it is better to give…
Andrew-Dufresne
  • 5,464
  • 7
  • 46
  • 68
42
votes
8 answers

Malloc and constructors

Unlike new and delete expressions, std::malloc does not call the constructor when memory for an object is allocated. In that case, how must we create an object so that the constructor will also be called?
ckv
  • 10,539
  • 20
  • 100
  • 144
41
votes
6 answers

aligned malloc() in GCC?

Is there any standardized function in GCC or glibc to allocate memory block at aligned pointer? Like _align_malloc() in MSVC?
user313885
40
votes
10 answers

Under what circumstances can malloc return NULL?

It has never happened to me, and I've programming for years now. Can someone give me an example of a non-trivial program in which malloc will actually not work? I'm not talking about memory exhaustion: I'm looking for the simple case when you are…
RanZilber
  • 1,840
  • 4
  • 31
  • 42
40
votes
2 answers

How do I free memory in C?

I'm writing code which has a lot of 1 & 2 dimensional arrays. I got "error: can't allocate region" and I think its because too much memory is allocated. I use "malloc" and "free" functions, but I'm not sure I'm using them correctly. Maybe you know…
andrey
  • 671
  • 1
  • 9
  • 20
40
votes
14 answers

Does malloc() allocate a contiguous block of memory?

I have a piece of code written by a very old school programmer :-) . it goes something like this typedef struct ts_request { ts_request_buffer_header_def header; char package[1]; } ts_request_def; ts_request_def*…
user66854
  • 865
  • 3
  • 11
  • 15