Pelles C forum

C language => Beginner questions => Topic started by: whatsup on September 03, 2010, 11:03:15 AM

Title: big trouble with optimizing
Post by: whatsup on September 03, 2010, 11:03:15 AM
first of all, the main reason i'm using pelles is because of the optimization.

i have a project (attached)
that i'm facing big trouble.
when i build it with optimization turned off, everything works fine.
when i build it with optimization turned on, there is a bug.

the bug is that the list in the second column , doesn't exist.

i tried to produce asm code, but couldn't find the problem.
i'll appreciate if someone can solve this mistory.

thanks in advanced.

in the attached file, you'll find the c file, and the batch i use to build it.
Title: Re: big trouble with optimizing
Post by: frankie on September 03, 2010, 01:33:11 PM
It's simply a problem of LV_ITEM structure initialization.
By zeroing that structure before to use (that should be a good practice always, especially if you don't fill in all the fields) the problem is corrected.
It apparently seems to depend on optimizations because for some reason the rubbish on the stack assigned to the automatic variable is correct or not (depending on the stack former usage) based on optimization.
Make the following change and it will work for any optimization. Alternatively you can set the remaining structure members (like mask, stateMask, etc.) with correct values.

void MakeListView (HWND hWnd)
{
  LV_COLUMN  lvCol;
  LV_ITEM  lvItem;
  memset(&lvItem,0,sizeof(LV_ITEM)); //Set unused members to zero
Title: Re: big trouble with optimizing
Post by: whatsup on September 05, 2010, 02:04:16 AM
thank you so much sir.
i'm shame it was my fault :(
Title: Re: big trouble with optimizing
Post by: CommonTater on September 05, 2010, 05:58:00 PM
Quote from: frankie on September 03, 2010, 01:33:11 PM
It's simply a problem of LV_ITEM structure initialization.
void MakeListView (HWND hWnd)
{
  LV_COLUMN  lvCol;
  LV_ITEM  lvItem;
  memset(&lvItem,0,sizeof(LV_ITEM)); //Set unused members to zero


Pelles C will also "auto initialize" the struct for you...

LV_ITEM lvitem = {0};

Will cause the compiler to generate the initialization code for you.