[英]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.