简体   繁体   English

在C中:为什么在函数之外存在堆栈分配结构?

[英]in C: Why does a stack allocated structure exist outside of the function?

my function: 我的功能:

struct hostent * gethost(char * hostname){
    if(/*some condition under which I want 
         to change the mode of my program to not take a host*/){
       return null
    }
    else{
        struct hostent * host = gethostbyname(hostname);
        return host;
    }
}

in main: 在主要:

struct hostent * host = gethost(argv[2]);

(ignore any minor errors in the code, I'm spewing from memory) (忽略代码中的任何小错误,我从内存中喷出)

this works fine. 这很好用。 and Valgrind doesn't tell me I'm losing memory, despite the fact I'm not freeing. 尽管事实上我没有自由,但Valgrind并没有告诉我,我正在失去记忆。

Why? 为什么? I thought stuff allocated on the stack disappears with the function call returning? 我认为在堆栈上分配的东西会随着函数调用返回而消失? or is it because I return the pointer? 或者是因为我返回指针? is this dangerous in any way? 这有什么危险吗?

host is not allocated on the stack, only a pointer to it is on the stack. host没有在堆栈上分配,只有指向它的指针在堆栈上。 The pointer gets copied when the function returns, so there is nothing wrong with the code. 函数返回时会复制指针,因此代码没有任何问题。

Note that gethostbyname does not actually dynamically allocate memory. 请注意, gethostbyname实际上并不动态分配内存。 It always returns a pointer to the same statically allocated block of memory, which is why valgrind doesn't report a leak. 它总是返回一个指向同一个静态分配的内存块的指针,这就是valgrind不报告泄漏的原因。 Be careful, though, because that means you have to copy the hostent returned by your function if you want to save the value for later because further calls to gethost will overwrite it. 但要小心,因为这意味着如果要稍后保存值,则必须复制函数返回的hostent ,因为对gethost进一步调用将覆盖它。

It's fine and does leak because the returned pointer doesn't point to data on stack or heap, but some static variable. 它很好并且确实泄漏,因为返回的指针不指向堆栈或堆上的数据,而是指向一些静态变量。

http://linux.die.net/man/3/gethostbyname : http://linux.die.net/man/3/gethostbyname

The functions gethostbyname() and gethostbyaddr() may return pointers to static data, which may be overwritten by later calls . 函数gethostbyname()和gethostbyaddr()可能会返回指向静态数据的指针,这些指针可能会被以后的调用覆盖 Copying the struct hostent does not suffice, since it contains pointers; 复制struct hostent是不够的,因为它包含指针; a deep copy is required. 需要深层复印件。

from the manual : 从手册:

RETURN VALUE
       The gethostbyname() and gethostbyaddr() functions  return  the  hostent
       structure  or a NULL pointer if an error occurs.  On error, the h_errno
       variable holds an error number.  When non-NULL, the  return  value  may
       point at static data, ...

Some memory is reserved at the compile time (ie. inside the binary the code) for the structure, the function returns a pointer to this memory. 一些内存在编译时保留(即在二进制代码内部)为结构保留,该函数返回一个指向该内存的指针。

Well the memory is not leaked until all references to it is lost, in your example the pointer is returned so there is still a reference to it. 好吧,在所有对它的引用都丢失之前,内存不会被泄露,在你的例子中,指针被返回,所以仍然有对它的引用。

However imho it's a bad design decision on most cases to rely on another part of the code to release dynamic memory. 然而,对于大多数情况而言,依赖于代码的另一部分来释放动态内存是一个糟糕的设计决策。 If the function needs to return a struct for example, the caller should do it and pass a pointer to the struct. 如果函数需要返回一个结构,例如,调用者应该这样做并传递指向结构的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C语言中,结构数组是否在堆栈中分配? - In C, does an array of a structure gets allocated in the stack? 为什么在C中允许的函数中返回堆栈分配的指针变量? - Why is returning a stack allocated pointer variable in a function allowed in C? 为什么不能在它的函数之外访问我动态分配的数组? [C] - Why can't my dynamically allocated array be accessed outside of it's function? [C] 为什么gcc返回0而不是堆栈分配变量的地址? - Why does gcc return 0 instead of the address of a stack allocated variable? 是否为每个函数和ISR分配了自己的堆栈大小 - Does each function and ISR get allocated its own stack size 使用malloc分配的内存不会在函数范围之外保留? - Memory allocated with malloc does not persist outside function scope? 为什么C中的填充对堆栈上分配的变量/结构有效? - Why padding in C is valid for variables/structs allocated on stack? 为什么我在C中的递归函数会导致堆栈溢出? - Why does my recursive function in C cause a stack overflow? 为什么在调用汇编函数时C不能将指针压入堆栈? - Why does C not push a pointer on the stack when calling a assembly function? 为什么 C 标准库不提供 function 以了解分配的 memory 的大小? - Why C standard library does not provide a function for knowing the size of allocated memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM