?
Solved

Mysterious erase of this pointer info in MVC++

Posted on 2003-03-24
4
Medium Priority
?
193 Views
Last Modified: 2010-04-01
I am working in Microsoft Visual C++.  For some reason my class info disappears when I get to a certain point.
Here is a sample of what I have:
my .h file looks something like this
class A{
public:
   A(int b);//constructor
   init();//function
private:
//some data values
int a;
}

my .cpp file looks something like this:

A::A(int b){
a=b;
init():
}

A::init(){
char dummy1[3], dummy2[3];
strcpy(dummy1,"dummyinfo");
itoa(a,dummy2,10);
}

The problem that I am having is when it goes inside the init function, right after it executes strcpy, it erases all of the this pointer information, so when I want to call itoa with the variable a passed into it, a does not exist anymore.  If I get rid of the call to strcpy(...), the variable a exists and the code works.
i.e.
//this works
A::init(){
char dummy1[3], dummy2[3];
//strcpy(dummy1,"dummyinfo");
itoa(a,dummy2,10);
}
Don't understand this at all, would appreciate it if somebody could tell me what was going on.
K
0
Comment
Question by:KayodeCS_BU
[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
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:skyDaemon
ID: 8198991
Your call to strcpy is overflowing dummy1 which can only hold 3 characters.  Since dummy2 is declared right after dummy1, the overflow will likely overwrite whatever is in dummy2, as well as another 4 bytes passed the end of dummy2.  Try declaring dummy1 as dummy1[9] and see what happens.



0
 

Author Comment

by:KayodeCS_BU
ID: 8199084
Thank you, it does work.  Even with the overflow, I still don't understand why it would erase all of the class info though.  I have a lot of other private variables in the class and they all were deleted also.  Do you know why that happens?
0
 
LVL 2

Accepted Solution

by:
skyDaemon earned 1000 total points
ID: 8199114
Maybe the next variable was the pointer to your class (conveniently 4 bytes).  If you overwrote that, then your object would suddenly think it existed way out in random memory somewhere.  That could explain why all your variables disappeared.
0
 

Author Comment

by:KayodeCS_BU
ID: 8199148
Thank you
K
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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

771 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