Author Topic: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?  (Read 4791 times)

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« on: January 31, 2019, 11:13:22 pm »
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?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #1 on: February 01, 2019, 12:01:59 am »
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++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #2 on: February 01, 2019, 04:52:55 am »
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?

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #3 on: February 01, 2019, 09:34:12 am »
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

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.
« Last Edit: February 01, 2019, 09:51:40 am by Miguel Gimenez »

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #4 on: February 01, 2019, 05:50:08 pm »
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.

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #5 on: February 01, 2019, 05:54:31 pm »
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);
}

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #6 on: February 01, 2019, 06:09:57 pm »
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.

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #7 on: February 02, 2019, 06:27:51 am »
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);
}

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #8 on: February 02, 2019, 06:35:39 am »
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).

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #9 on: February 02, 2019, 06:37:25 am »
To be sure, I downloaded my own attachment, and I got it OK.

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #10 on: February 02, 2019, 06:39:30 am »
Problem seems to be that lower case "I" is not accepted in the post text.

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: Bug in CodeBlocks for Windows 10, Mingw compiler or debugger?
« Reply #11 on: February 02, 2019, 07:04:23 am »
I understand now: these 3 characters are taken as "set font to italics"

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org