Home > Back-end >  Dynamically allocated memory problems
Dynamically allocated memory problems

Time:09-27

Const int siz=5;
Char * ch1=new char [siz];//dynamically allocated memory
cin> Ch1;
coutSuch as input: sdadfdsf, output is also sdadfdsf
I input the number is more than 5 characters, the end result will be all of its output, not should be prompt said input errors or output only five characters?
Do there who can help me explain clearly, thank you?

CodePudding user response:

This problem involves the allocation of memory
In a computer, you apply for 5 bytes of memory, in system memory, as long as there is space behind and is not used, it can be used more than five space
But doing so is safe is not stable, you are already the subscript overflow, in the development. The result of the subscript overflow is "unexpected", that is just the result could be right
But you may get unexpected results

CodePudding user response:

Buffer is too small, the code is not safe, buffer overflow, easy to enter a long carefully constructed of unsafe process of machine code instructions and cover part of the code from the code segment, and then programs a jump instruction to perform unsafe code covered in attack, although modern operating systems and programming languages have some mechanism to guard against, but the buffer overflow is may lead to attack, if you write the code is on the order of the system software,

CodePudding user response:

Thank you very much for the reply to the third floor on the second floor, now see,
  • Related