User forums > General (but related to Code::Blocks)

Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?

(1/3) > >>

rudolf128:
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?

stahta01:
Post the full build log; you need to have no optimization enabled if you wish to get the output you expect.

Tim S.

rudolf128:
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?

Miguel Gimenez:
fillhash() does not modify the array passed as third parameter, so in your loop


--- Code: ---  for (i = 1; Tabring.key[0]; i++)
    fillhash(RINGTAB, i, Tabring.key); // <- Here

--- End code ---

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.

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

Navigation

[0] Message Index

[#] Next page

Go to full version