|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
c++.dos - size with an array of char
#include <stdio.h>
main()
{
char ar[9];
ar[10] ='T';
printf("%c",ar[10]);
}
Output > T
Why I got "T" in output and not an error ?
Aug 29 2003
Because that's exactly what you asked the program to do. You created an array bounds violation. C does not care about that. It just means you are accessing memory that you have not declared. Jan noobi wrote: Aug 29 2003
In addition, you are only using a small amount of stack for the array and the stack grows upwards (or downwards depending on your point of view) for the character array and hasn't hit the "roof". Printf happens to be a varargs and therefore the array argument is on top of stack. if you include this: extern "C" unsigned __cdecl _stack = 32; // or some smaller number // which I haven't tried to see which causes failures You might get a crash. If you read the news lately, this is exactly the scenerio of what we called the buffer overflow. Your "buffer" of 9 character is "overflowed". Attacker can use these sort of programmer's flaw to do something nasty "Jan Knepper" <usenet digitalmars.com> wrote in message news:biov51$sl7$1 digitaldaemon.com...Because that's exactly what you asked the program to do. You created an array bounds violation. C does not care about that. It just means you are accessing memory that you have not declared. Jan noobi wrote: Aug 31 2003
KarL schrieb...n addition, you are only using a small amount of stack for the array and the stack grows upwards (or downwards depending on your point of view) for the character array and hasn't hit the "roof". Printf happens to be a varargs and therefore the array argument is on top of stack. if you include this: extern "C" unsigned __cdecl _stack = 32; // or some smaller number // which I haven't tried to see which causes failures You might get a crash. Sep 01 2003
See sample: http://www.securiteam.com/securityreviews/5OP0B006UQ.html "Heinz Saathoff" <hsaat bre.ipnet.de> wrote in message news:MPG.19bd2bc5421c07909896d1 news.digitalmars.com...KarL schrieb...n addition, you are only using a small amount of stack for the array and the stack grows upwards (or downwards depending on your point of view) for the character array and hasn't hit the "roof". Printf happens to be a varargs and therefore the array argument is on top of stack. if you include this: extern "C" unsigned __cdecl _stack = 32; // or some smaller number // which I haven't tried to see which causes failures You might get a crash. Sep 01 2003
|