Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
108 user(s) are online (91 user(s) are browsing Forums)

Members: 0
Guests: 108

more...

Support us!

Headlines






signed char became unsigned ?
Home away from home
Joined:
2007/9/11 12:31
From Russia
Posts: 6821
@All

Have some theoretical question.

I do work on some port of a game, which is C++ and which have such a class:

class Door : public Object
{
    public :
        ...
balbalba...

    private :

        ...
balbalba...

        
char _direction;

        ...
balbalba...

};



Now, in the code of the game, depending on the needs, we have set _direction to be 1 or -1. Like this:

void Door::setDirection(core::vector3df ref)
{
    if (
_doorType == DT_ROTATE_X)
    {
        if (
ref._rotateCenter->getPosition().Z_direction 1;
        else 
_direction = -1;
    }
    else if (
_doorType == DT_ROTATE_Z)
    {
        if (
ref._rotateCenter->getPosition().X_direction = -1;
        else 
_direction 1;
    }
}


And when I build it for win32 and when puts prints with %d in relevant parts, it prints 1 or -1 when should.

But when I do the same on amigaos4, it prints then 1 and 255 instead of 1 and -1 (i.e. -1 became 255).

I.e. exactly the same code line per line. Just 2 binaries, one for aos4 and one for win32.

I feel it's something trivial, just can't get what. Does it look like signed char become unsigned? Maybe I need specially specify that this is signed? Or maybe some flag to GCC need to be passed, or it just bad code-practice somewhere?

Maybe in win32 pure "char" mean "signed char", but on amigaos4 pure "char" mean "unsigned char" ?:)

_________________
Join us to improve dopus5!
zerohero's mirror of os4/os3 crosscompiler suites
   Report Go to top

Re: signed char became unsigned ?
Home away from home
Joined:
2007/9/11 12:31
From Russia
Posts: 6821
Damn right, win32:

cat test.cpp
#include <stdio.h>
int main()
{
        
char _direction;
        
_direction = -1;
        
printf("_direction = %dn",_direction);
}

gcc test.cpp -o test
$ ./test
_direction 
= -1



Amigaos4:

2/10.Work:testcat test.cpp
#include <stdio.h>
int main()
{
        
char _direction;
        
_direction = -1;
        
printf("_direction = %dn",_direction);
}

2/10.Work:testgcc test.cpp -o test
2
/10.Work:test> ./test
2
/10.Work:test_direction 255


So seems just different defaults. Strange through why differences have a place at all between platforms in such case.

_________________
Join us to improve dopus5!
zerohero's mirror of os4/os3 crosscompiler suites
   Report Go to top

Re: signed char became unsigned ?
Just can't stay away
Joined:
2006/11/30 11:30
From Finland
Posts: 1799
@kas1e

Whether char type defaults to signed or unsigned is and always has been platform dependent. If you require a specific type of char (signed or unsigned) you should specify it in the variable definition.

In this case:
signed char _direction = -1;

   Report Go to top

Re: signed char became unsigned ?
Quite a regular
Joined:
2007/2/27 10:47
From Gravity well
Posts: 727
There should be a warning in gcc for that. It's *the* cross platform toolchain.

   Report Go to top

Re: signed char became unsigned ?
Just popping in
Joined:
2018/3/1 21:08
From italy
Posts: 26
These is a gcc compiler option about how to manage chars datatypes.
Maybe on windows defaults are different from others platforms.
As suggested you can declare var as signed char and look at debug printf output just to verify.

_________________
Memento audere semper!
   Report Go to top

Re: signed char became unsigned ?
Home away from home
Joined:
2007/9/11 12:31
From Russia
Posts: 6821
@All
As code is written like this, probably it's better to use --fsigned-char GCC option then? Or it may fix this part, but fail then in another...

_________________
Join us to improve dopus5!
zerohero's mirror of os4/os3 crosscompiler suites
   Report Go to top

Re: signed char became unsigned ?
Just popping in
Joined:
2011/7/20 20:01
From In the sticks
Posts: 136
@kas1e

-fsigned-char should do it for you in GCC.

   Report Go to top





[Advanced Search]



Powered by XOOPS 2.0 © 2001-2016 The XOOPS Project