Solved

Stray Pointer??

Posted on 2001-09-17
5
238 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
ID: 6488475
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
ID: 6493718
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
ID: 6496874
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
ID: 6500108
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
ID: 6500134
That's the explanation I was hoping to get.

thanks

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

803 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