如何创建主函数中的局部变量? [英] How to create local variables inside the main function?
问题描述
我知道如何将参数传递到用户定义的函数,以及如何创建这样的函数内部的局部变量。但我想要的是main函数创建的局部变量。
所以,主要的功能是,当程序启动时,但什么是ESP当主开始执行的初始值执行的第一件事情?即什么是对,当主开始执行堆栈顶部,它是命令行参数?
如果我想创建一个局部变量里面主要的,我应该保存的ESP值存入EBP,然后我是多么需要的数据,就像我一个用户定义的函数中做递增ESP?
所以,主要的功能是,当程序启动时,但什么是ESP当主开始执行的初始值执行的第一件事情?即什么是对,当主开始执行堆栈顶部,它是命令行参数?
块引用>
主
被称为一个正常功能,所以(与CDECL
调用约定),最上面的事情,从顶部到底部,(可选)环境指针,则指针参数字符串指针数组,那么ARGC
,然后返回地址主
。
如果我想创建一个局部变量里面主要的,我应该保存的ESP值存入EBP,然后我是多么需要的数据,就像我一个用户定义的函数中做递增ESP?
块引用>
主
是用户的功能。它是由的crt0.o
称为从code大致是这样的(名称可能因操作系统不同):无效
_start(无效)
{
/ *跳过初始化* /
INT RV =主(newargc,newargv,ENVIRON);
do_global_dtors();
出口(RV);
/* 还没到 */
}所以,TL;博士:是的,
(请注意,即使
_start
有一个有效的堆栈指针,但通常没有回信地址,所以它最终必须调用退出
系统调用)。I know how to pass parameters to a user-defined function and how to create local variables inside such function. But what I want is to create local variables for the main function.
So the main function is the first thing that executes when the program starts, but what is the initial value of esp when main starts executing? i.e what is on top of the stack when main starts executing, is it the command line arguments?
If I want to create local variables inside main, should I save the value of esp into ebp and then increment esp by how much data I need just like I do inside of a user-defined function?
解决方案So the main function is the first thing that executes when the program starts, but what is the initial value of esp when main starts executing? i.e what is on top of the stack when main starts executing, is it the command line arguments?
main
is called as a normal function, so (withcdecl
calling convention), the topmost things are, from the top to the bottom, (optionally) the environment pointer, then the pointer to the argument string pointer array, thenargc
, then the return address ofmain
.If I want to create local variables inside main, should I save the value of esp into ebp and then increment esp by how much data I need just like I do inside of a user-defined function?
main
is a user function. It is called fromcrt0.o
(name may differ depending on the operating system) from code roughly like this:void _start(void) { /* initialisation skipped */ int rv = main(newargc, newargv, environ); do_global_dtors(); exit(rv); /* NOTREACHED */ }
So, tl;dr: yes.
(Note that even
_start
has a valid stack pointer, but usually no return address, so it must eventually call theexit
syscall.)这篇关于如何创建主函数中的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!