Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #7187

Re: a little c programming that no works!!

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: a little c programming that no works!!
Date 2011-06-28 22:40 -0700
Organization None to speak of
Message-ID <lnpqlx8ab7.fsf@nuthaus.mib.org> (permalink)
References <e4f31412-ae7a-455f-9296-3cca0bbccedb@g16g2000yqg.googlegroups.com> <iuau65$5fc$1@news-1.m-online.net>

Show all headers | View raw


"Heinrich Wolf" <invalid@invalid.invalid> writes:
> "Vinicio Flores" <vfloreshdz@gmail.com> schrieb im Newsbeitrag 
> news:e4f31412-ae7a-455f-9296-3cca0bbccedb@g16g2000yqg.googlegroups.com...
> ...
>> static FILE *create_database(char *arg)
>> {
>>  FILE *newbase;
>>
>>  newbase = fopen(arg, "w+");
>>
>>  return newbase;
>> }
> ...
> newbase resides on the stack in the scope of create_database. After you 
> leave create_database, the memory space allocated for newbase is lost! You 
> return the pointer newbase to the caller of create_database, but after you 
> have left create_database, it points to freed memory. If the caller uses 
> this pointer, this leads to a segmentation fault!
>
> static FILE *newbase;
> will solve that problem.

Returning the value of a local object returns *a copy* of that value.
It's not necessary for the object to continue to exist after that.  It's
as true for pointer objects as for, say, int objects.

int func(void)
{
    int local = 42;
    return local;
}

Here, "local" no longer exists, but func1() has no problem returning the
value 42.

It's returning the *address* of a local variable that can cause problems.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

a little c programming that no works!! Vinicio Flores <vfloreshdz@gmail.com> - 2011-06-26 20:50 -0700
  Re: a little c programming that no works!! Ian Collins <ian-news@hotmail.com> - 2011-06-27 16:07 +1200
    Re: a little c programming that no works!! Dirk Zabel <zabel@riccius-sohn.eu> - 2011-06-27 11:37 +0200
      Re: a little c programming that no works!! Ian Collins <ian-news@hotmail.com> - 2011-06-27 22:44 +1200
  Re: a little c programming that no works!! "BartC" <bc@freeuk.com> - 2011-06-27 09:44 +0100
  Re: a little c programming that no works!! Barry Schwarz <schwarzb@dqel.com> - 2011-06-27 06:04 -0700
  Re: a little c programming that no works!! "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-27 23:54 +0200
    Re: a little c programming that no works!! Lew Pitcher <lpitcher@teksavvy.com> - 2011-06-27 18:11 -0400
    Re: a little c programming that no works!! Barry Schwarz <schwarzb@dqel.com> - 2011-06-27 15:44 -0700
    Re: a little c programming that no works!! Keith Thompson <kst-u@mib.org> - 2011-06-28 22:40 -0700
    Re: a little c programming that no works!! "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-29 15:44 +0200
      Re: a little c programming that no works!! Keith Thompson <kst-u@mib.org> - 2011-06-29 10:06 -0700

csiph-web