Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Malloc & Calloc difference in terms of memory allocated (http://www.go4expert.com/forums/malloc-calloc-difference-terms-memory-t3933/)

swapnaoe 19Apr2007 12:13

Malloc & Calloc difference in terms of memory allocated
 
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

DaWei 19Apr2007 12:59

Re: Malloc & Calloc difference in terms of memory allocated
 
Did you read the documentation?
Quote:

The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object.
One must presume that this applies to a contiguous array, not so?
Quote:

calloc calls malloc
'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.

shabbir 19Apr2007 13:19

Re: Malloc & Calloc difference in terms of memory allocated
 
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.

Quote:

Originally Posted by MSDN
calloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when calloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call


swapnaoe 19Apr2007 14:29

Re: Malloc & Calloc difference in terms of memory allocated
 
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

shabbir 19Apr2007 15:29

Re: Malloc & Calloc difference in terms of memory allocated
 
Why dont you try the following as share with us what happens?

int *p;
p= calloc(6);

swapnaoe 19Apr2007 16:24

Re: Malloc & Calloc difference in terms of memory allocated
 
#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...

DaWei 19Apr2007 17:54

Re: Malloc & Calloc difference in terms of memory allocated
 
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.

swapnaoe 19Apr2007 18:02

Re: Malloc & Calloc difference in terms of memory allocated
 
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."

wrecker 20Apr2007 02:01

Re: Malloc & Calloc difference in terms of memory allocated
 
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.......

DaWei 20Apr2007 02:31

Re: Malloc & Calloc difference in terms of memory allocated
 
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.


All times are GMT +5.5. The time now is 15:34.