SA-MP Forums Archive
printf Bug - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP (https://sampforum.blast.hk/forumdisplay.php?fid=3)
+--- Forum: Bug Reports (https://sampforum.blast.hk/forumdisplay.php?fid=20)
+--- Thread: printf Bug (/showthread.php?tid=540686)



printf Bug - Kaliber - 06.10.2014

Well actually there is a Bug in this function.

Look at this Code-snippet:

Код:
#include <a_samp>

new string[2]; //The size isn't matter

main() 
{
    for(new i; i<sizeof string; i++) string[i] = 'a';
    printf("|%s|",string);
}
You should expect, that the output print is:

Код:
|aa|
But it isn't. Its actually:

Код:
|aa|%s||
Well, its not such a hard bug, but maybe u can fix it anyway

mfg.


Re: printf Bug - Vince - 06.10.2014

No, you're overwriting the null terminator. Thus the compiler can't tell where the string ends and pops out.


Re: printf Bug - IJzerenRita - 06.10.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
No, you're just overwriting the null terminator. Thus the compiler can't tell where the string ends and pops out.
Not the compiler. The printf function looks for the terminator, but as OP did not include it, it will read onto into the memory next to it. This produces the unexpected results. But to be clear: this is a runtime error, not a compiler error.


Re: printf Bug - KingHual - 06.10.2014

totally printf's fault that you haven't read up on C strings bro


AW: Re: printf Bug - Kaliber - 06.10.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
No, you're overwriting the null terminator. Thus the compiler can't tell where the string ends and pops out.
I mean so the Code runs:

Код:
#include <a_samp>

main()
{
    new string[2];
    for(new i; i<sizeof string; i++) string[i] = 'a';
    printf("|%s|",string);
}
That Prints |aa|

But maybe you are right and i overwrite the null-char..but why it runs local?!


Re: printf Bug - Vince - 06.10.2014

Index 2 does not exist. You only have 0 and 1 and you're overwriting them both.


AW: Re: printf Bug - Kaliber - 06.10.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
Index 2 does not exist. You only have 0 and 1 and you're overwriting them both.
Yes, thats true.

But Local it runs...and global not?!


Re: AW: Re: printf Bug - KingHual - 06.10.2014

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Thats not true?

Код:
new string[2];
0='a'|1='a'|2='\0'
Thats all right?!

I mean so the Code runs:

Код:
#include <a_samp>

main()
{
    new string[2];
    for(new i; i<sizeof string; i++) string[i] = 'a';
    printf("|%s|",string);
}
That Prints |aa|

But maybe you are right and i overwrite the null-char..but why it runs local?!
new var[2]; declares an array with 2 cells, where exactly is there room for the null character if you're setting the 2 cells to 'a'?


Re: AW: Re: printf Bug - IJzerenRita - 06.10.2014

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
But Local it runs...and global not?!
Ahem.

Quote:
Originally Posted by IJzerenRita
Посмотреть сообщение
This produces the unexpected results.



Re: printf Bug - Anzipane - 06.10.2014

It's a run-time error, you're writing over the string terminator character at index 1, giving unexpected results at run-time.