new and memset

Discussion in 'C++' started by divya_rathore_@gmail.com, Dec 25, 2005.

  1. Guest

    Consider the dynamic allocation of a 2D array:

    int **temp;
    temp = new int*[height];
    for (y=0; y<height; y++) temp[y] = new int[width];

    I have 2 Questions:

    [a] Does new initializes memory to 0?

    I want to initialize this to some value not necessarily 0. Is it
    ok to use 'memset' to this dynamically allocated array?

    If yes, what would be the syntax? Would it be:

    memset(temp, 0, width*height*sizeof(int));


    thanks in advance,
    D. Rathore
    (remove underscores for email ID)
    , Dec 25, 2005
    #1
    1. Advertising

  2. Luke Meyers Guest

    [a] No, thankfully. That would add a O(N) overhead cost, needlessly,
    to every memory allocation.

    Sure. memset works with any pointer, it doesn't matter if it's
    pointing to the stack or to the heap. Your syntax looks fine for
    setting it to 0. Remember that the second parameter is converted to an
    unsigned char.
    Luke Meyers, Dec 25, 2005
    #2
    1. Advertising

  3. Guest

    Luke Meyers wrote:
    > Sure. ...
    > Remember that the second parameter is converted to an
    > unsigned char.


    What shall be done to convert it to int? Or float, if the 2D array is
    of floats?

    thanks in advance,
    D. Rathore
    (remove underscores for email ID)
    , Dec 25, 2005
    #3
  4. wrote:
    > Consider the dynamic allocation of a 2D array:
    >
    > int **temp;
    > temp = new int*[height];
    > for (y=0; y<height; y++) temp[y] = new int[width];
    >
    > I have 2 Questions:
    >
    > [a] Does new initializes memory to 0?
    >
    > I want to initialize this to some value not necessarily 0. Is it
    > ok to use 'memset' to this dynamically allocated array?
    >
    > If yes, what would be the syntax? Would it be:
    >
    > memset(temp, 0, width*height*sizeof(int));
    >
    >
    > thanks in advance,
    > D. Rathore
    > (remove underscores for email ID)


    ever consider programming safely?

    > temp = new int*[height];
    > for (y=0; y<height; y++) temp[y] = new int[width


    does you environment incur prohibitive cost using vector<vector<int> >
    ?
    puzzlecracker, Dec 25, 2005
    #4
  5. Luke Meyers Guest

    > What shall be done to convert it to int? Or float, if the 2D array is
    > of floats?


    It's not converted to an int, it's converted to an unsigned char, which
    is the usual way to refer to an individual byte value. memset only
    operates on bytes, it will not initialize an array of floats that
    represent zero value for you. It will set all bytes in the specified
    range to the exact byte value attained by implicitly converting the
    supplied int to an unsigned char.

    I really question what benefit you expect to achieve from this.
    Relying on default initializations to null values is generally
    indicative of a problematic design.

    Luke
    Luke Meyers, Dec 26, 2005
    #5
  6. wrote:
    > Consider the dynamic allocation of a 2D array:
    >
    > int **temp;
    > temp = new int*[height];
    > for (y=0; y<height; y++) temp[y] = new int[width];
    >
    > I have 2 Questions:
    >
    > [a] Does new initializes memory to 0?


    No, but

    new int[width]();

    does.

    > I want to initialize this to some value not necessarily 0. Is it
    > ok to use 'memset' to this dynamically allocated array?


    No. memset() operates on bytes. If sizeof(int) is more than a byte,
    you'll get weird results. Usually, memset() is used to zero-initialize
    an array or a structure, not to assign an arbitrary value. What's more,
    some types (such as floats or non-pods) cannot be memset'ed.

    > If yes, what would be the syntax? Would it be:
    >
    > memset(temp, 0, width*height*sizeof(int));


    That would work.

    If you want to put another value than 0, or if your types are not
    integrals, do not use memset. Either use a loop or another container,
    such as standard containers.


    Jonathan
    Jonathan Mcdougall, Dec 26, 2005
    #6
  7. Luke Meyers wrote:
    > > What shall be done to convert it to int? Or float, if the 2D array is
    > > of floats?

    >
    > It's not converted to an int, it's converted to an unsigned char, which
    > is the usual way to refer to an individual byte value. memset only
    > operates on bytes, it will not initialize an array of floats that
    > represent zero value for you. It will set all bytes in the specified
    > range to the exact byte value attained by implicitly converting the
    > supplied int to an unsigned char.
    >
    > I really question what benefit you expect to achieve from this.
    > Relying on default initializations to null values is generally
    > indicative of a problematic design.


    Is it?


    Jonathan
    Jonathan Mcdougall, Dec 26, 2005
    #7
  8. Guest

    Thank you Jonathan and others for the suggestions and the constructive
    criticism.

    regards,
    - D. Rathore

    --------------------------------
    Jonathan Mcdougall wrote:

    > > If yes, what would be the syntax? Would it be:
    > >
    > > memset(temp, 0, width*height*sizeof(int));

    >
    > That would work.


    > Jonathan
    , Dec 26, 2005
    #8
  9. Jim Langston Guest

    "" <> wrote in message
    news:...
    > Consider the dynamic allocation of a 2D array:
    >
    > int **temp;
    > temp = new int*[height];
    > for (y=0; y<height; y++) temp[y] = new int[width];
    >
    > I have 2 Questions:
    >
    > [a] Does new initializes memory to 0?
    >
    > I want to initialize this to some value not necessarily 0. Is it
    > ok to use 'memset' to this dynamically allocated array?
    >
    > If yes, what would be the syntax? Would it be:
    >
    > memset(temp, 0, width*height*sizeof(int));


    You are not guaranteed that the different blocks of memory allocated will be
    contiguous.

    temp[0] and temp[1] may not be width bytes apart. You'll be lucky if they
    are, but there is no guarantee.

    Some of the reasons they wouldn't be next to each other include, but are not
    limited to:
    1. Previously you allocated a memory, and some of it was freed that is big
    enough for width bytes, so your first new grabs that memory, but there is
    allocated memory in the way for the second new, so it grabs it at another
    point in memory.
    2. Your first block of memory grabed with new is toward the end of a memory
    page. The next allocation won't fit in one page so the OS decides to grab
    that memory at the start of a new page.
    3. Any other reason your OS decides to not grab the memory right after each
    other.

    It may work for you 99 times out of 100. But that 100th time you're gonna
    get weird errors and not be able to track them down. Don't treat your temp
    array as a contiguous block of memory unless you actually allocate it that
    way.

    Better is:
    .... = new int[width]();

    or

    for ( y = 0; y < height; y++ )
    memset( temp[y], 0, width*sizeof(int) );


    >
    >
    > thanks in advance,
    > D. Rathore
    > (remove underscores for email ID)
    >
    Jim Langston, Dec 26, 2005
    #9
  10. Guest

    Jim Langston wrote:
    > Better is:
    > ... = new int[width]();
    >
    > or
    >
    > for ( y = 0; y < height; y++ )
    > memset( temp[y], 0, width*sizeof(int) );



    Way to go, Jim! That's what I was looking for :)
    , Dec 26, 2005
    #10
    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. k-man
    Replies:
    4
    Views:
    4,499
    Shelley Hebert
    Dec 18, 2003
  2. Joe C
    Replies:
    5
    Views:
    8,821
    tom_usenet
    Aug 24, 2004
  3. copx

    memset() and portability

    copx, Jul 24, 2003, in forum: C Programming
    Replies:
    10
    Views:
    645
    Eric Sosman
    Jul 28, 2003
  4. Jonathan Bartlett
    Replies:
    7
    Views:
    438
    Peter Nilsson
    Jul 8, 2005
  5. Shhnwz.a

    Doubt in memcpy() and memset()

    Shhnwz.a, Dec 6, 2006, in forum: C Programming
    Replies:
    12
    Views:
    704
    Kenneth Brody
    Dec 8, 2006
Loading...

Share This Page