Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
102 user(s) are online (52 user(s) are browsing Forums)

Members: 1
Guests: 101

Chris, more...

Headlines

 
  Register To Post  

signed char became unsigned ?
Home away from home
Home away from home


See User information
@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!
AmigaOS4 on youtube
Go to top
Re: signed char became unsigned ?
Home away from home
Home away from home


See User information
Damn right, win32:

cat test.cpp
#include <stdio.h>
int main()
{
        
char _direction;
        
_direction = -1;
        
printf("_direction = %d\n",_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 = %d\n",_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!
AmigaOS4 on youtube
Go to top
Re: signed char became unsigned ?
Just can't stay away
Just can't stay away


See User information
@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;

Go to top
Re: signed char became unsigned ?
Quite a regular
Quite a regular


See User information
There should be a warning in gcc for that. It's *the* cross platform toolchain.

Go to top
Re: signed char became unsigned ?
Just popping in
Just popping in


See User information
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!
Go to top
Re: signed char became unsigned ?
Home away from home
Home away from home


See User information
@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!
AmigaOS4 on youtube
Go to top
Re: signed char became unsigned ?
Just popping in
Just popping in


See User information
@kas1e

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

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project