Jump to content

Structs and Pointers


oMBRa
 Share

Recommended Posts

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

Link to comment
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]

Link to comment
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.
Link to comment
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.
Link to comment
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]

Link to comment
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.

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
Link to comment
Share on other sites

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...

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...