凡在内存中保存在c我的变量? [英] Where in memory are my variables stored in c?

查看:154
本文介绍了凡在内存中保存在c我的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是考虑到内存分为四个部分:数据,堆,栈,code,其中做全局变量,静态变量,常量数据类型,局部变量(定义和函数声明),变量(在主函数),指针和动态分配空间(使用malloc,释放calloc)获得存储在内存中?

By considering that the memory is divided into four segments: data, heap, stack, and code, where do global variables, static variables, constant data types, local variables (defined and declared in functions), variables (in main function), pointers, and dynamically allocated space (using malloc,calloc) get stored in memory?

我想他们会作如下分配​​:

I think they would be allocated as follows:


  • 全局变量------->数据

  • 静态变量------->数据

  • 常量数据类型-----> code

  • 局部变量(声明和功能定义)-------->栈

  • 宣布,以主函数中定义的变量----->堆

  • 指针(例如:字符*改编,诠释* ARR)------->堆

  • 动态分配空间(使用malloc,释放calloc)-------->栈

我只是从C角度指的是这些变量。

I am referring to these variables only from the C perspective.

请纠正我,如果我错了,因为我是新手到C

Please correct me if I am wrong as I am newbie to C.

推荐答案

您得到了其中的一些权利,但谁写的问题,骗你上至少有一个问题:

You got some of these right, but whoever wrote the questions tricked you on at least one question:


  • 全局变量------->数据(正确的)

  • 静态变量------->数据(正确的)

  • 常量数据类型-----> code和/或数据。考虑当一个恒定本身将被存储在数据段的情况字符串,并引用将它嵌入在code

  • 局部变量(声明和功能定义)-------->栈(正确的)

  • 变量声明和功能-----> <击>堆还栈(老师试图欺骗你)

  • 指针(例如:的char *改编为int *改编)-------> <罢工>堆数据或堆栈,根据上下文。 ç让你声明一个全局或静态指针,在这种情况下指针本身将数据段结束。

  • 动态分配空间(使用的malloc 释放calloc 的realloc )--------> <击>栈

  • global variables -------> data (correct)
  • static variables -------> data (correct)
  • constant data types -----> code and/or data. Consider string literals for a situation when a constant itself would be stored in the data segment, and references to it would be embedded in the code
  • local variables(declared and defined in functions) --------> stack (correct)
  • variables declared and defined in main function -----> heap also stack (the teacher was trying to trick you)
  • pointers(ex: char *arr, int *arr) -------> heap data or stack, depending on the context. C lets you declare a global or a static pointer, in which case the pointer itself would end up in the data segment.
  • dynamically allocated space(using malloc, calloc, realloc) --------> stack heap

值得一提的是,堆叠正式称为自动存储类。

It is worth mentioning that "stack" is officially called "automatic storage class".

这篇关于凡在内存中保存在c我的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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