简体   繁体   English

为什么在调试时初始化函数中的字符串不能像int那样工作

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

So I tried debugging some simple C programs today ; 所以今天我尝试调试一些简单的C程序;

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 ! 因为我需要8个字节在当前堆栈帧中存储a和b,因为它们是局部变量!

But when I try the same with Strings say 但是当我用Strings尝试相同时

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 0DCh是220,但是由于字符串只有12个字节长,因此不应

sub esp,0DCh sub esp,0DCh

be

sub esp,00ch 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 任何人都可以共享一些有关如何将字符串存储在内存中并稍后通过汇编程序[preferebly指令]进行访问的链接,例如,如果字符串问候语的长度很大,那么该字符串如何存储在内存中,因为我们无法将所有字符串存储在堆栈本身中

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 正如@ user3386109所指出的那样,问题是防止溢出,Visual Studio中启用了默认安全检查,并且它提供了额外的空间以防止溢出,因此将其关闭,使编译器仅分配12个字节:D

To turn this security measure ( Buffer Security Checks GS) off Project settings -> C/C++ -> Code generation -> security check = disable GS 要关闭此安全措施(缓冲区安全检查GS),请关闭项目设置-> C / C ++->代码生成->安全检查=禁用GS

Some post related to GS 一些与GS相关的帖子

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 队列初始化功能不起作用 - Queue initializing function doesn't work 为什么在反转 SLL 时将 **head 发送到函数工作,而 *head 在 C 中不起作用? - Why does sending **head to a function work while reversing an SLL and *head doesn't in C? 为什么类型int可与sscanf一起使用,而int16_t不起作用? - Why does type int work with sscanf but int16_t doesn't? 为什么该函数与float一起正常工作,但对double却不工作? - Why does the function work with float properly but doesn't work with double? alarm(int)在while循环中不起作用 - alarm(int) doesn't work in a while loop 在这段代码中,为什么Write()不能与Int一起使用? - In this code Why Write() doesn't work with Int? 在初始化指针数组时,为什么只有字符串不显示警告? - In initializing pointer array, why only string doesn't show warning? 为什么getchar()不起作用,但是getchar_unlocked()在读取字符串字符时却在主函数之外呢? - Why getchar() doesn't work but getchar_unlocked() do outside main function while reading string character wise? 为什么我的程序不能使用float变量,但是可以使用int变量? - Why doesn't my program work with float variables, but it does with int variables? 代码不起作用。 但是在调试时会 - Code doesn't work. But does when debugging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM