Why does initializing a string in a function doesn't work like int while debugging

安稳与你 提交于 2019-12-23 03:53:08

问题


So I tried debugging some simple C programs today ;

First one being

int main(){

 int a ,b ;
 return 0 ;

}

Which when de-compiled gave me

  push        ebp  
  mov         ebp,esp  
  sub         esp,008h 

because I need to have 8 bytes to store a and b in the current stack frame since they are local variable !

But when I try the same with Strings say

int main() {

    char greeting[12] = "Pwnit2Ownit";
    return 0;
}

Which when de-compiled gave me

 push        ebp  
  mov         ebp,esp  
  sub         esp,0DCh 

0DCh is 220 , But since the string is only 12 bytes long shouldn't the

sub esp,0DCh

be

sub esp,00ch

instead ?

And can anyone share some links on how the strings are stored in the memory and accessed later via assembly [preferebly instruction] , like hows the string greetings stored in memory if it's length is large since we can't store all in the stack itself


回答1:


As @user3386109 pointed out , The issue is to prevent overflow the default security check in visual studio is enabled , and it provides extra space in order to prevent overflows , so turning it off , made the compiler allocate only 12 bytes :D

To turn this security measure ( Buffer Security Checks GS) off Project settings -> C/C++ -> Code generation -> security check = disable GS

Some post related to GS

http://preshing.com/20110807/the-cost-of-buffer-security-checks-in-visual-c/



来源:https://stackoverflow.com/questions/37309076/why-does-initializing-a-string-in-a-function-doesnt-work-like-int-while-debuggi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!