malloced工会会员 [英] malloced union member

查看:50
本文介绍了malloced工会会员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我很困惑这个程序中的内存何时释放。


#include< stdlib.h>

#include< stdio.h>

#include< string.h>


union test {

char * i;

char * ch;

};


int main()

{

联合测试测试;

test.ch = malloc(6);

strcpy(test.ch,& ;你好");

printf(" test.i ==%s test.ch ==%s test.i == [%p] test.ch ==

[%p] \ n",test.i,test.ch,test.i,test.ch);

免费(test.i);

printf(" test.i ==%s test.ch ==%s test.i == [%p] test.ch ==

[%p] \ n" ,test.i,test.ch,test.i,test.ch);

free(test.ch);

printf(" test.i == %s test.ch ==%s test.i == [%p] test.ch ==

[%p] \ n",test.i,test.ch,test。我,test.ch);


返回0;

}


我从我的输出solaris盒子是:

test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]

test.i = = hello test.ch == hello test.i == [209b8] test.ch == [209b8]

test.i == hello test.ch == hello test.i == [ 209b8] test.ch == [209b8]


问候

rohitash

解决方案

pr******@yahoo.com (rohit)写道:

我很困惑这个程序中的内存何时释放。

联合测试{
char * i;
char * ch;
} ;

int main()
{union test test;
test.ch = malloc(6);
strcpy(test.ch,& ;你好");
printf(" test.i ==%s test.ch ==%s test.i == [%p] test.ch ==
[%p] \\ \\ n,test.i,test.ch,test.i,test.ch);
free(test.i);


这是免费的()d ...

printf(" test.i ==%s test.ch ==%s test。 i == [%p] test.ch ==
[%p] \ n",test.i,test.ch,test.i,test.ch);


....所以这个语句会调用未定义的行为。

test.i == hello test.ch == hello test.i == [209b8 ] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]




是的,看起来像正常一样工作是

未定义行为的法律结果之一。如果你现在想要滥用这个功能,

要小心!看起来在你的测试机器上正常工作但是b / b
在你客户的网络上崩溃是_also_一个合法的

UB的结果......


Richard


rohit写道:




Iam困惑关于何时在此程序中释放内存。

#include< stdlib.h>
#include< stdio.h>
#include< string.h> ;

联合测试{
char * i;
char * ch;
};

int main()
{
联合测试测试;
test.ch = malloc(6);
strcpy(test.ch," hello");
printf(" test.i = =%s test.ch ==%s test.i == [%p] test.ch ==
[%p] \ n",test.i,test.ch,test.i,test .ch);
免费(test.i);


你为什么'免费()'''test.i"在这里?

您将内存分配给test.ch

(尽管联盟的两个元素

碰巧包含相同的内容指针值,

这是一个非常糟糕的做法。)

printf(" test.i ==%s test.ch ==%s test.i = = [%p] test.ch ==
[%p] \ n",test.i,test.ch,test.i,test.ch);
免费(test.ch) ;


你在这里很幸运。你有'免费()'相同的

指针值_twice_。你已经损坏了你的堆。

printf(" test.i ==%s test.ch ==%s test.i == [%p] test.ch ==
[%p] \ n",test.i,test.ch,test.i,test.ch);

返回0;
}
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
测试.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8 ] test.ch == [209b8]

问候
rohitash




看起来你期待这个电话`free()''

以某种方式改变

指针所指向的对象的内容,甚至是指针本身。

调用`free()''

后指针的值是不确定的。


在这种特殊情况下,你能够取消引用

指针,它仍然包含你原来的

值。这是运气不好。


要回答你的问题,通过调用`malloc()''获得的内存是免费的。在进入点

进入`free()''的调用。 "游离QUOT;然而,这是一个非特定的

术语,应该理解为

不是免费的。让你的程序更长时间访问。

一些架构师可能会通过该指针生成错误访问内存,有些可能无效,

有些人可能会给_appearence_记忆

仍然有效且包含有效数据(如你的

Solaris盒子)。

HTH,


Stephen


" Stephen L." < SD ********* @ cast-com.net>写道:

rohit写道:

printf(" test.i ==%s test.ch ==%s test.i == [% p] test.ch ==
[%p] \ n",test.i,test.ch,test.i,test.ch);
free(test.ch);



你在这里很幸运。你有'(')指针值_twice_的free()'。你已经损坏了你的堆。




你不知道这个。它调用未定义的行为;这个_may_意味着

破坏他的堆,但也可能意味着忽略声明,

因分段错误而崩溃,或者将他的简历邮寄到
bi ****** @ whitehouse.gov


Richard


Hi,

Iam confused as to when is the memory freed in this program.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

union test{
char *i;
char *ch;
};

int main()
{
union test test;
test.ch = malloc(6);
strcpy(test.ch,"hello");
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.i);
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.ch);
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);

return 0;
}

And the output I get from my solaris box is :
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]

regards
rohitash

解决方案

pr******@yahoo.com (rohit) wrote:

Iam confused as to when is the memory freed in this program.

union test{
char *i;
char *ch;
};

int main()
{
union test test;
test.ch = malloc(6);
strcpy(test.ch,"hello");
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.i);
It is free()d here...
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
....so this statement invokes undefined behaviour.
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]



Yes, appearing to work as "normal" is one of the legal results of
undefined behaviour. Should you now be tempted to abuse this feature,
beware! Appearing to work as normal on your testing machines but
crashing spectacularly on your customer''s network is _also_ a legal
result of UB...

Richard


rohit wrote:


Hi,

Iam confused as to when is the memory freed in this program.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

union test{
char *i;
char *ch;
};

int main()
{
union test test;
test.ch = malloc(6);
strcpy(test.ch,"hello");
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.i);
Why are you `free()''ing "test.i" here?
You allocated the memory to "test.ch"
(even though both elements of the union
happen to contain the same pointer value,
that''s a very bad practice).
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.ch);
You got lucky here. You''ve `free()''d the same
pointer value _twice_. You''ve corrupted your heap.
printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);

return 0;
}

And the output I get from my solaris box is :
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]
test.i == hello test.ch == hello test.i == [209b8] test.ch == [209b8]

regards
rohitash



It looks like you''re expecting the call to `free()''
to somehow change the contents of the object the
pointer was pointing to, or even the pointer itself.
The value of a pointer after a call to `free()''
is indeterminate.

In this particular case, you were able to dereference
the pointer and it still contained your original
value. This is dumb luck.

To answer your question, the memory obtained by
a call to `malloc()'' is "free" at the point of entry
into the call to `free()''. "free" is a non-specific
term, however, and should be taken to mean that it
is not "free" for your program to access any longer.
Some architectues may generate an error accessing
the memory through that pointer, some may do nothing,
and some may give the _appearence_ that the memory
is still valid and contains valid data (as on your
Solaris box).
HTH,

Stephen


"Stephen L." <sd*********@cast-com.net> wrote:

rohit wrote:

printf("test.i == %s test.ch == %s test.i == [%p] test.ch ==
[%p]\n",test.i,test.ch,test.i,test.ch);
free(test.ch);



You got lucky here. You''ve `free()''d the same
pointer value _twice_. You''ve corrupted your heap.



You don''t know this. It invokes undefined behaviour; this _may_ mean
corrupting his heap, but it may also mean ignoring the statement,
crashing with a segmentation fault, or mailing his resume to
bi******@whitehouse.gov.

Richard


这篇关于malloced工会会员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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