This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Newbie questions about variables


Hi Andre

If you allocate memmory to theString (should be pointer to string not array of pointer's in your case)
in your theFunction from heap (using malloc) then certainly it will eat up your available heap
if you do not free it at each subsequent call and the results may be unpredictable.
On other hand if you declare string like "char theString[256] "(static memmory allocation) in
your funtion or allocate memmory using alloca " char *theString = alloca(StringLen) "
memmory will be allocated in functions stack and the stack will be flushed as soon as
control returns to main function.


Kunal.

Andre Kirchner wrote:

Hi,

what would happen if I had a function which creates a
string, and a for loop in the main function that
executes this function 100 times, will it create 100
theString variables which will just be using memory
space, or each time my program exists
theFunction theString variable will be erased from the
computer's memory?
Is there any command to destroy a variable, and free
the memory space it was using if I need?

Thanks

Andre

void theFunction( const char * newString )
{
 char * theString[ 256 ];


strcpy( theString, newString ); <---- This is syntax error




printf( "%s\n", theString ); }

void main()
{
 int counter;
 char theLine[ 256 ];

 for( counter = 0; counter< 100; counter++ )
 {
   sprintf( theLine, "%03d\n", counter );
   theFunction( theLine );
 }
}

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]