Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer return value changes after function call

Code snippets from two C source files:

A.c

Channel *testChannelGet()
{
    Channel *ch = channelGet (parser,parserCh);
    return ch;
}

B.c

Channel *channelGet(UINT8 parser, UINT16 parserCh)
{
    chnl.player = &solPlayer;
    return((Channel *)&chnl);
}

I compile both files and create a static and a shared library. Now I call testChannelGet from a sample program. When I link it against the static library, it works perfectly. But if I link it against the shared library, its SEGFAULTing. Debugging tells me that the pointer returned from channelGet is changing the moment it returns. GDB output below.

174         Channel *ch = channelGet (parser,parserCh);
(gdb) s
channelGet (parser=1 '\001', parserCh=1) at B.c:15174
15174           chnl.player = &solPlayer;
(gdb) n
15175           return((Channel *)&chnl);
(gdb) p ((Channel *)&chnl)
$1 = (Channel *) 0x7ffff7fed1a0
(gdb) n
15176   }
(gdb) n
testChannelGet at A.c:175
175         return ch;
(gdb) p ch
$2 = (Channel *) 0xfffffffff7fed1a0

It seems the address value points to a different offset now - 0xfffffffff7fed1a0 vs 0x7ffff7fed1a0 . The last bytes in both addresses are the same.

Any hints? I have tried the -fPIC option to no avail.

like image 343
Vasu Avatar asked Dec 18 '11 15:12

Vasu


People also ask

Does a pointer change the value?

Clearer thinking would have let you find the answer yourself much more quickly. Pointers don't "represent" values; they point to values (hence the name). You want to assign to the value that the pointer points at. You get the pointed-at value by dereferencing the pointer.

Can a pointer variable be returned by a function?

We can pass pointers to the function as well as return pointer from a function. But it is not recommended to return the address of a local variable outside the function as it goes out of scope after function returns.

How does a pointer return a value?

Return Function Pointer From Function: To return a function pointer from a function, the return type of function should be a pointer to another function. But the compiler doesn't accept such a return type for a function, so we need to define a type that represents that particular function pointer.

Can a pointer be changed within function?

Can a pointer be changed within function? If you modify the pointer inside the called function, you only modify the copy of the pointer, but the original pointer remains unmodified and still points to the original variable.


2 Answers

Is there a prototype in scope for channelGet() in A.c?

If not, the results you're seeing could be explained as follows:

  • channelGet() is assumed to return int (due to lack of prototype), so the result is truncated to 0xf7fed1a0
  • then it is cast to a 64-bit pointer, so gets sign-extended to 0xfffffffff7fed1a0

(You should get complaints about this if you compile with warnings enabled, of course...)

like image 55
Matthew Slattery Avatar answered Oct 13 '22 04:10

Matthew Slattery


Run your program under valgrind. Find and fix any errors it reports.

like image 30
John Zwinck Avatar answered Oct 13 '22 04:10

John Zwinck