C:C程序执行的内存布局 [英] C : Memory layout of C program execution

查看:97
本文介绍了C:C程序执行的内存布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在内核简单的C程序提供存储器。

I wanted know how the kernel is providing memory for simple C program .

例如:

#include<stdio.h> 
#include<malloc.h> 
int my_global = 10 ; 
main() 
{ 
char *str ; 
static int val ; 
str = ( char *) malloc ( 100 ) ; 
scanf  ( "%s" , str ) ;
printf( " val:%s\n",str ) ;
free(str) ;
return 1 ;
}

见,在这个项目中我用静态的,全球性的malloc分配动态内存
那么,怎样的内存布局将是...?
任何一个给我的网址,这将有对这个过程的详细信息。

See, In this program I have used static , global and malloc for allocating dynamic memory So , how the memory lay out will be ... ? Any one give me url , which will have have details information about this process..

推荐答案

非常基本,内置目标ELF(可执行和链接格式)C程序,如那些建立在Linux中有是创建了一个标准的内存布局。类似的布局可能存在于其他架构,但我不知道够多的给大家介绍一下他们。

Very basically, in C programs built to target ELF (Executable and Linkable Format) such as those built on linux there is a standard memory layout that is created. Similar layouts probably exist for other architectures, but I don't know enough to tell you more about them.

但是也有一些在低内存地址在内存初始化一些全局数据段(如当前执行code段,全球数据,以及与创建的字符串.. 你的C code内)。​​

There are some global data sections that are initialized at low memory addresses in memory (such as sections for the currently executing code, global data, and any strings that are created with "..." inside your C code).

下面,有可以使用的开放存储器堆。这堆增加自动调用malloc和free举动所谓内存中的程序中断向高地址的大小。

Below that there is a heap of open memory that can be used. The size of this heap increases automatically as calls to malloc and free move what is called the "program break" to higher addresses in memory.

在内存的高地址开始,堆栈增长向低地址。该堆栈包含存储任何本地分配的变量,例如那些在功能上还是范围( {...} )。

Starting at a high address in memory, the stack grows towards lower addresses. The stack contains memory for any locally allocated variables, such as those at the top of functions or within a scope ({ ... }).

有一个运行程序ELF一个很好的说明这里和上维基百科的文章。如果你想要一个编译器如何去约翻译C $ C $ç成一个例子组装你可以看看GCC,他们的内幕手册有一些有趣的东西在里面;最相关的部分可能是那些在第17章中,特别是17.10,17.19和17.21。最后,英特尔有一个关于内存布局大量的信息在其 IA-32架构软件开发人员手动。它描述了英特尔处理器如何处理内存分割和协议栈的创建,等等。有没有关于ELF的细节,但是可以看到两个搭配起来的地方。最有用的位大概1卷的3.3节:基本架构,并卷3A第3章:系统编程指南,第1部分

There is a good description of a running ELF program here and more details on the format itself on the Wikipedia article. If you want an example of how a compiler goes about translating C code into assembly you might look at GCC, their Internals Manual has some interesting stuff in it; the most relevant sections are probably those in chapter 17, especially 17.10, 17.19 and 17.21. Finally, Intel has a lot of information about memory layout in its IA-32 Architectures Software Developer’s Manual. It describes how Intel processors handle memory segmentation and the creation of stacks and the like. There's no detail about ELF, but it's possible to see where the two match up. The most useful bits are probably section 3.3 of Volume 1: Basic Architecture, and chapter 3 of Volume 3A: System Programming Guide, Part 1.

我希望这可以帮助任何人在深入运行的C程序的内部结构,祝你好运。

I hope this helps anyone diving into the internals of running C programs, good luck.

这篇关于C:C程序执行的内存布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆