Solved

Stray Pointer??

Posted on 2001-09-17
5
220 Views
Last Modified: 2010-04-02
This "appears" to work, I can run the code and get the desired result. After I add additional software modules, the system crashes.

class CStatus {
public:
  STATUS setButtonRequest(const int*);
...
};

STATUS CStatus::setButtonRequest(const int* ipInButton)
{
  SButtonRequest* pSInButtonRequest;
  pSInButtonRequest->iButtonID = *(ipInButton);
}

struct SButtonRequest {
    int iButtonID;
};

In order to fix the problem I do the following:
class CStatus {
public:
  STATUS setButtonRequest(const int*);
  SButtonRequest* pSInButtonRequest;
...
};

CStatus::CStatus()
{
  pSInButtonRequest = new SButtonRequest;
}

STATUS CStatus::setButtonRequest(const int* ipInButton)
{
  pSInButtonRequest->iButtonID = *(ipInButton);
}

I'm trying to figure out why I have to allocate space on the heap for the pointer to the object.

Why can't I localize the creation of the pointer in my method and have the storage be allocated and deallocated on the stack. Does anyone know why this fails?

Is there another way of doing this?

thanks for any help in understanding this problem.
0
Comment
Question by:GoldStrike
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:imladris
Comment Utility
In the first example no SButtonRequest is ever created.

SButtonRequest* pSInButtonRequest;

This line merely creates a pointer to an SButtonRequest, but no SButtonRequest is created, nor is the pointer initialized to anything. In the second example though:

CStatus::CStatus()
{
 pSInButtonRequest = new SButtonRequest;
}

An SButtonRequest structure is created, andpSInButtonRequest is initialized to point to it.
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
Did you find this answer helpful at all?

If so it is time to grade it. Otherwise perhaps a clarifying question would help.
0
 

Author Comment

by:GoldStrike
Comment Utility
In the first example no SButtonRequest is ever created.

If this is the case why does the code work?

Is there another way of doing this without using the new statement?
0
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
Comment Utility
The code appears to work because C and C++ do not protect you against yourself. In the first sample in the following lines:

 SButtonRequest* pSInButtonRequest;
 pSInButtonRequest->iButtonID = *(ipInButton);

a pointer is created, but not initialized to point to an actual object. The value that pSInButtonRequest contains is undefined. Thus there is a reasonable probability that the assignment to the place in memory at offset iButtonID from that random value is in fact accessible to the program, and an assignment is made. This is why it is a "stray" pointer. It is not explicitly set to a valid memory location. At first, in your case, it apparently happens to be pointing to an accessible location. However, as you add code the underlying state of the machine changes and a different random value winds up in the pointer which winds up pointing to memory that the program is not allowed to access, and so it crashes.

The only alternative to new, is to create the object directly with the variable, in which case there would be no pointer. Changing the second example it would wind up looking something like:

class CStatus {
public:
 STATUS setButtonRequest(const int*);
 SButtonRequest SInButtonRequest;
...
};

STATUS CStatus::setButtonRequest(const int* ipInButton)
{
 SInButtonRequest.iButtonID = *(ipInButton);
}

This creates the object with the declaration. The SButtonRequest object will now implicitly be created when the CStatus object is created. Note that accessing members now occurs with '.' notation.
0
 

Author Comment

by:GoldStrike
Comment Utility
That's the explanation I was hoping to get.

thanks

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now