I was completely baffled withis problem. I have rather large C program. While debugging it I got to a point where the index of a "for" seems not to behave correctly. As the program is lage, I extracted from it the part originating the problem, as follows:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define HASHSIZE 8
enum {NOTAB, RINGTAB, MODTAB};
typedef struct
{
char u;
char v;
} hashing;
typedef struct // For any parameter input
{
char *key;
char *anydata;
int attrib;
int use;
double prob;
int *place;
} general;
int X;
hashing Htable[HASHSIZE];
general Tabring[] =
{
{"", 0, '+', 0, 0, &X},
{"AND", (char *)&Tabring, '=', 0, 0, &X},
{"EQ", (char *)&Tabring, '=', 0, 0, &X},
{"NAND", (char *)&Tabring, '=', 0, 0, &X},
{"", "*", '*', 0, 0, &X}
};
general Tabmod[] = // Table of modifier names
{
{"", 0, '&', 0, 0, &X},
{"PHASE", (char *)&Tabmod, '=', 0, 0, &X},
{"RAND", (char *)&Tabmod, '=', 0, 0, &X},
{"", "*", -1, 0, 0, &X}
};
general *Tab[] = {NULL, Tabring, Tabmod, NULL};
void clear(void)
{
int i;
for (i = 0; i < HASHSIZE; i++)
Htable.v = Htable.u = 0;
}
void fillhash(char n, int k, char *key)
{
int j, i = 0, x = n, y = 0;
j = (int)key[0];
while (j > 32)
{
y += (x += j);
j = (int)key[++i];
}
y &= HASHSIZE - 1;
while (Htable[y].u)
{
y++;
y &= HASHSIZE - 1;
}
Htable[y].u = n;
Htable[y].v = (char)k;
}
void inittables(void)
{
int i;
for (i = 1; Tabring.key[0]; i++)
fillhash(RINGTAB, i, Tabring.key); // <- Here
for (i = 1; Tabmod.key[0]; i++)
fillhash(MODTAB, i, Tabmod.key);
}
int main(int argc, char *argv[])
{
int i;
clear();
inittables();
for (i = 0; i < HASHSIZE; i++)
printf("Hashtable[%d] = %d %d\n", i, Htable.u, Htable.v);
return 0;
}
This is a complete program, and runs perfectly. But in the large program, when debugging it, in the place signalled by "Here", index i becomes 2 instead of 1 the first time, and then it continues 3, 4...
Please, does somebody have an explanation?
Post the full build log; you need to have no optimization enabled if you wish to get the output you expect.
Tim S.
Thanks. Sorry for the typos; my spell checker is only for Spanish.
Here is the build log:
-------------- Build: Debug in Composer (compiler: GNU GCC Compiler)---------------
mingw32-gcc.exe -Wall -g -Og -g -c E:\WavCompil\Composer\main.c -o obj\Debug\main.o
mingw32-g++.exe -o bin\Debug\Composer.exe obj\Debug\main.o
Output file is bin\Debug\Composer.exe with size 132.29 KB
Process terminated with status 0 (0 minute(s), 3 second(s))
0 error(s), 0 warning(s) (0 minute(s), 3 second(s))
What is the strange thing in the "Test" program that you felt would need not to be optimized?
fillhash() does not modify the array passed as third parameter, so in your loop
for (i = 1; Tabring.key[0]; i++)
fillhash(RINGTAB, i, Tabring.key); // <- Here
Tabring.key is not changed inside the loop and (depending on the initial value of Tabring.key[0]) the loop is not executed at all or it is executed indefinitely.
As you initialized key with "" the loop will not be executed. If you use any other key the loop will run forever.
EDIT: regarding optimization, the compiler knows key = "", so it also knows the loop won't be executed and it can wipe the loop and fillhash(), because it isn't called. In fact even inittables() can be optimized away. This decreases file size and increases speed, but make debugging troublesome, because the code you want to debug isn't there. So always debug unoptimized programs.
Thanks.
But please notice that the loop starts with i = 1, not i = 0, so the pointer to the char string is not NULL the first time. Notice too that the last element of the Tabring vector is NULL, and then the loop ends when it reaches this last element.
I will try to replace the "for" with a "while", to make things clearer, and maybe then the program will execute correctly.
Please notice that the code in your last post is missing the indexing by i. The right code is:
void inittables(void)
{
int i;
for (i = 1; Tabring.key[0]; i++)
fillhash(RINGTAB, i, Tabring.key); // <- Here
for (i = 1; Tabmod.key[0]; i++)
fillhash(MODTAB, i, Tabmod.key);
}
The index start value is irrelevant, because the loop condition (Tabring.key[0]) is constant and independent of the index.
In any case the unexpected behaviour of your code is not a bug in C::B, so you must ask in a general programming forum.
I dont know how it happened, but I am sure the code I sent was not the one that appears at post 15 but:
void inittables(void)
{
int i;
for (i = 1; Tabring.key[0]; i++)
fillhash(RINGTAB, i, Tabring.key); // <- Here
for (i = 1; Tabmod.key[0]; i++)
fillhash(MODTAB, i, Tabmod.key);
}
Well, this is even more strange! In my previous post, Tabring is indexed by , but now I see this dissapeared in the post!
So, now I will attach the complete code (not from the large program but from the Test).
To be sure, I downloaded my own attachment, and I got it OK.
Problem seems to be that lower case "I" is not accepted in the post text.
I understand now: these 3 characters are taken as "set font to italics"
Please read this website rules and use code tags when posting code or build logs.
http://forums.next.codeblocks.org/index.php/topic,9996.0.html (http://forums.next.codeblocks.org/index.php/topic,9996.0.html)
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F (http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F)
Tim S.