Hi People, Please find my queries mention below. -------------------------------------------------------------------------------------------------------- How is memory allocated by malloc? Is the memory allocated contiguous? Is so, What if the requested number of bytes are not available contigously? Is casting really required?? If u don't cast the pointer what will be the disadvantage? What is the difference between calloc and malloc in terms of memory allocation? -------------------------------------------------------------------------------------------------------- I will be happy if some body could give me a diagramatic representation of the memory allocated by these two functions(calloc & malloc). Regards Swapna
Did you read the documentation? One must presume that this applies to a contiguous array, not so? 'Nuff said? Casting the return of malloc is necessary in C++ because of stronger typing constraints. However, malloc should not be used in C++, so the question is (should be) moot. In C, the void pointer is intrinsically convertible. Casting in C may actually hide potential errors if future changes to code (pointer types) are not made completely and thoroughly.
malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available and use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object in the heap and returns a valid pointer to that item.
Lemme first admit that i have never gone thru the documentaion.. have always referred books.. thats it...Request you to pls be patient with my questions When i worked on my VC++ complier I found this: int *p; p= malloc(8); No errors ---------------------------------------- int *p; p= malloc(6); Run-time error ----------------------------------------- Do I have to always use sizeof() with malloc? Cannot I allocate an integer array of 4 bytes with the following code assuming int on my system occupies 4 bytes? int *p; p=malloc(16); assume, p=0; then is p+1=4 ? or p+1= 1? If p+1=4 then what is the difference between calloc and malloc except for 0-byte filling. --------------------------------------------------------------------------------------------------- Hope my questions are not silly.. Swapna
#include<stdlib.h> void main() { int *p; p= calloc(6); } -------------------Configuration: swap - Win32 Debug-------------------- Compiling... swap.c c:\documents and settings\sharmask\swap.c(5) : error C2198: 'calloc' : too few actual parameters Error executing cl.exe. swap.obj - 1 error(s), 0 warning(s) ------------------------------------------------------------------------------------------------------ That says it all...
It isn't that your questions are silly, it's that your attitude is silly. Tutorial material is to give you a view of how to use the language. Reference material is to give you a precise lesson on how to use a specific part of the language. You need to look up malloc and you need to look up calloc. Look at the declarations. Look at the return type and the arguments to see what their types are, and what their values are for. Read the explanatory material concerning them. Read about their gotchas and how they tell you that they failed, when they fail. It is silly, when you're reading a book, to pop into a forum and ask what a word means. Use the dictionary. It comes in the form of pressing the help key, or typing "man malloc", or typing "malloc" into a Google search bar. Learning to do that is part of being a big boy. When you have done that, and still can't seem to understand malloc/calloc, or have some problem using them, I will be glad to answer your questions.
Hello DaWei, Yup u r right... I did search many websites for a clear explanation on these functions.. but i was not satisfied with their explanation. All the explanations gave information only on the syntax and their differences. I always try to give my max effort for a problem. When i am helpless and exhaustive i seek help in forums. Please try to help me out... Liked your last line.. "When you have done that, and still can't seem to understand malloc/calloc, or have some problem using them, I will be glad to answer your questions."
See Swapna, M not much with VC++, but i can very well tell you about malloc and calloc in Turbo C. int *p; p=(int*)malloc(sizeof(int)); this statement will allocate 2 bytres of memory to p and will return a void pointer which is then typecasted by (int*). to allocate memory to a block using malloc requires looping of statement.... This allocation is successful tries to allocate memory sequentiall but if it fails returns a null pointer. Calloc: int *p; p=(in t*)calloc(n,sizeof(int)); n represents no. of blocks. this is useful to allocate memory to a block in a single statement. Both alocates memory sequentiall...... I hope that this would clear your doubts. Although i have explainned in Turbo, concepts would be same in VC++ too, i think.......
No loop is required to allocate a block of memory. iPtr = (int *) malloc (64 * sizeof (int)); will allocate a block of 64 integers, regardless of their size on the system in question. Since there are typically 16 or more bytes of memory overhead to manage a malloc'd block, it is inadvisable to allocate dynamic memory in small chunks.
Is it compulsory or a must to cast the return value to a specify data type ? Thanks for your explanation.
I have read the post but i don't understand because of my poor english. I am a Malaysian. Thanks for oyur patient and help. Your help is greatly appreciated by me and others.