+ 1
Whats overflowing in the memory there ? Put 18 chars in and something weird happens. Can anyone logically explain, why ?
5 Respostas
+ 6
You're overwriting the return pointer (in the stack). That's how this code works:
https://code.sololearn.com/cYgU9YGHlfD1/?ref=app
You can make yours do the same thing by this line after gets():
a[18]=1; // max entry is 11 chars now; the pointer has to remain mostly intact. No input works fine.
This causes main to return backwards halfway into a multibyte instruction (this split just happens to be valid) then fall through main() again, forever.
Actually, if you remove the printf, you'll run out of memory but that's a longer explanation.
+ 4
'gets()' doesn't do any bounds checking,It cant tell if you entered a number of strings exceeding what's expected,thus you get a memory leak..Use fgets(a,10,stdin) instead.
+ 2
Yes i know this, i gave some stuff wich is too big in an memoryspace which is to small, but what im interested in is knowing what's going on there in the memory ? Why does it behave like this exactly. What am i overflowing ? Heap, Stack, buffer or which kind of magic is happening there ?
+ 2
[meta, reposted to clarify] By the way, compilers can enable a stack "security cookie" to guard against stack overflows with the proper compiler flag.
Cookies are autogenerated and corrupting the cookie auto-kills your app -- but they're optional.
+ 1
Nice to know, but it is very interesting to do things like that, thanks for the answer.
Your explanation is really good and helpfull. Would nice to disassemble your program, to see exactly what is happening there. (Another reason to make an assembly tutorial in sololearn :) )
Thanks for that good explanation.
3E2B thumbs up !!