Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: a little c programming that no works!! |
| Date | 2011-06-29 10:06 -0700 |
| Organization | None to speak of |
| Message-ID | <lnhb788t44.fsf@nuthaus.mib.org> (permalink) |
| References | <e4f31412-ae7a-455f-9296-3cca0bbccedb@g16g2000yqg.googlegroups.com> <iuau65$5fc$1@news-1.m-online.net> <iufa7v$o6s$1@news-1.m-online.net> |
"Heinrich Wolf" <invalid@invalid.invalid> writes:
> "Heinrich Wolf" <invalid@invalid.invalid> schrieb im Newsbeitrag
> news:iuau65$5fc$1@news-1.m-online.net...
>>
>> ...
>> 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.
>
> I'm sorry!
>
> I mixed it up with someting like this:
>
> char *SomeFunction(void)
> {
> char Buffer[10];
> strcpy(Buffer, "Hello");
> return Buffer;
> }
>
> Here you need
> static char Buffer[10];
Or one of several other possibilities, such as having the caller pass
in a pointer to an array that will hold the result, or allocating
the result with malloc() and making the caller responsible for
free()ing it.
C's special-case semantics here lay a trap for the unwary. The
statement "return Buffer;" *looks* like it's innocuously returning
the value of a local object, but the implicit array-to-pointer
conversion means it's really returning the address of a local objec.
--
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 | Find similar | Unroll 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