freelanceprogrammers.org Forum Index » C

regarding malloc and calloc(why should we use both,however b


View user's profile Post To page top
kvramu1 Posted: Wed Dec 22, 2004 8:58 pm


Joined: 22 Dec 2004

Posts: 1
regarding malloc and calloc(why should we use both,however b
hi all,
      please clear this to me.both malloc and calloc are used for memory allocation only.why not we can use any one of these instead of both?
 
THANKS AND REGARDS
ramakrishna

Yahoo! India Matrimony: Find your life partner
online.
Reply with quote
Send private message
View user's profile Post To page top
sflam108 Posted: Sat Dec 25, 2004 3:04 am


Joined: 25 Jul 2003

Posts: 48
regarding malloc and calloc(why should we use both,however b
Reply embedded...

> From: k ramakrishna [mailto:kvramu1@...]
> Sent: Wednesday, December 22, 2004 8:59 AM
> To: programmers-Town@yahoogroups.com
> Subject: (PT) regarding malloc and calloc(why should we use both,however
> both allocating the memory only)
>
> hi all,
>      please clear this to me.both malloc and calloc are used for memory
> allocation only.why not we can use any one of these instead of both?

calloc() does a little more then malloc() - it also initialized the memory
to zero. Sometimes this is not desirable and the result is not portable.

Shyan
Reply with quote
Send private message
View user's profile Post To page top
rani.deepika@... Posted: Sun Dec 26, 2004 7:14 pm


Joined: 26 Dec 2004

Posts: 1
regarding malloc and calloc(why should we use both,however b
 
malloc only allocates memory but calloc intialise the memory to 0s

On Wed, 22 Dec 2004 k ramakrishna wrote :
>hi all,
>      please clear this to me.both malloc and calloc are used for memory allocation only.why not we can use any one of these instead of both?
>
>THANKS AND REGARDS
>ramakrishna
>
>Yahoo! India Matrimony: Find your life partneronline.
Reply with quote
Send private message
View user's profile Post To page top
deo31 Posted: Mon Dec 27, 2004 9:57 am


Joined: 31 Dec 2004

Posts: 5
regarding malloc and calloc(why should we use both,however b
the question still remains that why we need a malloc when calooc will don the same thing by inistialising the memory with zero rther than some random value.Deepika Rani <rani.deepika@...> wrote:

malloc only allocates memory but calloc intialise the memory to 0sOn Wed, 22 Dec 2004 k ramakrishna wrote :>hi all,>      please clear this to me.both malloc and calloc are used for memory allocation only.why not we can use any one of these instead of both?>>THANKS AND REGARDS>ramakrishna>>Yahoo! India Matrimony: Find your life partneronline. To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Do you Yahoo!? Dress up your holiday email, Hollywood style. Learn more.
Reply with quote
Send private message
View user's profile Post To page top
taxikaps Posted: Mon Dec 27, 2004 12:08 pm


Joined: 03 Jan 2005

Posts: 10
regarding malloc and calloc(why should we use both,however b
deo sharma wrote:
> the question still remains that why we need a malloc when calooc will
> don the same thing by inistialising the memory with zero rther than some
> random value.
>
there are circumstances when zeroing out allocated memory is not
required, so malloc is used. and these circumstances are when the
allocated memory will be stuffed wil data as soon as it is allocated. So
there is no need to zero out everything and waste memory cycles

Kapil
--
PS:
1. Google: Feel free to Google before posting. http://www.google.com

2. Posting: I suggest the group members to avoid top posting, as this
pushes the original question to the bottom. While any body would prefer
to have the main question at the top, and subsequent replies added to
the bottom, so that it is easy to comprehend the flow of information.

3. Do not Hijack threads. If you need to ask a fresh question, do a
little effort of moving mouse to the New or Compose button instead of
Reply. Do not start a New message or thread by hitting "Reply" in your
mail client and changing the subject. When you do this, you mess up
other participant`s ability to read mail in a threaded fashion.

4. HTML Mail: HTML is not email, and email doesn`t contain HTML, so
please turn HTML formatting OFF in your email client. Please Do not use
italics, colors, bold, fonts, pictures, sounds, or other HTML elements.
Please use only 7-bit text when sending email to the lists.

Thanks
Reply with quote
Send private message
View user's profile Post To page top
pnadgir2004 Posted: Mon Dec 27, 2004 12:26 pm


Joined: 27 Dec 2004

Posts: 4
regarding malloc and calloc(why should we use both,however b
Kapil <taxikaps@...> wrote:

deo sharma wrote:> the question still remains that why we need a malloc when calooc will > don the same thing by inistialising the memory with zero rther than some > random value.> there are circumstances when zeroing out allocated memory is not required, so malloc is used. and these circumstances are when the allocated memory will be stuffed wil data as soon as it is allocated. So there is no need to zero out everything and waste memory cycles
Also, note that calloc() is just ALL BITS ZERO. Its *NOT* the same as the "zero initialized" global variables (i.e, a global floating point number or a global pointer getting initialized to "zero" is always correct as per the internal representations (which need not always be all-bits-zero)).
Kapil

Yahoo! India Matrimony: Find your life partner
online.
Reply with quote
Send private message
View user's profile Post To page top
shabble Posted: Wed Dec 29, 2004 5:44 pm


Joined: 29 Dec 2004

Posts: 31
regarding malloc and calloc(why should we use both,however b
From: deo sharma [mailto:deo31@...]

>the question still remains that why we need a malloc when
>calooc will don the same thing by inistialising the memory
>with zero rther than some random value.

Because you want to initialise it to a different value, and having it
initialised twice is pointless?

int i;
void* DefaultValue = (void*)main;
void* pPtrArray = malloc(10*sizeof(*pPtrArray));
for(i=0; i<10; ++i)
pPtrArray[i] = DefaultValue;

What gain does calloc() give you in this instance?

Can you give any good reason why having it filled with `all bits zero`
when this isn`t necessarily the `Null Pointer` (with the associated
extra overhead involved) is any better than gettting randomly filled
memory with no overhead?

--
PJH

bug, n: An elusive creature living in a program that makes it incorrect.
The activity of "debugging", or removing bugs from a program, ends when
people get tired of doing it, not when the bugs are removed. -
"Datamation", January 15, 1984






Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272

Website : www.alderley.com Sales : sales@... Service :
service@...

This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...

This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Reply with quote
Send private message
View user's profile Post To page top
sflam108 Posted: Tue Jan 04, 2005 2:37 am


Joined: 25 Jul 2003

Posts: 48
regarding malloc and calloc(why should we use both,however b
Didn`t you get my earlier reply?

> From: deo sharma [mailto:deo31@...]
> Sent: Sunday, December 26, 2004 9:58 PM
> To: Programmers-Town@yahoogroups.com
> Subject: Re: (PT) counter point regarding malloc and calloc(why should
> we use both,however both allocating the memory only)
>
> the question still remains that why we need a malloc when calooc will
> don the same thing by inistialising the memory with zero rther than
> some random value.

Zeroing memory is sometime unnecessary and sometime undesirable.

Unnecessary because you will be setting the data in your code right after
allocation.

Undesirable because they could produce non-portable codes. For example, a
NULL pointer constant is not necessary all-bits set to zero.

HTH
Shyan
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help