Dynamically allocating variables is a no-go since you're low on memory. As a possible solution, you could set each parameter of each function to a reference. References disallow copies of the passed argument. Instead, they refer to the address of the argument that was assigned to it. By eliminating the copy procedure, you effectively reduce the stack size, thus, reducing the system memory usage. This can be done with pointers also, but you have to explicitly pass the address of the argument by using the
address-of operator. You see, when a parameter is declared in the same way as
int argc, it creates copies of the argument it was given. For example, consider this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void Function( int Param )
{
Param = 100;
}
int main( )
{
int A( 10 );
std::cout << "Before: " << A << std::endl;
Function( A );
std::cout << "After: " << A << std::endl;
return 0;
}
|
This code will not modify the content of
A since
A was copied during the invocation of
Function( ). When the function was running,
Param held the copy of
A. When
Param was assigned to the value 100,
A's clone received the value of 100.
int usually reserves 4 bytes of memory. When a copied is made, an additional 4 bytes are reserved on the stack for the copy. If we used references, we avoid the copy procedure altogether. This example demonstrates this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void Function( int &Param )
{
Param = 100;
}
int main( )
{
int A( 10 );
std::cout << "Before: " << A << std::endl;
Function( A );
std::cout << "After: " << A << std::endl;
return 0;
}
|
This code is identical to the previous code, except for one minor detail: the reference parameter. When
A was passed to
Function( ), A was not copied, which means 4 byte less (copy).
The latter won't reduce the memory by a great deal, but it's definitely an improvement.
Wazzak