473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Stack corruption

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
3 2954

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ilia Poliakov | last post by:
I have a static function in the class like this: class A { static void f1() } void A::f1() { .....
18
by: Dev | last post by:
I'm working with some IBM sponsored C APIs to interface with a corporate legacy system, and it seems that I'm getting some stack corruption after any API call. I believe the APIs were designed...
1
by: Humber Consumer | last post by:
I have a simple question. I have following piece of code in a sample MFC application: void CSampleAppDlg::OnButton1() { try { char e; ...
14
by: Peter Schmitz | last post by:
Hi, I'm currently developing an application that includes an implementation of the well-known Boyer- Moore algorithm. For this, I use the implementation of Thierry Lecrq from...
6
by: Tony | last post by:
Is there any value to pursuing program designs that mimimize the mainline call stack? For example, within main() I could code up something like: while(GetMsg(m)) DispatchMsg(m); instead of...
13
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
7
by: ifoundgoldbug | last post by:
Ok here is a short description of what i am doing. I need to program a dll for use by another program. the dll compiles fine but when i call it from another program i get a stack corruption of...
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.