4

I want to allocate variable sized 2D array in a function without using new operator such that this 2D array is available to other functions in the same file.

    void draw(int i)
    {   size=i;   }

    void assign(char symbol)
    {
        char one[size][size];
        /// ... Assigning values to one  ...
    }
    void display()
    {   /// Displaying values of one[size][size]
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            cout<<one[i][j];
            cout<<endl;
        }
    }

Execution order of functions is draw -> assign -> display

This question may have been asked earlier. But my problem is.. -> I can't declare the array outside assign function globally because value of size is not known. -> I can't use "one" array in "display" function because its scope is limited to "assign" function.

And I also don't want to use either new or malloc operators. Please help if there is any alternative available.

3
  • 2
    std::vector sounds perfect. Commented Oct 22, 2012 at 19:29
  • Do you care if new or malloc get called behind the scenes? Commented Oct 22, 2012 at 19:33
  • std::vector is ideal for your purpose. You can create a zero sized array and then change its size with the resize(int size) method. The STL provides other methods to manage the array/vector. Commented Oct 22, 2012 at 19:53

6 Answers 6

4

C++ doesn't support stack-allocated Variable Length Arrays like for example C99. You should use std::vector<T> instead. If you really want to use stack allocation you can use alloca().

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

4 Comments

I think you mean alloca instead of calloc.
@ThomasMatthews I meant alloca()
By default, std::vector<T> uses dynamic memory. @Jekin would have to create a std::vector with allocators that called alloca().
@Thomas Matthews2 : I'm curious: how could an allocator usable with std::vector could be created? alloca allocates on the caller's stack frame, which would be allocator::allocate here, and diffferent from the one the std::vector was declared.
2

There is no way to do this. The array "one" is a temporary on the stack in the assign function. It gets destroyed whenever you leave the assign function.

So you need to allocate memory somehow.

If you only ever use one copy of the "one" array at a time, you could declare it in global scope with space large enough for a comfortable upper bound. And check each time that you use it that the static global array is large enough.

Is your concern performance? Or are you on a platform where memory allocation is not possible?

Some other options: statically allocate the array outside the system, and pass it in as an argument to each function.

Or if all the functions can be called from a single higher level "owner" function, you could allocate the array dynamically on the stack with "alloca".

E.g.

system()
{
   char one = alloca( size );
   draw( one, ... );
   assign( one, ...);
   display( one, ... );
}

Comments

1

If you don't want to use dynamic memory allocation then you must pay the cost of assigning some extra memory than what is required. You define the size of array to be large enough to handle the data at hand. like,

int arr[my_limit];

note that my_limit must be a constant expression.

In the program mentioned in the question, memory needs for the array 'one' is determined only at the runtime and hence it is a good practice to use dynamic memory allocation.

Comments

0

I am not perfect at c++, actually I am a newbie like you. However I think you can do it by using

"*pointer"

I mean you should show your array's referance. Then you can use it outside the function.

Comments

0
const int MAX_SIZE = 1000;
char one[MAX_SIZE][MAX_SIZE];
int size = 0; // needs to be smaller than MAX_SIZE

void draw(int i) {
    if(i < MAX_SIZE) size=i; 
}

void assign(char symbol, i, j)
{
    char one[i][j] = symbol;
}

void display()
{
    for(int i=0; i<size; i++) {
        for(int j=0; i<size; j++) {
            cout<<one[i][j];
        }
        cout<<endl;
    }
}

1 Comment

this won't work because the "one" array is allocated locally in assign. You're not using the same "one" array everywhere. You have to pass it in everywhere.
0

Have you tried defining the array outside of the functions, but within the file?
For example:

static unsigned int my_array[16][32];
void my_func()
{
    /*...*/
}

int another_func()
{
  /*...*/
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.