getenv connected to argc corruption?

A

anuragmenon

Hello,

Trying to debug a weird problem here. When I run this program many
times over, at some point argc gets corrupted and I get the error that
enough number of arguments are not provided. It definitely seems to
have something to do with the env. variable being set and used because
the previous version of this code did not have that variable and did
not have this problem. I am running this on linux on a powerpc based
hardware. stdlib.h is included.

My condition is that if this env variable does not exist at all or if
it exisits and it is set to 1, I want to <DO STUFF> and if it exisits
and is set to 0, <DO NOT DO STUFF>


Here is the relevant portion of the program I am trying to debug. .

int main(int argc, char** argv) {

char* status = 0;

if(argc < 2)
{
fprintf(stderr, " Not enough arguments.\n");
fflush(stderr);
}

while(1)
{

status = getenv("TIMER_STATUS");
if((status == 0) || ((status != 0) && (atoi(status) != 1)))
{
<Do STUFF>
}
else
{
<DO NOT STUFF>

}
}

In MOST cases, the program runs just fine. But after about 10-15 times
of running repeatedly, i will always get the error that there are not
enough arguments. If I reboot the entire board that will restart the
kernel and everything, all is well once again until we get into that
restarting many times scenario.

Any ideas of where I might start to debug this?

Thanks much,
Vinod
 
K

Keith Thompson

Trying to debug a weird problem here. When I run this program many
times over, at some point argc gets corrupted and I get the error that
enough number of arguments are not provided. It definitely seems to
have something to do with the env. variable being set and used because
the previous version of this code did not have that variable and did
not have this problem. I am running this on linux on a powerpc based
hardware. stdlib.h is included.

My condition is that if this env variable does not exist at all or if
it exisits and it is set to 1, I want to <DO STUFF> and if it exisits
and is set to 0, <DO NOT DO STUFF>


Here is the relevant portion of the program I am trying to debug. .

How do you know this is the relevant portion? Since you don't know
what the problem is, you probably don't know what's causing the
problem.
int main(int argc, char** argv) {

char* status = 0;

if(argc < 2)
{
fprintf(stderr, " Not enough arguments.\n");
fflush(stderr);
}

Here you print an error message (the fflush() is unnecessary, BTW),
but the program continues to run.
while(1)
{

status = getenv("TIMER_STATUS");
if((status == 0) || ((status != 0) && (atoi(status) != 1)))
{
<Do STUFF>
}
else
{
<DO NOT STUFF>

}
}

In MOST cases, the program runs just fine. But after about 10-15 times
of running repeatedly, i will always get the error that there are not
enough arguments. If I reboot the entire board that will restart the
kernel and everything, all is well once again until we get into that
restarting many times scenario.

It sounds like the problem is something external to your program.
 
J

Joe Wright

Hello,

Trying to debug a weird problem here. When I run this program many
times over, at some point argc gets corrupted and I get the error that
enough number of arguments are not provided. It definitely seems to
have something to do with the env. variable being set and used because
the previous version of this code did not have that variable and did
not have this problem. I am running this on linux on a powerpc based
hardware. stdlib.h is included.

My condition is that if this env variable does not exist at all or if
it exisits and it is set to 1, I want to <DO STUFF> and if it exisits
and is set to 0, <DO NOT DO STUFF>


Here is the relevant portion of the program I am trying to debug. .

int main(int argc, char** argv) {

char* status = 0;

if(argc < 2)
{
fprintf(stderr, " Not enough arguments.\n");
fflush(stderr);
}

while(1)
{

status = getenv("TIMER_STATUS");
if((status == 0) || ((status != 0) && (atoi(status) != 1)))
{
<Do STUFF>
}
else
{
<DO NOT STUFF>

}
}

In MOST cases, the program runs just fine. But after about 10-15 times
of running repeatedly, i will always get the error that there are not
enough arguments. If I reboot the entire board that will restart the
kernel and everything, all is well once again until we get into that
restarting many times scenario.

Any ideas of where I might start to debug this?

Thanks much,
Vinod
You could start by presenting us the program that you are compiling. The
snippet above is not a program and will not compile. If you post a short
program that compiles correctly and still exhibits your problem, I will
be happy to try to help.
 
Q

Q

I agree with the others. it is near impossible to determine what has
gone wrong if we don't have a clear understanding of what the program
is "expected" to do.

:)
 
K

Keith Thompson

Q said:
I agree with the others. it is near impossible to determine what has
gone wrong if we don't have a clear understanding of what the program
is "expected" to do.

It's even harder if there's no context.

If you've never read this newsgroup before, you probably haven't seen
<http://cfaj.freeshell.org/google/> or any of the other explanations
of how to post properly -- but then again, you should probably read a
newsgroup for a while before posting.
 
R

Rod Pemberton

Hello,

Trying to debug a weird problem here. When I run this program many
times over, at some point argc gets corrupted and I get the error that
enough number of arguments are not provided. It definitely seems to
have something to do with the env. variable being set and used because
the previous version of this code did not have that variable and did
not have this problem. I am running this on linux on a powerpc based
hardware. stdlib.h is included.

My condition is that if this env variable does not exist at all or if
it exisits and it is set to 1, I want to <DO STUFF> and if it exisits
and is set to 0, <DO NOT DO STUFF>
In MOST cases, the program runs just fine. But after about 10-15 times
of running repeatedly, i will always get the error that there are not
enough arguments. If I reboot the entire board that will restart the
kernel and everything, all is well once again until we get into that
restarting many times scenario.

Any ideas of where I might start to debug this?

Thanks much,
Vinod

I've experienced a similar problem with a simple getopt() routine as part of
a directory search program. It works fine with DJGPP but will lose access
to argc and argv under OpenWATCOM 1.3. The routine only uses string
functions (strcpy,strcmp,strcat) and _searchenv, a DOS specific function. I
haven't had time to locate the error. The openwatcom.users.c_cpp newgroup
was unfamiliar with the issue.

Rod Pemberton

PS. 2nd post, sorry if first shows up...
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top