I mean, WTF...

You can talk about almost anything that you want to on this board.

Moderator: Moderators

Post Reply
WedNESday
Posts: 1236
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

I mean, WTF...

Post by WedNESday » Fri Nov 14, 2008 3:28 pm

Code: Select all

if (CPU.A16 < 0x10)
	sprintf(Output, "A: 0x000%X", CPU.A16);
...won't work but...

Code: Select all

if (CPU.A16 < 0x10)
	sprintf(Output, "A: 0x0%X", CPU.A16);
...just because I remove those two 0's. It puts the correct data into Output, but TextOut just returns 'The paramenter is incorrect.'.

I mean, WTF?

tepples
Posts: 21755
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples » Fri Nov 14, 2008 3:34 pm

How big is Output?

And what are you doing when A16 >= 0x10? I have a feeling what you're trying to do in the sprintf has something to do with 0x%02X or 0x%04X.

WedNESday
Posts: 1236
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Post by WedNESday » Fri Nov 14, 2008 3:37 pm

Well I changed char *Output = ""; to char Output[100]; and now it works. Thanks anyway.

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg » Sat Nov 15, 2008 6:49 am

Attempting to write to a string literal is undefined behavior. Doesn't your compiler warn when you try to assign a string literal to a non-const char pointer? By being const-correct, the compiler can diagnose the error for you:

Code: Select all

char* p = "foo"; // error in C++, and C compiler should warn
*p = 'b'; // will compile, but cause UB when executed
sprintf( p, "bar" ); // same, causes UB

const char* cp = "foo"; // OK
*cp = 'b'; // error because cp points to const object
sprintf( cp, "bar" ); // error because sprintf expects pointer to non-const char

Post Reply