Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer usage in c

Reply
Thread Tools

pointer usage in c

 
 
rskeples@gmail.com
Guest
Posts: n/a
 
      05-11-2005
I write a small program.

char *foo(void);
char *a = "I like C";

int main(void) {
if((strcmp(a,foo())) {
printf("\n i like c");
}
}

char *foo(void)
{
char b[100] = "I like C";
return b;
}

My teacher tells me that this program not work since the return value
from function foo() is not guaranteed to be preserved. I think this is
correct usage of pointers.

I dont think so. I think my teacher wrong.

Please explain.
thanks you
Rick

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      05-11-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> char *foo(void);
> char *a = "I like C";
>
> int main(void) {
> if((strcmp(a,foo())) {
> printf("\n i like c");
> }
> }
>
> char *foo(void)
> {
> char b[100] = "I like C";
> return b;
> }
>
> My teacher tells me that this program not work since the return value
> from function foo() is not guaranteed to be preserved. I think this is
> correct usage of pointers.
>
> I dont think so. I think my teacher wrong.


Your teacher is(!) right. You're returning the address of an auto
("local") array from foo(). This array, like all auto objects, has
automatic duration, which means that once you return from foo(), it goes
out of existence, and any attempt to use it invokes undefined behaviour,
which means it may do anything from appearing to work correctly to crash
hard. Sure, it may _look_ like it still exists after the return, in this
simple test program. However, you cannot rely on this at all. In any
more realistic program you're likely to read garbage.

Richard
 
Reply With Quote
 
 
 
 
T.M. Sommers
Guest
Posts: n/a
 
      05-11-2005
(E-Mail Removed) wrote:
> I write a small program.


#include <string.h>

> char *foo(void);
> char *a = "I like C";
>
> int main(void) {
> if((strcmp(a,foo())) {
> printf("\n i like c");


You should in general terminate an output line with a newline in
order to guarantee that it appears.

> }
> }
>
> char *foo(void)
> {
> char b[100] = "I like C";
> return b;
> }
>
> My teacher tells me that this program not work since the return value
> from function foo() is not guaranteed to be preserved. I think this is
> correct usage of pointers.
>
> I dont think so. I think my teacher wrong.


Your teacher is correct. The array b is not guaranteed to
continue to exist after foo() returns. It might, for instance,
be in a location that is not accessible after the function
returns, or it might get overwritten. Make it static and it will
work.

--
Thomas M. Sommers -- (E-Mail Removed) -- AB2SB

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-11-2005
(E-Mail Removed) writes:
> I write a small program.
>
> char *foo(void);
> char *a = "I like C";
>
> int main(void) {
> if((strcmp(a,foo())) {
> printf("\n i like c");
> }
> }
>
> char *foo(void)
> {
> char b[100] = "I like C";
> return b;
> }
>
> My teacher tells me that this program not work since the return value
> from function foo() is not guaranteed to be preserved. I think this is
> correct usage of pointers.
>
> I dont think so. I think my teacher wrong.


As others have pointed out, your teacher is right.

Your array b is local to the function foo. As soon as foo returns,
the array ceases to exist, and any attempt to refer to it will invoke
undefined behavior.

You've probably been misled by the fact that the program *appears* to
work properly. That's the worst thing about undefined behavior; it's
not guaranteed to blow up. What probably happens in your program (for
a typical C implementation) is that the memory that the array occupied
is still there; it's just beyond the top of the stack. It *could* be
re-used for some other purpose. For example, an interrupt could be
triggered after foo() returns and before you use the value, clobbering
it. Or the system could mark the memory space as unavailable, causing
a trap when you try to access it. But the most likely thing is that
it's still right where you left it. Accessing it is still an error,
but the implementation isn't required to detect the error.

By returning from foo() you've told the system that you're finished
with the array. By attempting to use the array afterwards, you're
lying to the system. It's not obligated to show you any kind of
mercy. So don't do that.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      05-11-2005
On Wed, 11 May 2005 00:01:06 -0700, rskeples wrote:

> I write a small program.
>
> char *foo(void);
> char *a = "I like C";
>
> int main(void) {
> if((strcmp(a,foo())) {
> printf("\n i like c");
> }
> }
>
> char *foo(void)
> {
> char b[100] = "I like C";
> return b;
> }
>
> My teacher tells me that this program not work since the return value
> from function foo() is not guaranteed to be preserved. I think this is
> correct usage of pointers.


The problem is not with the return value directly, it is with what the
return value is pointing at. A pointer value is only valid as long as the
thing it points at still exists. The return value of foo() isn't b itself
or a copy of b, it is a pointer to (the first element of) b. When foo()
returns all of its automatic variables cease to exist and that includes b,
and so any pointer to b becomes invalid. In main() you are trying to use
the value of an invalid pointer which gives undefined behaviour.

> I dont think so. I think my teacher wrong.


Remember that a pointer and what it points at are separate things and you
have to consider both.

Lawrence
 
Reply With Quote
 
Jonathan Bartlett
Guest
Posts: n/a
 
      05-11-2005
> My teacher tells me that this program not work since the return value
> from function foo() is not guaranteed to be preserved. I think this is
> correct usage of pointers.


Your teacher is correct. The memory for "b" is garbage once the
function returns. In order to make it right, you need to allocate
memory from the heap, and copy the string into the heap.

Here is an untested function, but which should do you right:

char *foo(void)
{
char b[100] = "I like C"; /* this will go away when the function returns */
char *c = malloc((strlen(b) + 1) * sizeof(char)); /* malloc enough
memory to hold "I like C" -- this will NOT go away when the function
returns */
strcpy(c, b); /* copy the contents of b to c */

return c; /* return c */
}

Note that since you allocated the memory with malloc, when you are
finished with it you MUST deallocate it with free().

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      05-11-2005
Jonathan Bartlett:
....
> return c; /* return c */


Very enlightening comment.

Jirka
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-11-2005
(E-Mail Removed) wrote on 11/05/05 :
> char *foo(void);
> char *a = "I like C";
>
> int main(void) {
> if((strcmp(a,foo())) {
> printf("\n i like c");
> }
> }
>
> char *foo(void)
> {
> char b[100] = "I like C";
> return b;
> }
>
> My teacher tells me that this program not work since the return value
> from function foo() is not guaranteed to be preserved. I think this is
> correct usage of pointers.
>
> I dont think so. I think my teacher wrong.


Yes, so is my compiler...

main.c:13: warning: initialization discards qualifiers from pointer
target type main.c: In function `main_':
main.c:16: warning: implicit declaration of function `strcmp'
main.c:16: parse error before '{' token
main.c: In function `foo':
main.c:24: warning: function returns address of local variable

"Please engage your brain..." (c) Dan Pop...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-11-2005
(supersedes <(E-Mail Removed)>)

(E-Mail Removed) wrote on 11/05/05 :
> char *foo(void);
> char *a = "I like C";
>
> int main(void) {
> if((strcmp(a,foo())) {
> printf("\n i like c");
> }
> }
>
> char *foo(void)
> {
> char b[100] = "I like C";
> return b;
> }
>
> My teacher tells me that this program not work since the return value
> from function foo() is not guaranteed to be preserved. I think this is
> correct usage of pointers.
>
> I dont think so. I think my teacher wrong.


Yes, so is my compiler...

main.c:13: warning: initialization discards qualifiers from pointer
target type main.c: In function `main_':
main.c:16: warning: implicit declaration of function `strcmp'
main.c:16: parse error before '{' token
main.c: In function `foo':
main.c:24: warning: function returns address of local variable

"Please engage your brain..." (c) Dan Pop...

#include <string.h>
#include <stdio.h>

char const *foo (void)
{
char const *b = "I like C";
return b;
}

int main (void)
{
char const *a = "I like C";

if (strcmp (a, foo ()) == 0)
{
printf ("I like c\n");
}
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

 
Reply With Quote
 
Nitin Bhardwaj
Guest
Posts: n/a
 
      05-12-2005
Hi All,

I think the following code will also work:

char *foo( )
{
return "I like C";
}

since string-literals have static life-time ( regardless of scope) !.

Nitin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments