initializing values of a pointer without using calloc

Discussion in 'C Programming' started by ssylee, Feb 4, 2009.

  1. ssylee

    ssylee Guest

    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?
     
    ssylee, Feb 4, 2009
    #1
    1. Advertising

  2. ssylee

    Ian Collins Guest

    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
     
    Ian Collins, Feb 4, 2009
    #2
    1. Advertising

  3. ssylee

    ssylee Guest

    On Feb 4, 2:17 pm, Ian Collins <> 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.
     
    ssylee, Feb 4, 2009
    #3
  4. ssylee

    Default User Guest

    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
     
    Default User, Feb 4, 2009
    #4
  5. ssylee <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 4, 2009
    #5
  6. Keith Thompson <> writes:
    > ssylee <> 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) <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"
     
    Keith Thompson, Feb 4, 2009
    #6
  7. ssylee <> wrote:
    > Ian Collins <> 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 = 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;

    > > 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
     
    Peter Nilsson, Feb 4, 2009
    #7
  8. ssylee

    Ben Pfaff Guest

    ssylee <> 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
     
    Ben Pfaff, Feb 4, 2009
    #8
  9. ssylee

    Kaz Kylheku Guest

    On 2009-02-04, Ben Pfaff <> wrote:
    > ssylee <> 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. :)
     
    Kaz Kylheku, Feb 4, 2009
    #9
  10. ssylee

    Ben Pfaff Guest

    Kaz Kylheku <> writes:

    > On 2009-02-04, Ben Pfaff <> wrote:
    >> ssylee <> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
     
    Ben Pfaff, Feb 5, 2009
    #10
  11. ssylee

    Kaz Kylheku Guest

    On 2009-02-05, Ben Pfaff <> wrote:
    > Kaz Kylheku <> writes:
    >
    >> On 2009-02-04, Ben Pfaff <> wrote:
    >>> ssylee <> 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.


    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.
     
    Kaz Kylheku, Feb 5, 2009
    #11
  12. Kaz Kylheku wrote:
    > On 2009-02-05, Ben Pfaff <> wrote:
    >> Kaz Kylheku <> writes:
    >>
    >>> On 2009-02-04, Ben Pfaff <> wrote:
    >>>> ssylee <> 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.

    >
    > 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.
     
    J. J. Farrell, Feb 5, 2009
    #12
  13. ssylee

    Richard Bos Guest

    Peter Nilsson <> wrote:

    > ssylee <> wrote:
    > > Ian Collins <> wrote:
    > > > ssylee wrote:


    > > > > allocated the memory using malloc, but there is no
    > > > > calloc available. How would I set the entries to 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.)


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

    Richard
     
    Richard Bos, Feb 9, 2009
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler

    initializing references / null pointer

    Alexander Stippler, Oct 18, 2003, in forum: C++
    Replies:
    1
    Views:
    367
    Rob Williscroft
    Oct 18, 2003
  2. MK
    Replies:
    27
    Views:
    2,721
    Sidney Cadot
    Feb 13, 2004
  3. arun

    initializing array using pointer

    arun, Dec 13, 2005, in forum: C Programming
    Replies:
    31
    Views:
    1,128
    Richard Bos
    Dec 15, 2005
  4. Alexander Behnke

    NULL pointer returned in calloc

    Alexander Behnke, Dec 4, 2006, in forum: C++
    Replies:
    3
    Views:
    1,489
    Alexander Behnke
    Dec 4, 2006
  5. mthread
    Replies:
    7
    Views:
    430
    James Kanze
    Dec 12, 2007
Loading...

Share This Page