本地静态和局部变量内存分配 [英] memory allocation for local static and local variable

查看:124
本文介绍了本地静态和局部变量内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1

void main(void)
{
  int *ptr1;
  ptr1 = (int *)malloc(..);
}

2

void main(void)
{
  static int *ptr2;
  ptr2 = (int *)malloc(..);
}

我想问的是如何分配的内存ptr1的&放完成的; PTR2?

I want to ask how is memory allocation done for ptr1 & ptr2?

推荐答案

ptr1的指针本身在栈上分配。 ptr1的指向的堆内存。

The ptr1 pointer itself is allocated on the stack. ptr1 points to memory on the heap.

PTR2 指针本身是在程序启动时分配的(前被调用),并是全球性的,但恰好仅在可见,因为它是在其范围内声明。 PTR2 指向的内存堆也是如此。

The ptr2 pointer itself is allocated on program startup (before main is invoked) and is global but just happens to be visible only in main because it is declared in its scope. ptr2 points to memory on the heap as well.

声明 PTR2 之外只会使它在它下面的所有功能可见,但其存储会相同。

Declaring ptr2 outside of main would only make it visible in all functions below it, but its storage will be the same.

这篇关于本地静态和局部变量内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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