Creating an array in runtime

Posted on 2004-11-28
Last Modified: 2013-11-20
Dear Experts,
If this is possible (creating an unspecified array and size during runtime), including the memory allocation part, can you provide me a code example please?
I'm using C++. Thank you very much.

Question by:sunshine8888
    LVL 55

    Accepted Solution

    Sure you can.
    In standard C++ you can use the vector template:

    #include <vector>
    vector <yourdatatype> array;

    where yourdatatype could be a fundamental type like 'float' or a user-define class type.

    In Visual C++, you can use CArray:

    #include <afxtempl.h>
    CArray <yourdatatype, yourdatatype> array;

    you can use both as a standard C array, by example:

    float a = array[0];

    But you have more functionality than standard C arrays, including the possibility to grow array at runtime.
    LVL 2

    Expert Comment

    If array size is unknown at compile time, but known and fixed at runtime you can allocate an array like this:

    int* pMyIntegerArray = new int[nSize];

    where nSize is initialized at runtime to some fixed value. Don't forget to

    delete pMyIntegerArray;

    to make everyone happy... :-)
    LVL 30

    Expert Comment

    I recommend using vector<> or CArray type instead of using new[], since it will automatically clean up the memory.

    To add to jaime_olivares's comment, if you use vector<> type, you can either initialize the vector with zero length, or initialize it with a specific size at runtime.

    vector <yourdatatype> array(MySizeOfArray);

    If you initialize it with no size value, then you can use the push_back function to make it grow as you need it.

    vector <yourdatatype> array;

    Author Comment

    Thank you for all the answers. I really appreciate it.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now