我的数据是存储在堆中还是在下面的代码中的堆栈中

发布于 2025-01-13 09:35:43 字数 335 浏览 3 评论 0原文

在下面的代码中,我通过malloc获取内存并通过指针ptr指向它。当我如图所示分配值时,我将数据存储在指针中,我们知道指针位于堆栈帧中。

所以我的问题是:我的数据(整数)存储在堆栈中还是堆中?

#include<stdio.h>
#include<stdlib.h>
void main()
{
  int *ptr;
  ptr=(int*)malloc(5*sizeof(int));
  ptr[0]=5;
  ptr[1]=6;
  ptr[2]=8;
  ptr[3]=10;
  ptr[4]=11;
}

In the below code, I get memory through malloc and pointed it by pointer ptr. When I assigned value as shown I stored the data in pointer and we know that pointer is located in stack frame.

So my question is: My data(integers) is stored in stack or in heap?

#include<stdio.h>
#include<stdlib.h>
void main()
{
  int *ptr;
  ptr=(int*)malloc(5*sizeof(int));
  ptr[0]=5;
  ptr[1]=6;
  ptr[2]=8;
  ptr[3]=10;
  ptr[4]=11;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2025-01-20 09:35:43

是的,指针存储在栈上,但它指向的内存却在堆上。因此,整数存储在堆上。


编辑回答评论中的问题:

为什么我们不使用 * 像 *ptr[0]=5;这意味着该地点有 5 家商店
ptr[0] 指向的地方 所以,我的问题是为什么我们不使用 *before
指针

在 C 语言中,数组访问是根据指针定义的。所以数组元素访问ptr[0]可以写成*ptr。要访问数组的元素n,可以编写ptr[n]*(ptr+n)

如果您的数组存储指针并且您想要访问该值,则可以使用*ptr[0]。您也可以将其视为二维数组。换句话说,*ptr[0] 相当于ptr[0][0]

来自 C 标准

后缀表达式,后跟方括号 [] 中的表达式
是数组对象元素的下标名称。这
下标运算符 [] 的定义是 E1[E2] 等于
(*((E1)+(E2)))。由于适用于的转换规则
二元 + 运算符,如果 E1 是一个数组对象(相当于一个指针
到数组对象的初始元素)并且 E2 是一个整数,
E1[E2]表示E1的第E2个元素(从零开始计数)。

另请参阅此问题:数组名称是指针吗?

Yes, the pointer is stored on the stack, but the memory it points to is on the heap. Therefore, the integers are stored on the heap.


Edit to answer the question from the comments:

Why we didn't use * like *ptr[0]=5; this means 5 store at the location
where ptr[0] is pointing So, my question is why we didn't use *before
ptr

In C, an array access is defined in terms of pointers. So the array element access ptr[0] can be written as *ptr. To access element n of the array, you can write ptr[n] or *(ptr+n).

You would use *ptr[0] if your array stored pointers and you wanted to access the value. You can also think of that as a two-dimensional array. In other words, *ptr[0] is equivalent to ptr[0][0].

From the C standard:

A postfix expression followed by an expression in square brackets []
is a subscripted designation of an element of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))). Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero).

Also see this question: Is an array name a pointer?

愚人国度 2025-01-20 09:35:43

迂腐地说,从 C 标准的角度来看,这里涉及的所有对象只是占用一些适当对齐的内存,并且与其他对象脱节。

虽然栈和堆的概念很常见,但 C 标准没有提及这两个术语。数据的存储方式和位置主要是由实现定义的。

区分它们的主要因素是它们的可见性和有效生命周期(请参阅:存储持续时间生命周期范围):

  • ptr 的使用是在 main 返回之前有效。

  • malloc返回的地址处使用内存是有效的直到它被释放


通常是公平的

  • 然而,实际上来说,将具有自动存储持续时间的对象称为存在于“堆栈”上的
  • 静态 存储持续时间存在于“数据段”中,
  • 分配存储持续时间存在于“堆”中,

因为这是 C 实现的一种非常常见的工作方式。

使用这个术语,由标识符ptr指定的对象存在于堆栈上,并且通过malloc返回的指针访问的对象存在于堆栈上。

Pedantically speaking, from the perspective of the C standard, all the objects involved here simply occupy some memory that is suitably aligned, and disjointed from other objects.

While the notion of a stack and heap are commonplace, the C standard makes no reference to either terms. How and where data is stored is mostly implementation-defined.

The principle thing differentiating them is their visibility and effective lifetimes (see: Storage duration, Lifetime, and Scope):

  • The use of ptr is valid until main returns.

  • The use of the memory at the address returned by malloc is valid until it is deallocated.


Practically speaking, however, it is generally fair to refer to objects with

  • automatic storage duration as existing on the "stack",
  • static storage duration as existing in the "data segment", and
  • allocated storage duration as existing on the "heap",

as this a very common way C implementations work.

With this terminology, the object designated by the identifier ptr exists on the stack, and the objects accessed via the pointer returned by malloc exist on the heap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文