News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

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

Started by rudolf128, January 31, 2019, 11:13:22 PM

Previous topic - Next topic

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.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

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

#3
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.

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.

rudolf128

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);
}

Miguel Gimenez

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.

rudolf128

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);
}

rudolf128

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).

rudolf128

To be sure, I downloaded my own attachment, and I got it OK.

rudolf128

Problem seems to be that lower case "I" is not accepted in the post text.

rudolf128

I understand now: these 3 characters are taken as "set font to italics"

stahta01

C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]