问题
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.
回答1:
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()
.
回答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, ... );
}
回答3:
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.
回答4:
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.
回答5:
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;
}
}
回答6:
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()
{
/*...*/
}
来源:https://stackoverflow.com/questions/13018413/declaring-variable-sized-array-without-using-dynamic-memory-allocation