Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Programming: malloc() inside another function

I need help with malloc() inside another function.

I'm passing a pointer and size to the function from my main() and I would like to allocate memory for that pointer dynamically using malloc() from inside that called function, but what I see is that.... the memory, which is getting allocated, is for the pointer declared within my called function and not for the pointer which is inside the main().

How should I pass a pointer to a function and allocate memory for the passed pointer from inside the called function?


I have written the following code and I get the output as shown below.

SOURCE:

int main() {    unsigned char *input_image;    unsigned int bmp_image_size = 262144;     if(alloc_pixels(input_image, bmp_image_size)==NULL)      printf("\nPoint2: Memory allocated: %d bytes",_msize(input_image));    else      printf("\nPoint3: Memory not allocated");         return 0; }  signed char alloc_pixels(unsigned char *ptr, unsigned int size) {     signed char status = NO_ERROR;     ptr = NULL;      ptr = (unsigned char*)malloc(size);      if(ptr== NULL)     {         status = ERROR;         free(ptr);         printf("\nERROR: Memory allocation did not complete successfully!");     }      printf("\nPoint1: Memory allocated: %d bytes",_msize(ptr));      return status; } 

PROGRAM OUTPUT:

Point1: Memory allocated ptr: 262144 bytes Point2: Memory allocated input_image: 0 bytes 
like image 643
HaggarTheHorrible Avatar asked May 14 '10 22:05

HaggarTheHorrible


People also ask

Can you use malloc inside a function?

Memory allocation (malloc), is an in-built function in C. This function is used to assign a specified amount of memory for an array to be created. It also returns a pointer to the space allocated in memory using this function.

What happens if you malloc something twice?

When you call malloc a second time, it has no way of knowing you are doing anything with newPtr . It merely allocates new space and returns a pointer to it. Then that new pointer is assigned to newPtr , which erases the old value that was in newPtr .

What does malloc () calloc () realloc () free () do?

allocates multiple block of requested memory. realloc() reallocates the memory occupied by malloc() or calloc() functions. free() frees the dynamically allocated memory.

How does malloc work in C internally?

When one calls malloc , memory is taken from the large heap cell, which is returned by malloc . The rest is formed into a new heap cell that consists of all the rest of the memory. When one frees memory, the heap cell is added to the end of the heap's free list.


1 Answers

How should I pass a pointer to a function and allocate memory for the passed pointer from inside the called function?

Ask yourself this: if you had to write a function that had to return an int, how would you do it?

You'd either return it directly:

int foo(void) {     return 42; } 

or return it through an output parameter by adding a level of indirection (i.e., using an int* instead of int):

void foo(int* out) {     assert(out != NULL);     *out = 42; } 

So when you're returning a pointer type (T*), it's the same thing: you either return the pointer type directly:

T* foo(void) {     T* p = malloc(...);     return p; } 

or you add one level of indirection:

void foo(T** out) {     assert(out != NULL);     *out = malloc(...); } 
like image 91
jamesdlin Avatar answered Nov 02 '22 02:11

jamesdlin