?
Solved

Stray Pointer??

Posted on 2001-09-17
5
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 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