NO

Author Topic: struct do not work  (Read 3035 times)

elmilenmch

  • Guest
struct do not work
« on: January 05, 2016, 03:22:10 PM »
In this code:

Code: [Select]
#include <SFML/Graphics.h>

int _cdecl main(void)
{
    sfVideoMode mode = {800, 200, 32};
    sfRenderWindow * window;
    sfEvent event;

sfVector2f v = {10, 0};

    window = sfRenderWindow_create(mode, "SFML window", sfClose, NULL);
sfRenderWindow_setFramerateLimit(window, 60);

sfCircleShape* c1 = sfCircleShape_create();
sfCircleShape_setFillColor(c1, sfGreen);
sfCircleShape_setRadius(c1, 100);

    while (sfRenderWindow_isOpen(window)){
        while (sfRenderWindow_pollEvent(window, &event)){if (event.type == sfEvtClosed) sfRenderWindow_close(window);}
        sfRenderWindow_clear(window, sfYellow);
sfRenderWindow_drawCircleShape(window, c1, NULL);
        sfRenderWindow_display(window);

sfCircleShape_move(c1, v);

if ((sfCircleShape_getPosition(c1).x >= 600) || (sfCircleShape_getPosition(c1).x < 0)) v.x *= -1;
    }

    sfRenderWindow_destroy(window);
    return 0;
}

int WinMain(void)
{
return main();
}

64bit

sfVector2f is struct with float x and float y;

When compile with NONE optimization - works fine.
When compile with optimization:

Building main.obj.
\main.c(26): error #3114: [asm] Invalid combination of opcode and operands.
*** Error code: 1 ***
Done.

problem is with code:

v.x *= -1;

Generally at try to change struct member var.

elmilenmch

  • Guest
Re: struct do not work
« Reply #1 on: January 06, 2016, 07:37:12 AM »
Here - minimal project (no external libraries or any other), which demonstrates the problem:

Windows 7, 64bit, 64bit Console Program:

Code: [Select]
#include <stdio.h>

typedef struct {float x; float y;} point;

int main(void)
{
  point p = {1, 0};

p.x *= -1;
printf("%f\n", p.x);
getchar();
}

- When compile with NONE optimization - works OK.
- When compile with any Optimization:

Building main.obj.
\Pelles C Projects\struct_problem1\main.c(9): error #3114: [asm] Invalid combination of opcode and operands.
\Pelles C Projects\struct_problem1\main.c(10): error #3114: [asm] Invalid combination of opcode and operands.
*** Error code: 1 ***
Done.

problem is at:

p.x *= -1;

Other is not error - for example: p.x = 3; works, but sign change: p.x = -p.x; do not work as upper code.

P.S. I attach the whole project folder as archive.

P.P.S. I see some similar problems with "struct" in the forum ever since 2013 year.

Snowman

  • Guest
Re: struct do not work
« Reply #2 on: January 06, 2016, 12:14:55 PM »
I confirm this bug in Pelles C 8.00.60 32-bit on Windows XP, however in my case there is no compilation error and the output is "nan".

It seems that when Optimizations are enabled, the compiler throws away the float initializers, and the data is left uninitialized. So when we add a dummy member that isn't float everything should work (or at least it does in my case).

Code: [Select]
#include <stdio.h>

typedef struct {float x; float y; int dummy;} point;

int main(void)
{
    point p = {1, 0};

    p.x *= -1;
    printf("%f\n", p.x);
}

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: struct do not work
« Reply #3 on: January 06, 2016, 01:14:14 PM »
For Pelle:
Wrong code ? mulss rax,dword [(@15) wrt rip]
Code: [Select]
[cvinit]
[global $mainCRTStartup]
[section .text]
[align 16]
#line 7 "C:\code\PellesC\Forum\elmilenmch\test_struct_1.c"
[function $mainCRTStartup]
$mainCRTStartup:
..?S$mainCRTStartup:
[cvproto 0 0x0 0x74 4]
[cvgproc $mainCRTStartup $mainCRTStartup ..?X$mainCRTStartup ..?S$mainCRTStartup ..?E$mainCRTStartup 0x1000]
[cvfldlist 2 'x' 0 0x40 'y' 4 0x40]
[cvstruct '12' 2 8 0x1001]
[cvreg $p 1025 0x1002]
[cvendp]
#line 8 "C:\code\PellesC\Forum\elmilenmch\test_struct_1.c"
#line 9 "C:\code\PellesC\Forum\elmilenmch\test_struct_1.c"
mov rax,0x3f800000
#line 11 "C:\code\PellesC\Forum\elmilenmch\test_struct_1.c"
mulss rax,dword [(@15) wrt rip]
#line 12 "C:\code\PellesC\Forum\elmilenmch\test_struct_1.c"
#line 13 "C:\code\PellesC\Forum\elmilenmch\test_struct_1.c"
cvttss2si eax,rax
..?E$mainCRTStartup:
ret
..?X$mainCRTStartup:
[section .rdata]
[alignb 4]
@15:
dd 0xbf800000
[section .drectve]
db " -defaultlib:crt64"
db " -defaultlib:kernel32"
May the source be with you