delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/08/18:16:30

From: horst DOT kraemer AT snafu DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Allocating vars in middle of function (possible DJGPP bug)
Date: Wed, 08 Jul 1998 22:02:46 GMT
Organization: Unlimited Surprise Systems, Berlin
Lines: 46
Message-ID: <35a3e699.30301061@news.snafu.de>
References: <35A23721 DOT FAB AT interlog DOT com>
NNTP-Posting-Host: n32-192.berlin.snafu.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Tue, 07 Jul 1998 07:56:33 -0700, Jedediah Smith
<jedediah AT interlog DOT com> wrote:

>In the middle of a function I had something like this:

>MyFunction()
>{
>   ...

>   {
>      char myString[20];

>      // myString is used only in here

>   }

>   ...
>}

>My intention was to have myString allocated at the beginning of the code
>block and deallocated at the end. Clearly myString was not getting
>allocated at all because I was getting protection faults in the code
>that used it. When I moved the myString declaration to the beginning of
>the function, it worked fine. Should myString have been allocated in the
>above example, or do I have my C++ syntax wrong?

Allocating/deallocating space and coming into /going out of scope are
not the same thing. The first is technical implementation detail and
the second is a language definition. 

Your definition is correct in C and in C++.

If you enter your subblock, the variable myString is in scope, but
most compilers - including gcc - will usually already allocate it on
the stack upon entry to the function and deallocate it when leaving
the function.

Not doing so would create awkward technical problems if you would
enter/leave a block via goto.

Please post a minimal example which supports your point.


Regards
Horst

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019