Stack corruption

H

harry

Hi all

I am putting a code snippet

#include "stdio.h"
#include "conio.h"
struct base{
int i;
char c;
};
struct der{
base b;
};
void FillUp(der *ptr)
{
ptr->b.i=10;
ptr->b.c=65;
}
int _tmain(int argc, _TCHAR* argv[])
{
der d;
FillUp(&d);
getch();
return 0;
}

I have a similar model in a bigger project. I am seeing some kind of
stack corruption. The value of "c" is not getting written to the
address at d.b.c when the function is called. Can anyone give me some
inputs abt when such a thing is possible?...or maybe point me to a link
where i can read about some stack issues.

thank you in advance...

Harry
 
H

Howard

harry said:
Hi all

I am putting a code snippet

#include "stdio.h"
#include "conio.h"
struct base{
int i;
char c;
};
struct der{
base b;
};
void FillUp(der *ptr)
{
ptr->b.i=10;
ptr->b.c=65;
}
int _tmain(int argc, _TCHAR* argv[])
{
der d;
FillUp(&d);
getch();
return 0;
}

I have a similar model in a bigger project. I am seeing some kind of
stack corruption. The value of "c" is not getting written to the
address at d.b.c when the function is called. Can anyone give me some
inputs abt when such a thing is possible?...or maybe point me to a link
where i can read about some stack issues.

thank you in advance...

Harry


I don't see any problem here. More likely, what you're seeing isn't related
to this code, but is caused by some other code problem and only exhibited in
this fashion. Look for any uninitialized pointers, or writing beyond the
end of arrays (such as indexing them from 1 instead of from 0, or forgetting
that a C-style string needs a null terminator at the end).

Try debugging. Check the address of d, and compare it to the value of ptr.
They should be the same. And step through, watching for any use of a
pointer that's not initialized yet, or has already been deleted.

But there's no problem in the code above, at least as far as setting the
members of d is concerned.


(By the way, I think explicitly researching "stack corruption" is likely a
waste of time.)

-Howard
 
V

Victor Bazarov

harry said:
I am putting a code snippet

#include "stdio.h"
#include "conio.h"
struct base{
int i;
char c;
};
struct der{
base b;
};
void FillUp(der *ptr)
{
ptr->b.i=10;
ptr->b.c=65;
}
int _tmain(int argc, _TCHAR* argv[])
{
der d;
FillUp(&d);
getch();
return 0;
}

I have a similar model in a bigger project. I am seeing some kind of
stack corruption. The value of "c" is not getting written to the
address at d.b.c when the function is called. Can anyone give me some
inputs abt when such a thing is possible?...or maybe point me to a link
where i can read about some stack issues.

Stack corruption is a nasty bug and sometimes hard to catch. You should
try some kind of memory debugging tool, like Insure++ or Purify. Since
you wrote _tmain and _TCHAR, I presume your platform is Windows. MS has
some memory checking facilities too. That said, you could do yourself
a favour, still.

Are you sure you've isolated the area where the stack corruption occurs?
The reason I ask is that if you run your "code snippet", no stack gets
corrupted, does it? That suggests that the "similar model" you have in
your "bigger project" is not similar enough. Could it be you accidentally
wrote

der *pd;
FillUp(pd);

instead of

der d;
FillUp(&d);

? It would make a world of difference, you know.

Try to localize the problem and carefully remove the excess code and you
will find the cause yourself. If you don't find the cause, post the
distilled version of the code here and we'll give it a shot.

V
 
E

Emmanuel Charruau

Hi,

Since this is a windows program, I did have a stack error last week
simply because I converted a vc++ 6.0 project to a vc++ 7 project.
Building a new project with exactly the same source files from scratch
was the solution (I lost hours before to find this solution).

Emmanuel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

FIELD_OFFSET 1
stack corruption 7
Linux: using "clone3" and "waitid" 0
Error : Copying user defined stack object to Another 2
Command Line Arguments 0
polymorphism 5
BST insertion 1
C++ Union compilation errors 5

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,211
Latest member
NelleWilde

Latest Threads

Top