Ray Mitchell
2009-10-07 20:16:02 UTC
Hello,
Have you ever had a program that simply seemed to defy the code it
contained? In my experience I've only seen it a few times and now I'm seeing
it again in one of my VS 2005 C programs (WinXP Pro). I know there must be
something fundamentally wrong in the program overall but I'm at a loss for a
reasonable way to debug it. For example, consider:
FILE *fp;
fp = fopen(rinexFilePath, "r+");
if (fp == NULL) // if file doesn't
exist...
{
if ((fp = fopen(rinexFilePath, "w")) == NULL) // ...create it
{
perror(rinexFilePath);
return 1;
}
}
x = 5;
The file I'm trying to open doesn't exist, so the first fopen fails and
assigns a null pointer into fp. On the next line I test fp so I can attempt
to create the file if it was found not to exist. When I breakpoint with the
debugger at the first "if", it shows that fp is indeed null. However, when I
single-step once more the program jumps to the statement, x = 5;. Just for
laughs I tried "if (!fp)" instead, which should be identical, and it didn't
work properly either.
The same program also sometimes works differently for things like the
following, simply because of the unnecessary braces:
for (whatever1)
for (whatever2)
for (whatever3)
statement;
versus
for (whatever1)
{
for (whatever2)
{
for (whatever3)
{
statement;
}
}
}
I've seen things like this before, including programs that run differently
based upon the names chosen for the variables, the time of day they are run,
and other bizarre things. The problem is that I don't know how to begin
debugging such a thing short of getting into the assembly code itself, which
I really dread doing. Maybe moving to VS2008 or VS2010, crossing my fingers,
and throwing salt over my left shoulder is the most appropriate first
approach. Any ideas?
Thanks,
Ray
Have you ever had a program that simply seemed to defy the code it
contained? In my experience I've only seen it a few times and now I'm seeing
it again in one of my VS 2005 C programs (WinXP Pro). I know there must be
something fundamentally wrong in the program overall but I'm at a loss for a
reasonable way to debug it. For example, consider:
FILE *fp;
fp = fopen(rinexFilePath, "r+");
if (fp == NULL) // if file doesn't
exist...
{
if ((fp = fopen(rinexFilePath, "w")) == NULL) // ...create it
{
perror(rinexFilePath);
return 1;
}
}
x = 5;
The file I'm trying to open doesn't exist, so the first fopen fails and
assigns a null pointer into fp. On the next line I test fp so I can attempt
to create the file if it was found not to exist. When I breakpoint with the
debugger at the first "if", it shows that fp is indeed null. However, when I
single-step once more the program jumps to the statement, x = 5;. Just for
laughs I tried "if (!fp)" instead, which should be identical, and it didn't
work properly either.
The same program also sometimes works differently for things like the
following, simply because of the unnecessary braces:
for (whatever1)
for (whatever2)
for (whatever3)
statement;
versus
for (whatever1)
{
for (whatever2)
{
for (whatever3)
{
statement;
}
}
}
I've seen things like this before, including programs that run differently
based upon the names chosen for the variables, the time of day they are run,
and other bizarre things. The problem is that I don't know how to begin
debugging such a thing short of getting into the assembly code itself, which
I really dread doing. Maybe moving to VS2008 or VS2010, crossing my fingers,
and throwing salt over my left shoulder is the most appropriate first
approach. Any ideas?
Thanks,
Ray