public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Newbie questions about variables
@ 2003-09-26 19:53 Andre Kirchner
  2003-09-26 20:06 ` Eljay Love-Jensen
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Andre Kirchner @ 2003-09-26 19:53 UTC (permalink / raw)
  To: gcc-help

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 );
  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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Newbie questions about variables
  2003-09-26 19:53 Newbie questions about variables Andre Kirchner
@ 2003-09-26 20:06 ` Eljay Love-Jensen
  2003-09-26 20:33 ` Fabiano Ramos
  2003-09-27  1:26 ` kunal chavan
  2 siblings, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2003-09-26 20:06 UTC (permalink / raw)
  To: Andre Kirchner, gcc-help

Hi Andre,

You cannot strcpy into an array of 256 pointers.  Did you mean
  char theString[256];
?

theLine variable is being overwritten each time.

The memory occupied by theString variable is not erased from the computer's memory -- it is returned to the OS's free store.  If you are worried about the data stored in that variable, especially "post mortem" of the program's execution, you may want to take measures to overwrite the variable with garbage.

Assuming such a level of paranoia is warranted.

To allocate an object from the heap in C, use malloc.  And use free to relinquish it back to the heap.

To allocate an object from the free storein C++, use new. And use delete to relinquish it back to the free store.

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Newbie questions about variables
  2003-09-26 19:53 Newbie questions about variables Andre Kirchner
  2003-09-26 20:06 ` Eljay Love-Jensen
@ 2003-09-26 20:33 ` Fabiano Ramos
  2003-09-27  1:26 ` kunal chavan
  2 siblings, 0 replies; 4+ messages in thread
From: Fabiano Ramos @ 2003-09-26 20:33 UTC (permalink / raw)
  To: gcc-help

How are you creating the variable at the loop? You are doing just a

for ( ...) {
  int a;

}

or like

for (....) {
  int a = malloc(...);

}

Fabiano


On Fri, 2003-09-26 at 16:53, 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 );
>   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
-- 
Fabiano Ramos <fabramos@bol.com.br>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Newbie questions about variables
  2003-09-26 19:53 Newbie questions about variables Andre Kirchner
  2003-09-26 20:06 ` Eljay Love-Jensen
  2003-09-26 20:33 ` Fabiano Ramos
@ 2003-09-27  1:26 ` kunal chavan
  2 siblings, 0 replies; 4+ messages in thread
From: kunal chavan @ 2003-09-27  1:26 UTC (permalink / raw)
  To: Andre Kirchner, gcc-help

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
>



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-09-27  1:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-26 19:53 Newbie questions about variables Andre Kirchner
2003-09-26 20:06 ` Eljay Love-Jensen
2003-09-26 20:33 ` Fabiano Ramos
2003-09-27  1:26 ` kunal chavan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).