3

Is there any way of creating a static array in c++ at run time. What i want is really simple like just want to get input number from user and create a static array of size of input number at run time. No new operator is required no pointer are required just static array?

3
  • 1
    What does it mean to you that an array is static? Commented Jun 12, 2011 at 23:04
  • 1
    Presumably you mean "automatic storage duration", rather than "static storage duration"? Commented Jun 12, 2011 at 23:06
  • I think you are confusing the meaning of static (unfortunately the term is overloaded a lot). If you can describe what you mean be the term static it may help clarify the situation. Commented Jun 12, 2011 at 23:37

6 Answers 6

5

No. static variable is allocated before the program code is actually running (i.e.: before your main is called). What you need is a dynamic (aka created at run time) array. If you want to avoid new you can create it on stack (by passing parameter to a function that will create it and working on it within that function), but that's not the same as static. You can also use template containers that will do the allocation and resizing for you (like std::vector, mentioned in other answers)

edit

It seems to bother some people that I didn't mention the matter of initializing static objects. Although not directly relevant to the question - worth to know that static member variables or static variables within a scope can be initialized at run time, but the space for them is reserved prior to the main, so the size of the variable cannot be changed.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm not sure that this is the meaning of "static" the OP is talking about.
The storage is created before the program starts... when the constructor is run depends on whether it's a global, class static member, or local static.
@littleadv: Array construction includes running a constructor for each element.
@littleadv: Reading your answer, I would think that I couldn't pass arguments determined at run-time to the constructor of a static local variable, but that's not the case.
@littleadv: It's the first complete sentence: "static variable is created before the program code is actually running". "created" means the constructor runs. You probably are looking for "allocated" or "storage is reserved".
4

If you mean

unsigned size;
std::cin >> size;
int arr[size];

Then: No. C99 has a feature called Variable-Length-Arrays, but the C++03 (and '0x) standard have no notion of this kind of feature.

2 Comments

I think you mean std::cin ;)
In my answer I've described how alloca can be used to allocate memory on the stack in C++, this is equivalent to C99's variable length arrays.
3

Use alloca to allocate space on the stack, just like a static array or like a C99 variable length array.

#include <iostream>
#include <alloca.h>
int main() {
        unsigned sz;
        std :: cin >> sz;
        int * p = static_cast<int*>(alloca(sz * sizeof(int)));
        // do stuff with p, do not attempt to free() it
}

I've only ever used it with C, but it works well. Read about it first though. It probably isn't very portable.

Comments

1

No.

By definition, you need to dynamically allocate anything whose size is not known until runtime. That's why new and friends exist.

Comments

0

What does it mean to you that an array is static? What advantages do you think it gives you?

Actually, any static variable (including arrays) has its storage reserved at the beginning of the program. For this reason, its size has to be known before the program starts running, so it can't depend on user input in any way.

There are a number of ways to make dynamic non-static arrays, however, so let us know what you're trying to do.

2 Comments

It wasn't me, but I'm not convinced that this strictly answers the question. Isn't it more of a comment?
Not a answer. The question can be answered by "yes" or "no", not by "I don't know what you mean". If you want - you can put it as a comment. What I understand from your answer is that you don't know, so why bother?
0

It sounds like what you want is an array whose size is run-time defined but whose life time is defined by static storage duration (ie the length of the program).

You can not use the basic array built into the language. The size of these objects are defined at compile time and can not be modified at run-time. But there are a couple of alternatives.

The basic std::vector is probably what you want:

std::vector<int>    data;

int main()
{
    int size;
    std::cout << "Input the size of the array\n";
    std::cin >> size;

    // This line sets the size of the container
    // vector is basically a C++ wrapper around a dynamically sized array
    data.resize(size);

    // Now we can safely read values into the array (like) container.
    for(int loop =0;loop < size;++loop)
    {
        std::cout << "Input Value " << loop << "\n";
        std::cin >> data[loop];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.