Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Mysterious erase of this pointer info in MVC++

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
KayodeCS_BU
Asked:
KayodeCS_BU
  • 2
  • 2
1 Solution
 
skyDaemonCommented:
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
 
KayodeCS_BUAuthor Commented:
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
 
skyDaemonCommented:
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
 
KayodeCS_BUAuthor Commented:
Thank you
K
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now