指针和全局变量在C中的位置 [英] Location of pointers and global variables in C

查看:84
本文介绍了指针和全局变量在C中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 中的指针全局变量存储在哪里?它们是保存在内存,堆还是堆栈中?

Where are pointers and global variables stored in C? Are they saved in the memory, heap or stack?

推荐答案

全局变量可以位于几个位置,具体取决于它们的设置方式-例如,const全局变量可以位于只读部分可执行文件. 普通"全局变量位于可执行文件的读写部分中.它们根本不在堆或堆栈上.指针只是变量的一种,因此它们可以位于您想要的任何位置(如果您使用malloc(),则在堆上;如果它们是局部变量,则在堆栈上;如果是全局变量,则在数据部分中)

Global variables can be in a couple places, depending on how they're set up - for example, const globals may be in a read-only section of the executable. "Normal" globals are in a read-write section of the executable. They're not on the heap or the stack at all. Pointers are just a type of variable, so they can be wherever you want them to be (on the heap if you malloc() them, on the stack if they're local variables, or in the data section if they're global).

这篇关于指针和全局变量在C中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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