what is the use of malloc,calloc,realloc ?? i studied i didnt get proper knowledge .why instead of this why cant we use array???

Recommended Answers

All 4 Replies

what is the use of malloc,calloc,realloc ?? i studied i didnt get proper knowledge .why instead of this why cant we use array???

I can't explain malloc, calloc, and realloc better than all of the tutorials.
cplusplus.com is my go-to site.

http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/

As to why you can't use an array, the question shows the misunderstanding. You use malloc to allocate memory so you can use an array.

int* array = (int*) malloc(100 * sizeof(int));
// now I can use it.  Without malloc, I couldn't.
array[32] = 7;

why instead of this why cant we use array???

And if you don't know the size of the array at compile time?

you need it for dynamic memory allocation. just think of a program where sb has to input a n-dimensional matrix of unkown n. let the user input n and then go forward to get the necessary memory space.
next advantage of malloc is that all the memory-places allocated stick next to each other. if you would set up static variables, their locations would be at random places. for example you want to allocate a dynamic nxn-matrix:

double *matrix;
int n;
printf("input dimension:");
scanf("%d",&n);
matrix=(double*)malloc(n*sizeof(double));

malloc is a void function so you have to bring it into a any pointer type like (int*),(float*),or(double*)
matrix then represents the start address of your memoryspace allocated.
You can access these spaces like following:

scanf("%lf",matrix[i]);  // i=0,1,2...,n-1
//that should be the same as
scanf("%lf",matrix)          //for i=0
//or
scanf("%d",matrix+3);        //for i=3

you wont need the address operator "&" cause pointers are adresses.

malloc is a void function so you have to bring it into a any pointer type like (int*),(float*),or(double*)

That's a confusing statement, and it's also incorrect. When people say "void function" they typically mean a function that returns void, not a generic pointer to void. A more accurate, but still incorrect statement would be:

"malloc() returns a pointer to void, so you have to cast it into the destination pointer type."

But C allows an implicit conversion between void* and any non-function pointer type without a cast. In fact, adding the cast is dangerous because it can hide the warnings you would get by forgetting to include stdlib.h.

A good pattern for malloc() that I see a lot is this:

p = malloc(n * sizeof *p);

No cast is needed, and sizeof *p gets the size of the pointed-to type without having to name it explicitly. That way if you decide to change the type, you don't need to change the call to malloc(). So your code, with a few alterations, would look like this:

double *matrix;
int n;

printf("input dimension: ");
fflush(stdout); // Force the prompt to show before blocking

if (scanf("%d",&n) != 1) // Always check input for failure
{
    // Handle failure
}

matrix = malloc(n * sizeof *matrix);

if (!matrix) // Always check malloc() for failure too :)
{
    // Handle failure
}

One caveat to omitting the cast is that if you're writing code that needs to compile as both C and C++, the cast is required. C++ has different rules for converting from void*.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.