Initializing dynamic arrays

This is a discussion on Initializing dynamic arrays within the C++ Programming forums, part of the General Programming Boards category; How do you initialize an array which was created in run-time? With normal arrays, I learned that you can do ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Initializing dynamic arrays

    How do you initialize an array which was created in run-time?

    With normal arrays, I learned that you can do the following:

    Code:
    int i[10] = {0,};
    But that doesn't seem to work with arrays you create dynamically:

    Code:
    int* i;
    i = new int[j]; //j would be some value you got through user input
    i = {0,};
    Is the only way to initialize it passing it through a loop?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    A loop always works.

    The C function calloc (which dynamically allocates memory and sets it to zero) is an alternative in some cases. The problem with using calloc() is that it does NOT work with C++ class types that have constructors (the constructors will not be called). Similarly, memset() can be used to set values in an array, but it assumes it is initialising an array of chars.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    int* i;
    i = new int[j]; //j would be some value you got through user input
    There are some fundamental misunderstandings in evidence in that code. The operator new returns the address of where the array is located in memory. So, 'i' is assigned an address. If this statement worked:
    Code:
    i = {0,};
    'i' would be overwritten and no longer contain the address. The way you access the values that a pointer points to is by derefencing the pointer: *i.

    Lastly, what is that comma for: {0,}?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    The comma was from a piece of code I got off the Internet. It worked fine in code like this (it initialized every element):

    Code:
    int i[20] = {0,}
    So I used it without question, but it seems it works fine even without the comma, so go figure.

    And I am aware that the i is a pointer to an array of integers.

    Since I'm new to c/c++, I was just curious if there was a way to initialize the values of runtime arrays and reduce about two~three lines of code while you're at it (i.e. no pesky for loop).

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    In C++, you generally use vector for your dynamic array needs, and vectors initialize their values automatically to 0 (or with the default constructor). You can also initialize them to a value other than zero.
    Code:
    std::vector<int> i(j); // j elements set to 0

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thank you for telling me that vectors also initialize values.

    I did not know that, whereas I am aware that vectors exist (I picked it up while mindlessly surfing the net).

    However, I didn't really feel comfortable using them since I'm new to c++ programming and I have no understanding of the internal structure of vector templates (they are templates, right?).

    While I am quite confident that the designers of STL are far, far more proficient in c++ than I am, and that (in compiled form) vectors would probably be as fast as dynamically allocated arrays for basic types (are they?), I still feel I'm reaching a bit when I move on to STL when I'm still trying to figure out the most fundamental aspects of c/c++.

    So, question: when is it the right time to start learning STL (I'm kinda past the pointers to pointers stage, and I'm trying to figure out linked lists right now)?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    However, I didn't really feel comfortable using them since I'm new to c++ programming and I have no understanding of the internal structure of vector templates (they are templates, right?).
    The beginning C++ book "Accelerated C++" starts right into the STL and covers vectors before talking about arrays or pointers.

    So, question: when is it the right time to start learning STL (I'm kinda past the pointers to pointers stage, and I'm trying to figure out linked lists right now)?
    Now. You can still understand the mechanics(simple) without understanding how they work. My 1,000 page beginning C++ book doesn't talk about the STL until the last chapter. To gain some understanding of how they work, you need to learn about pointers. To really understand them, you have to study what "iterators" are, i.e. so called "smart" pointers, and to understand them you need to know about objects, classes, and overloading operators.
    Last edited by 7stud; 11-20-2005 at 05:01 PM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    memset works fine..

    Code:
    int *i = new int[100];
    memset(i, 0, 100); // first param is address of var, second is value, third is count

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    As grumpy already mentioned, memset assumes chars, so that will not initialize the whole array, just the first 100 bytes. If int is 4 bytes on your platform, there will be 75 uninitialized values in that array.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    oh sorry about that, i rushed through it:

    Code:
    int *i = new int[100];
    memset(i, 0, sizeof(int) * 100); // first param is address of var, second is value, third is count

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 12:32 PM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 08:34 PM
  5. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21