Thread: Initialize elements to zero

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Initialize elements to zero

    Hello, is there a way to initialize elements to 0 on dynamic allocation?

    int *a = new int(0);
    doesn't work..I'm not exactly sure what that does..initialize what to 0?
    It looks like a member initializer to me..
    this is what I tried..
    Code:
         int *a = new int(0);
         a = new int[30] ;

    but didn't work..It is C++ I am sure there is something, I don't want to end up using calloc

    and I don't want to use a loop. :O
    You ended that sentence with a preposition...Bastard!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    new int(0) allocates one int and initializes it to 0.
    Why don't you use std::vector? It will initialize all elements to 0.
    Or you could use push_back to push elements into the vector as needed.
    There is also std::fill.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    heck yeah..would use that...why not..
    ty E.
    You ended that sentence with a preposition...Bastard!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Just for the record, new[] allows zero-initialization like this:

    Code:
    int* a = new int[30]();
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    omg! I was so close!!
    i tried that earlier except I put new int[30](0)
    ah well...noted anon
    You ended that sentence with a preposition...Bastard!

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You are better served just learning to use `std::fill' as a matter of course because you only ever get default constructed values and not every compiler does this for native types.

    *grumble*

    Why didn't the committee just standardize constructing arrays in the first place?

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array elements get overridden?
    By ThatDudeMan in forum C Programming
    Replies: 5
    Last Post: 11-22-2010, 03:56 PM
  2. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  3. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  4. how to initialize a char matrix with empty elements?
    By kobra_swe in forum C Programming
    Replies: 3
    Last Post: 08-22-2006, 05:28 AM
  5. using constructor to initialize data
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2002, 08:55 PM