Sign in to follow this  
Followers 0
oMBRa

Structs and Pointers

13 posts in this topic

well, I just started studying C++, and I have a doubt with this piece of code:

struct Something
{
    int nX, nY, nZ;
};
Something *psValue;

I dont get what *psValue is, a pointer ?

thanks in advance

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Yes it is. However it doesn't point to any valid data so you cannot use it.

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Yes it is. However it doesn't point to any valid data so you cannot use it.

ok, Thanks :)

Share this post


Link to post
Share on other sites

Well, theoretically it could point to valid data. The chances are slim though.

Share this post


Link to post
Share on other sites

Technically what it points to is undefined and it should not be used till it is.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites

What it points to is whatever happened to last occupy the address. The initial address of a pointer is whatever happened to locate the address of the pointer. There is a valid (is minute) chance that it could be something.

It's not undefined in C++.

Share this post


Link to post
Share on other sites

What it points to is whatever happened to last occupy the address. The initial address of a pointer is whatever happened to locate the address of the pointer. There is a valid (is minute) chance that it could be something.

It's not undefined in C++.

I'm not really sure you're right there. Think about what the compiler is doing, why it's doing it from a technical stand-point and what would happen if the compiler did something different. Then think about what the term "undefined behavior" means.

Share this post


Link to post
Share on other sites

I would call it "invalid" more so than undefined. It is defined and technically has a value. It's just not a useful one yet.

Share this post


Link to post
Share on other sites

I would call it "invalid" more so than undefined. It is defined and technically has a value. It's just not a useful one yet.

"Undefined" is referring to the behavior of an uninitialized pointer rather than the pointer itself. The C++ specification just doesn't define the behavior of such a situation. A compiler can do whatever it wishes in this case, including giving the pointer a default value of 0.

Share this post


Link to post
Share on other sites

I would call it "invalid" more so than undefined. It is defined and technically has a value. It's just not a useful one yet.

I agree, the pointer would be defined and have a value however you would have no idea of its value and you certainly cant make any guarantees. It may be said that it contains an indeterminate initial value but I would not call that defined.

It's not undefined in C++.

Why would there be a difference in a declaring the pointer in C or C++? No auto initialization is done with pointers in either language.

The way I learnt was, what it points to is undefined mainly because the use of the pointer will lead to undefined behavior.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I would call it "invalid" more so than undefined. It is defined and technically has a value. It's just not a useful one yet.

The C++ standard calls it indeterminate (My emphasis):

If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a non-static object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed.

I can't find an actual description in the standard to describe what indeterminate means. I also can't find a description of what happens when you try to use an indeterminate value. The description of undefined behavior is this:

behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements. Undefined behavior may also be expected when this International Standard omits the description of any explicit definition of behavior. [Note: permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or with-out the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. ]

So my interpretation is: The C++ standard doesn't describe (unless I missed it) what happens when an attempt is made to use an indeterminate value. Since the standard says that undefined behavior exists for things that are not documented, this seems like something that is undefined. If somebody knows of another clause that I missed then please point it out. Otherwise I see nothing wrong with my interpretation. Keep in mind that this doesn't just apply to pointers - any uninitialized object would qualify.

Edit: Quotes are from ISO/IEC 14882:2003.

Edited by Valik

Share this post


Link to post
Share on other sites

Semantics. This is why programming languages can get jobs done better than English (or any other natural language).

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

An uninitialized object is a little different from an uninitialized simple variable, including a pointer.

An automatic variable containing a pointer would, without initialization, reference the data that was already in the memory space that the variable now occupies. This will usually be contents of automatic variables from prior function calls. Usually this is referred to as (and this is the technical term) junk data, because the information in that memory space, when interpreted in the new and possibly misaligned type, is nonsensical. Following such a pointer is just asking for trouble. If a variable is initialized then we know what to expect. BTW, initializing is more efficient than creating a variable and later assigning a first value to the variable, unless you do not know the value to be assigned when the variable is created.

e.g.

int func1(void)
{
     int i, *p;   // i could have just about any valid value.  p could point anywhere.  This is scary.
     int k=0, *q = &i;  // k is initialized to 0.  q points to i.  This is good.

An object can be given a default constructor, which should set all of the objects data members to reasonable values. This is a capability that just does not apply to simple variables under most circumstances. If you do not have a default constructor, then the data members will again point to just about anywhere; this is not really a good idea. Most C++ compilers (all the ones I have used) will complain if your class does not contain a default constructor if you have any private data members.

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0