getenv connected to argc corruption?

Discussion in 'C Programming' started by anuragmenon@gmail.com, Feb 16, 2006.

  1. Guest

    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
     
    , Feb 16, 2006
    #1
    1. Advertising

  2. writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 16, 2006
    #2
    1. Advertising

  3. Joe Wright Guest

    wrote:
    > 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.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Feb 16, 2006
    #3
  4. Q Guest

    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.

    :)
     
    Q, Feb 17, 2006
    #4
  5. "Q" <> writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 17, 2006
    #5
  6. <> wrote in message
    news:...
    > 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...
     
    Rod Pemberton, Feb 18, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    setuid() and getenv()?

    vertigo, Jul 16, 2004, in forum: Perl
    Replies:
    1
    Views:
    691
    Joe Smith
    Jul 17, 2004
  2. RC
    Replies:
    1
    Views:
    661
    Tor Iver Wilhelmsen
    Jul 7, 2005
  3. Benjamin Lerman

    System.getenv()

    Benjamin Lerman, May 9, 2006, in forum: Java
    Replies:
    11
    Views:
    18,859
    sasami009
    Mar 27, 2009
  4. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  5. lordy

    To getenv or not getenv

    lordy, Jul 28, 2006, in forum: Java
    Replies:
    2
    Views:
    4,811
    lordy
    Jul 28, 2006
Loading...

Share This Page