Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stray Pointer??

Posted on 2001-09-17
5
Medium Priority
?
257 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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

610 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