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.

Jekin
  • 79
  • 1
  • 8

6 Answers6

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().

mauve
  • 1,976
  • 12
  • 18
  • I think you mean `alloca` instead of `calloc`. – Pubby Oct 22 '12 at 19:40
  • @ThomasMatthews I meant `alloca()` – mauve Oct 22 '12 at 19:42
  • By default, `std::vector` uses dynamic memory. @Jekin would have to create a `std::vector` with allocators that called `alloca()`. – Thomas Matthews Oct 22 '12 at 19:48
  • @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. – Éric Malenfant Oct 22 '12 at 22:22
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, ... );
}
Rafael Baptista
  • 11,181
  • 5
  • 39
  • 59
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.

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.

ferit enişer
  • 41
  • 1
  • 8
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;
    }
}
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
  • 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. – Rafael Baptista Oct 22 '12 at 19:40
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()
{
  /*...*/
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154