initializing values of a pointer without using calloc

S

ssylee

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?
 
I

Ian Collins

ssylee said:
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.
 
D

Default User

ssylee said:
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
 
K

Keith Thompson

ssylee said:
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?
 
K

Keith Thompson

Keith Thompson said:
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?".
 
P

Peter Nilsson

Where is the harm in that?

for (i = 0; i < N; i++) a = 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 = zero;
Thanks Ian. Totally forgot about memset.

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

Ben Pfaff

ssylee said:
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;
}
 
B

Ben Pfaff

Kaz Kylheku said:
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.
 
K

Kaz Kylheku

Then I get into trouble when I recompile on a system that does
have calloc.

Of course, you wrap that with

#ifdef MISSING_CALLOC

#endif

Calling the function my_calloc just lets you get away with using it
anyway when the real thing is available.

And in fact anything wrapped in #ifdef MISSING ... could be put into
a separate source file missing.c which you wouldn't even compile and
link with your program on a platform where nothing is missing.
 
J

J. J. Farrell

Kaz said:
Of course, you wrap that with

#ifdef MISSING_CALLOC

#endif

Calling the function my_calloc just lets you get away with using it
anyway when the real thing is available.

And in fact anything wrapped in #ifdef MISSING ... could be put into
a separate source file missing.c which you wouldn't even compile and
link with your program on a platform where nothing is missing.

Once the program's been developed and debugged using my_calloc(),
there's an argument for carrying on using it even when moving to an
environment where calloc() is available. If, either by accident or
forgotten intent, my_calloc() doesn't conform exactly to the Standard
calloc() definition, then switching to calloc() might introduce
problems. This is unlikely for something as simple as calloc(), but
might apply for the more complex library routines.
 
R

Richard Bos

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

No, but it _will_ set them to the same values that calloc() would.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top