By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Stack corruption

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"harry" <ar********@gmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
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

Jul 23 '05 #2

P: n/a
harry wrote:
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
Jul 23 '05 #3

P: n/a
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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.