Solved

Stray Pointer??

Posted on 2001-09-17
5
239 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 128
C++ Language error 28 245
convert char array to number in c 5 86
FMX TEdit KeyUp handler detecting  "enter" key 4 13
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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