Char Arrays [Solved]

Posts 1–9 of 9 · Page 1 of 1
Char Arrays [Solved]
Why is it that storing more then the initilized number or members of an array will not cause problems?
Code:
    char username[16];
    char password[100];
    print("welcome to mc_bot 1.0!");
    printf("username: ");
    scanf("%s", username);
if i enter a username longer then 16 characters, it will still work as normal. WHy?
Look up buffer overflows. Once you exceed 16 bytes it continues writing over whatever memory is after that, which in your case is the bytes for 'password'.
Quote Originally Posted by Void View Post
Look up buffer overflows. Once you exceed 16 bytes it continues writing over whatever memory is after that, which in your case is the bytes for 'password'.
Actually, allocation size is up to the compiler and also depends on the settings, e.g., memory alignment and debug build. So its not certain that the data written will actually overflow into "password", unless you write a ridiculous amount of data.
that makes sense.
I was expected more of a run time error or something that i could visually see
Quote Originally Posted by kibbles18 View Post
that makes sense.
I was expected more of a run time error or something that i could visually see
This is very visual, the way you declared your variables alone should be visual enough. Variables are given a name, and all it is is a name. Once in memory it's one big chunk.
Quote Originally Posted by kibbles18 View Post
that makes sense.
I was expected more of a run time error or something that i could visually see
C++ doesn't do any runtime checks (though MS have extensions that let you check for this). This is the beauty of C++, in my opinion -- you get to control what happens at a very down-to-Earth manner. Nothing is "assumed" - it does what you tell it to do.
Quote Originally Posted by freedompeace View Post


C++ doesn't do any runtime checks (though MS have extensions that let you check for this). This is the beauty of C++, in my opinion -- you get to control what happens at a very down-to-Earth manner. Nothing is "assumed" - it does what you tell it to do.
Assembly is even more so like this.
Quote Originally Posted by Void View Post
Assembly is even more so like this.
In assembly the code runs you ! (*reads in a Russian accent)
Quote Originally Posted by .::SCHiM::. View Post
In assembly the code runs you ! (*reads in a Russian accent)
Hahahaha! Good laugh!
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us