Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > initializing values of a pointer without using calloc

Reply
Thread Tools

initializing values of a pointer without using calloc

 
 
ssylee
Guest
Posts: n/a
 
      02-04-2009
I'm trying to set all the entries of a dynamically allocated memory
block for a variable to zero. I have allocated the memory using
malloc, but there is no calloc available. How would I set the entries
to zero? Would I have to do it manually using a for loop, setting each
individual entry to zero?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-04-2009
ssylee wrote:
> I'm trying to set all the entries of a dynamically allocated memory
> block for a variable to zero. I have allocated the memory using
> malloc, but there is no calloc available. How would I set the entries
> to zero? Would I have to do it manually using a for loop, setting each
> individual entry to zero?


See memset.

--
Ian Collins
 
Reply With Quote
 
 
 
 
ssylee
Guest
Posts: n/a
 
      02-04-2009
On Feb 4, 2:17*pm, Ian Collins <(E-Mail Removed)> wrote:
> ssylee wrote:
> > I'm trying to set all the entries of a dynamically allocated memory
> > block for a variable to zero. I have allocated the memory using
> > malloc, but there is no calloc available. How would I set the entries
> > to zero? Would I have to do it manually using a for loop, setting each
> > individual entry to zero?

>
> See memset.
>
> --
> Ian Collins


Thanks Ian. Totally forgot about memset.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-04-2009
ssylee wrote:

> I'm trying to set all the entries of a dynamically allocated memory
> block for a variable to zero. I have allocated the memory using
> malloc, but there is no calloc available.


Why is calloc() not available?




Brian

--
Day 1 of the "no grouchy usenet posts" project
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-04-2009
ssylee <(E-Mail Removed)> writes:
> I'm trying to set all the entries of a dynamically allocated memory
> block for a variable to zero. I have allocated the memory using
> malloc, but there is no calloc available. How would I set the entries
> to zero? Would I have to do it manually using a for loop, setting each
> individual entry to zero?


Why its here no calloc available? It's a standard function. Are you
using a freestanding implementation?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-04-2009
Keith Thompson <(E-Mail Removed)> writes:
> ssylee <(E-Mail Removed)> writes:
>> I'm trying to set all the entries of a dynamically allocated memory
>> block for a variable to zero. I have allocated the memory using
>> malloc, but there is no calloc available. How would I set the entries
>> to zero? Would I have to do it manually using a for loop, setting each
>> individual entry to zero?

>
> Why its here no calloc available? It's a standard function. Are you
> using a freestanding implementation?


Make that "Why is there no calloc available?".

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      02-04-2009
ssylee <(E-Mail Removed)> wrote:
> Ian Collins <(E-Mail Removed)> wrote:
> > ssylee wrote:
> > > I'm trying to set all the entries of a dynamically
> > > allocated memory block for a variable to zero. I have
> > > allocated the memory using malloc, but there is no
> > > calloc available. How would I set the entries to zero?
> > > Would I have to do it manually using a for loop,
> > > setting each individual entry to zero?


Where is the harm in that?

for (i = 0; i < N; i++) a[i] = 0;

Even if you have a structure to zero initialise, it's just...

static const struct X zero = { 0 };
for (i = 0; i < N; i++) a[i] = zero;

> > See memset.

>
> Thanks Ian. Totally forgot about memset.


Note that memset() won't necessarily set pointers to null
pointers (or floating point variables to 0.)

--
Peter
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-04-2009
ssylee <(E-Mail Removed)> writes:

> I'm trying to set all the entries of a dynamically allocated memory
> block for a variable to zero. I have allocated the memory using
> malloc, but there is no calloc available. How would I set the entries
> to zero? Would I have to do it manually using a for loop, setting each
> individual entry to zero?


void *my_calloc(size_t n)
{
void *p = malloc(n);
if (p)
memset(p, 0, n);
return p;
}
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      02-04-2009
On 2009-02-04, Ben Pfaff <(E-Mail Removed)> wrote:
> ssylee <(E-Mail Removed)> writes:
>
>> I'm trying to set all the entries of a dynamically allocated memory
>> block for a variable to zero. I have allocated the memory using
>> malloc, but there is no calloc available. How would I set the entries
>> to zero? Would I have to do it manually using a for loop, setting each
>> individual entry to zero?

>
> void *my_calloc(size_t n)


If calloc is really not available, you may feel free to call it calloc.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-05-2009
Kaz Kylheku <(E-Mail Removed)> writes:

> On 2009-02-04, Ben Pfaff <(E-Mail Removed)> wrote:
>> ssylee <(E-Mail Removed)> writes:
>>
>>> I'm trying to set all the entries of a dynamically allocated memory
>>> block for a variable to zero. I have allocated the memory using
>>> malloc, but there is no calloc available. How would I set the entries
>>> to zero? Would I have to do it manually using a for loop, setting each
>>> individual entry to zero?

>>
>> void *my_calloc(size_t n)

>
> If calloc is really not available, you may feel free to call it calloc.


Then I get into trouble when I recompile on a system that does
have calloc. It's better, in my opinion, to avoid reserved
identifiers even if they are not in use on a particular system,
to avoid surprises later.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference b/w using calloc and new mthread C++ 7 12-12-2007 12:34 PM
NULL pointer returned in calloc Alexander Behnke C++ 3 12-04-2006 04:28 PM
initializing array using pointer arun C Programming 31 12-15-2005 09:42 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Help with warning when using calloc/malloc MK C Programming 27 02-13-2004 11:31 PM



Advertisments