public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: kunal chavan <kunal@deeproot.co.in>
To: Andre Kirchner <sieg1974@yahoo.com>,  gcc-help@gcc.gnu.org
Subject: Re: Newbie questions about variables
Date: Sat, 27 Sep 2003 01:26:00 -0000	[thread overview]
Message-ID: <3F74E80C.4010901@deeproot.co.in> (raw)
In-Reply-To: <20030926195342.93263.qmail@web41411.mail.yahoo.com>

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
>



      parent reply	other threads:[~2003-09-27  1:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-26 19:53 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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3F74E80C.4010901@deeproot.co.in \
    --to=kunal@deeproot.co.in \
    --cc=gcc-help@gcc.gnu.org \
    --cc=sieg1974@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).