Question:
why we need to use malloc() ?
Saravanan R
2013-07-10 04:37:45 UTC
in a c the progrom
int *p;
*p=45;
printf("%d",*p);
or
char *p;
p="hello";
printf("%s",p);

the both statements i tested work perfectly so why i need malloc?
Four answers:
anonymous
2013-07-10 07:33:48 UTC
The function malloc is used to allocate a certain amount of memory during the execution of a program. The malloc function will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory.



When the amount of memory is not needed anymore, you must return it to the operating system by calling the function free.



Take a look at the following example:



#include

#include //used for malloc, on windows I believe

//you still use windows.h instead



int main()

{

int *ptr_one;



ptr_one = (int *)malloc(sizeof(int));



if (ptr_one == 0)

{

printf("ERROR: Out of memory\n");

return 1;

}



*ptr_one = 25;

printf("%d\n", *ptr_one);



free(ptr_one);



return 0;

}



The malloc statement will ask for an amount of memory with the size of an integer (32 bits or 4 bytes). If there is not enough memory available, the malloc function will return a NULL. If the request is granted a block of memory is allocated (reserved). The address of the reserved block will be placed into the pointer variable.



The if statement then checks for the return value of NULL. If the return value equals NULL, then a message will be printed and the programs stops. (If the return value of the program equals one, than that’s an indication that there was a problem.)



The number twenty-five is placed in the allocated memory. Then the value in the allocated memory will be printed. Before the program ends the reserved memory is released.



In such a small example like you provided, this is not necessary, but on larger amounts of memory, you should always use malloc. Especially when talking about memory for structs and representing data structures. It's good to get into the practice of trying to allocate memory, checking if the allocation is allowed, and when finished, freeing your allocated memory. It will help in debugging, and make your code more portable. This is especially crucial if you want to be a software developer.
michaeljhuman
2013-07-10 10:59:54 UTC
I am surprised the first case worked. You assign 45 to the integer pointed at by p, but never initialized p. It was a bug to try to do so, and I would have expected the program to crash. Maybe p pointed at valid memory for some reason.



In the second case, you pointed p to a constant string. This is valid. But you should never change the contents of the string. A long time ago, attempting that would probably have crashed the program, but in modern compilers, it probably works as long as you don't overflow the memory allocated for the constant string. If you do plan on changing the string that p points to, you would first allocated enough memory for the string using 'p = malloc( ...', and then strcpy the string constant like 'strcpy( p, "hello")'. You now have a valid string, and can change it if you like, as long as you keep the string small enough to fit in the space allocated.
ammayar
2013-07-10 06:37:02 UTC
The function malloc() is used for memory allocation.
Robert
2013-07-10 05:17:00 UTC
You can often fluke it, but you are risking failure. Have a read of http://stackoverflow.com/questions/7075117/in-memcpy-how-to-handle-memory-overflow


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...