printf Bug
#1

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.
Reply
#2

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

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.
Reply
#4

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

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?!
Reply
#6

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

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?!
Reply
#8

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'?
Reply
#9

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

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)