动态分配内存(堆),本地到一个函数,或者线程中的所有函数都可以访问它,即使没有传递指针作为参数 [英] Is dynamically allocated memory (heap), local to a function or can all functions in a thread have access to it even without passing pointer as an argument

查看:235
本文介绍了动态分配内存(堆),本地到一个函数,或者线程中的所有函数都可以访问它,即使没有传递指针作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的问题,需要帮助。
我试图理解动态分配的内存的范围(堆上)。

  #include< stdio.h> 
#include< malloc.h>

// ----- Struct def -------
struct node {
int x;
int y;
};

// ------ GLOBAL DATA ------

// ----- FUNC定义----
void funct(){
t-> x = 5; // **我可以访问main中的heap上分配的't'吗?**
t-> y = 6; // **可以访问main中的heap上分配的't'吗?**
printf(int x =%d\\\
,t-> x);
printf(int y =%d\\\
,t-> y);
return;
}

// ----- MAIN FUNCTION ------
int main(void){
struct node * t = NULL; / /如果我在main()之外定义
//作为全局指针变量
t =(struct node *)malloc(sizeof(struct node)),它将会有什么不同?
t-> x = 7;
t-> y = 12;
printf(int x =%d\\\
,t-> x);
printf(int y =%d\\\
,t-> y);

funct(); // FUNCTION CALLED **
return 0;
}

所以,我可以在funct()中访问结构't'内存分配在main()中,而不会传递参数(指向t的函数到函数funct) - 因为堆是一个线程共有的?如果在main()之外定义struct node * t = NULL作为全局变量,它会有什么不同,有什么问题吗?谢谢

解决方案

当您使用malloc()时,返回的内存可以在代码的任何位置访问,假设您可以看到具有malloc()返回的指针的变量。



所以在你的代码中,如果t是全局的,它将在main和funct()中可见,是的,你可以在两者中使用它。



正如以前的答案所述,funct()不知道是什么,因为声明和定义的主要; t超出了功能范围。你分配给t 的记忆将会在功能中使用,如果功能知道是什么。


I have a very basic question and need help. I am trying to understand what is the scope of a dynamically allocated memory (on heap).

#include <stdio.h>
#include <malloc.h>

//-----Struct def-------
struct node {
        int x;
        int y;
};

//------GLOBAL DATA------

//-----FUNC DEFINITION----
void funct(){
  t->x = 5; //**can I access 't' allocated on heap in main over here ?**
  t->y = 6; //**can I access 't' allocated on heap in main over here ?**
  printf ("int x = %d\n", t->x);
  printf ("int y = %d\n", t->y);
  return;
}

//-----MAIN FUNCTION------
int main(void){
      struct node * t = NULL;// and what difference will it make if I define 
                                 //it outside main() instead- as a global pointer variable
          t = (struct node *) malloc (sizeof(struct node));
      t->x = 7;
      t->y = 12;
      printf ("int x = %d\n", t->x);
      printf ("int y = %d\n", t->y);

    funct(); // FUNCTION CALLED**
    return 0;
}

So, here can I access structure 't' in funct() even though the memory is allocated in main() without passing argument (pointer to t to function funct) - since heap is common to a thread ? And what difference will it make if I define struct node * t = NULL outside of main() as a global variable and is there anything wrong with it ? Thanks

解决方案

When you use malloc(), the memory returned by that can be accessed anywhere in your code, assuming that you can see the variable which has the pointer returned by malloc().

So in your code, if t was global, it would be visible in main and in funct(), and yes, you could use it in both.

As it is, as previous answers have mentioned, funct() has no idea what t is, because the declaration and definition of t are in main; t is out of scope in funct. The memory you've allocated onto t would be useable in funct, if funct knew what t was.

这篇关于动态分配内存(堆),本地到一个函数,或者线程中的所有函数都可以访问它,即使没有传递指针作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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