473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Interesting pointer problem (g++ v 3.1.1-24)

Greetings!

Please consider the following piece of program code (note that I have stripped
quite a lot of code here):

int foo(void)
{
unsigned short l_valbuf; // address of this goes to another function
Objtype *l_item; // I want to get this one's data!

..
..
..

l_item->QueryData(&l_valbuf,BITMASK);

}

l_valbuf is to act as a buffer for a two-byte data entity to be read out from
an object refrenced by l_item.

Now, when I attempt to read out a piece of data (as defined by BITMASK) and
have it put into the local variable l_valbuf I get absolute nonsense (although
the address QueryData() receives is correct - I already have verified this).
But when I make l_valbuf global, everything works just fine. *boggle*
Anyone around who has experienced this or a similar problem?
Any ideas how to make this thing work properly with local variables? Or is
this some sort of subtle bug within g++?
I'm using GNU C++ version 3.1.1-24 for Linux (underlying kernel is 2.4.21-166)

Any help would be greatly appreciated.

--
Olaf Martens Linux User #246244 http://counter.li.org/
Hugo-Luther-Str. 8 E-Mail: ol*********@arcor.de
38118 Braunschweig Fon: +49-531-314834
"Who the heck is General Failure, and why is he reading my harddisk?"

Jul 22 '05 #1
6 1392

"Olaf Martens" <ol*********@arcor.de> wrote in message
news:40***********************@newsread4.arcor-online.net...
Greetings!

Please consider the following piece of program code (note that I have stripped quite a lot of code here):

int foo(void)
{
unsigned short l_valbuf; // address of this goes to another function
Objtype *l_item; // I want to get this one's data!

.
.
.

l_item->QueryData(&l_valbuf,BITMASK);

}

l_valbuf is to act as a buffer for a two-byte data entity to be read out from an object refrenced by l_item.

Now, when I attempt to read out a piece of data (as defined by BITMASK) and have it put into the local variable l_valbuf I get absolute nonsense (although the address QueryData() receives is correct - I already have verified this). But when I make l_valbuf global, everything works just fine. *boggle*
Anyone around who has experienced this or a similar problem?
Any ideas how to make this thing work properly with local variables? Or is
this some sort of subtle bug within g++?
I'm using GNU C++ version 3.1.1-24 for Linux (underlying kernel is

2.4.21-166)

The only 'bug' I see is in your understanding of how automatic objects
work. In your above function, the objects 'l_valbuf' and 'l_item' are
*destroyed*, *cease to exist*, when the function returns. So if you
take the address of an automatic object, and then try to use that
address after the object has been destroyed, you get what the language
calls 'undefined behavior', which might be manifest as a crash if you're
lucky, or 'seem to work' if you're not.

You can extend the lifetime of an object of function scope by defining
it as 'static'. This makes its lifetime that of the entire program.

int foo(void)
{
static unsigned short l_valbuf;
/* etc */
}

-Mike
Jul 22 '05 #2
Olaf Martens wrote:


Please consider the following piece of program code
(note that I have stripped quite a lot of code here):
To make it easier for us to figure out what you did wrong, I suppose.

int foo(void) {
unsigned short l_valbuf;
// address of this goes to another function
Objtype* l_item; // I want to get this one's data!

.
.
.

l_item->QueryData(&l_valbuf,BITMASK);

}

l_valbuf is to act as a buffer for a two-byte data entity
to be read out from an object refrenced by l_item.

Now, when I attempt to read out a piece of data (as defined by BITMASK)
and have it put into the local variable l_valbuf,
Can you show us exactly how you do this?
I get absolute nonsense (although the address QueryData()
receives is correct - I already have verified this).
But when I make l_valbuf global, everything works just fine. *boggle*
Anyone around who has experienced this or a similar problem?
Any ideas how to make this thing work properly with local variables?
Or is this some sort of subtle bug within g++?
No. You just screwed up.
I'm using GNU C++ version 3.1.1-24 for Linux
(underlying kernel is 2.4.21-166)


Please show us the definition of QueryData.

Jul 22 '05 #3
Olaf Martens wrote:
Greetings!

Please consider the following piece of program code (note that I have
stripped quite a lot of code here):

int foo(void)
{
unsigned short l_valbuf; // address of this goes to another function
Objtype *l_item; // I want to get this one's data!

.
.
.

l_item->QueryData(&l_valbuf,BITMASK);

}

l_valbuf is to act as a buffer for a two-byte data entity to be read out
from an object refrenced by l_item.

Now, when I attempt to read out a piece of data (as defined by BITMASK)
and have it put into the local variable l_valbuf I get absolute nonsense
(although the address QueryData() receives is correct - I already have
verified this). But when I make l_valbuf global, everything works just
fine. *boggle*
Anyone around who has experienced this or a similar problem?
Any ideas how to make this thing work properly with local variables? Or
is this some sort of subtle bug within g++?
I'm using GNU C++ version 3.1.1-24 for Linux (underlying kernel is
2.4.21-166)

Any help would be greatly appreciated.

It's hard to tell what's going wrong. It would be helpful if you posted
a minimal, correct piece of code to show the problem; that is, one with
complete (but short) function and type definitions.

Here's something to start with; see whether this produces the error
you're seeing in your larger program. The value printed should be 4.

struct S
{
void set_my_local_int( int* p, int i ) { *p = i; }
};

#include <iostream>

int main( )
{
int i = 0;
S s;

s.set_my_local_int( &i, 4 );

std::cout << i << '\n';
}

Jul 22 '05 #4
Jeff Schwab schrieb:
Olaf Martens wrote:
[snip]

It's hard to tell what's going wrong. It would be helpful if you posted
a minimal, correct piece of code to show the problem; that is, one with
complete (but short) function and type definitions.


Thanks for the help, but by coincidence I managed to fix the problem: When I
initialize the variable l_valbuf with some arbitrary value (static and the
likes is not necessary in this case) the call of
l_item->QueryData(&l_valbuf,BITMASK); works like a charm. I don't know what
was going on before the modification, but I already noticed during a debug
session that g++ is packing multiple variables in one spot (providing they are
used strictly in sequence, and no variable is ever accessed again once the
next variable comes to use), and so some other operation must have trashed the
(uninitialized) var after the function call returned.

--
Olaf Martens Linux User #246244 http://counter.li.org/
Hugo-Luther-Str. 8 E-Mail: ol*********@arcor.de
38118 Braunschweig Fon: +49-531-314834
"Who the heck is General Failure, and why is he reading my harddisk?"

Jul 22 '05 #5
Olaf Martens wrote:
Jeff Schwab schrieb:
Olaf Martens wrote:

[snip]

Thanks for the help, but by coincidence I managed to fix the problem:
When I initialize the variable l_valbuf with some arbitrary value
(static and the likes is not necessary in this case) the call of
l_item->QueryData(&l_valbuf,BITMASK); works like a charm.


The issue is that you have a local variable, and passing the address of
the local variable to a function called QueryValue. QueryValue is
supposed to change the value of the local variable. When you return
from QueryValue, you expect a certain value for l_valbug.

OK, so are you testing l_valbuf *immediately* after the call from
QueryData? If l_valbuf is being checked there, then either

1) QueryData is buggy, or

2) there is something about QueryData that relies on l_valbuf being set
beforehand, or

3) you are just not using QueryData properly (i.e. it needs to be called
after other functions have been called).

Those are the *only* possibilities that I can think of *if* you are
checking l_valbuf *immediately* after the call to QueryValue. If you're
doing a whole bunch of other stuff after the call to QueryValue, and
then check l_valbuf *within the foo function*, then your original
question cannot be answered until we see what you did in the time you
called QueryValue, and when you actually checked the l_valbuf value.

If you are leaving the foo() function, and for some reason expecting
l_valbuf to retain your data, that isn't going to work for the obvious
reason that local variables will be destroyed on function exit.

In any event, never blame the compiler first for something very simple
as passing an address and not expecting the results you desire. 99.9%
percent of the time for "bugs" such as this, it is the fault of the
coder not understanding some aspect of C++ and pointers or the program
is doing something illegal. Before even hinting of a compiler bug, just
think: if the compiler (g++ in this case) had such a problem with a
simple concept of pointer passing, literally thousands of other
programmers would have encountered it and would have reported it (and
hopefully it would have been fixed). The g++ compiler is mature enough
to not have such an obvious bug such as passing a pointer to a local and
have things screw up.

Paul
Jul 22 '05 #6
Paul wrote:

Of course, where I stated QueryValue in my previous post, it should be
QueryData.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Davy | last post by:
I found a interesting pointer problem. What does the pointer mean? EXTERN short (*blocks); And what does the malloc mean? blocks = (short (*))malloc(count*sizeof(short )); Any suggestions...
27
by: Riaan Cillié | last post by:
Hi I'm trying to learn C, but I am struggling with using scanf and a struct. I want to define a structure and declare a variable of that type in int main. This has to be passed to a function and...
23
by: Bruno R. Dias | last post by:
Perhaps it would be interesting to program a virtual machine simulating an ancient computer (such as the pdp-7). Then, it would be rather interesting to code for it (porting gcc to it maybe?). I...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
11
by: Antoninus Twink | last post by:
What's the correct syntax to define a function that returns a pointer to a function? Specifically, I'd like a function that takes an int, and returns a pointer to a function that takes an int and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.