......来自罗马的malloc ......-) [英] ...malloc ...from Rome :-)

查看:81
本文介绍了......来自罗马的malloc ......-)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......我在这儿又一次...一个简单的问题:


#include< stdlib.h>

#包括< stdio.h>


int main(无效)

{

/ *

**我知道其他人使用不同的malloc

**指令

* /


int * pInt =(int *)malloc(1 * sizeof(pInt));


if(pInt)

{

printf(" Memory分配SUCCESS [%p] \ n",pInt);

免费(pInt);

}


printf( 释放以前分配的内存[%p] \ n,pInt);


/ *

**为什么我能看到相同的地址一次我已经发布了吗?

* /


返回0;

}


谢谢大家,祝你有个美好的一天(???)...

解决方案

" lasek" < CL ************** @ acrm.it>写道:

大家好......我在这儿又一次...一个简单的问题:

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

int main(void)
{
/ *
**我知道其他人使用不同的malloc
**指令
* /

int * pInt =(int *)malloc(1 * sizeof(pInt));

if(pInt)
{
printf(" Memory allocated SUCCESS [%p] \ n",pInt);
免费(pInt);
}

printf (释放以前分配的内存[%p] \ n,pInt);

/ *
**为什么我一旦发布就能看到相同的地址?
* /




freeres不会删除地址,只有内存pInt指向

将被释放通话结束后。它甚至可以发生,你可以访问已经释放的内存,但这取决于操作系统

系统。


亲切的回归,

Nicolas


-

| Nicolas Pavlidis | Elvis Presly:| \ | __ |

| SE& s的学生KM | 进入goto | \ | __ | |

| pa****@sbox.tugraz.at | ICQ#320057056 | |

| -------------------格拉茨技术大学---------------- |


文章

< 97 ********************* *********@localhost.talkab outprogramming.com> ;,

" lasek" < CL ************** @ acrm.it>写道:

大家好......我在这儿又一次...一个简单的问题:

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

int main(void)
{
/ *
**我知道其他人使用不同的malloc
**指令
* /

int * pInt =(int *)malloc(1 * sizeof(pInt));


这是假的 - (int *)强制转换是不需要的,而sizeof()

表达式是错误的。它应该是:


int * pInt = malloc(1 * sizeof(* pInt));


if(pInt)
{
printf(" Memory allocated SUCCESS [%p] \ n",pInt);
免费(pInt);
}

printf(" ;释放先前分配的内存[%p] \ n",pInt);




这会引发未定义的行为 - 您可能无法引用
的值
pInt以任何方式传递给free()后。


干杯,

- jonathan


>这是假的 - (int *)强制转换是不需要的,而sizeof()

表达式是错误的。它应该是:

int * pInt = malloc(1 * sizeof(* pInt));

if(pInt)
{
printf(" Memory allocated SUCCESS [%p] \ n",pInt);
免费(pInt);
}

printf(" ;释放先前分配的内存[%p] \ n",pInt);



这会引发未定义的行为 - 您可能无法以任何方式引用
pInt的值之后它被传递给free()。​​




实际上它不是。


如果你定义int * p;

可以访问p但是可以在免费执行后访问* p



另外你也是似乎有些不可取的是,一旦传递给免费功能,p />
的价值就会改变。这也是

不正确。只有p指向的内存地址可能/可能不是
已更改。 p的实际值将保持不变。


James


-

------ --------------------

手机:+44 07779080838
http://www.stev.org

下午3:00起1天,23:42,5位用户,平均负载:0.03,0.01,0.00


Hi all..i''m here another time..for a simple question:

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

int main(void)
{
/*
** I know that someone else use different malloc
** instruction
*/

int *pInt=(int*)malloc(1*sizeof(pInt));

if(pInt)
{
printf("Memory allocated SUCCESS [%p]\n",pInt);
free(pInt);
}

printf("Release memory previously allocated [%p]\n",pInt);

/*
** Why i can see the same address once that i''ve released?
*/

return 0;
}

Thanks all and have a nice day(???)...

解决方案

"lasek" <cl**************@acrm.it> writes:

Hi all..i''m here another time..for a simple question:

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

int main(void)
{
/*
** I know that someone else use different malloc
** instruction
*/

int *pInt=(int*)malloc(1*sizeof(pInt));

if(pInt)
{
printf("Memory allocated SUCCESS [%p]\n",pInt);
free(pInt);
}

printf("Release memory previously allocated [%p]\n",pInt);

/*
** Why i can see the same address once that i''ve released?
*/



The addres will not be removed by free(), only the memory pInt points to
will be freed after the call. It can even happen, that you can access
the memory which was already freed, but this depends on the operatin
system.

Kind regrads,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|


In article
<97******************************@localhost.talkab outprogramming.com>,
"lasek" <cl**************@acrm.it> wrote:

Hi all..i''m here another time..for a simple question:

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

int main(void)
{
/*
** I know that someone else use different malloc
** instruction
*/

int *pInt=(int*)malloc(1*sizeof(pInt));
This is bogus -- the (int*) cast is unneeded, and the sizeof()
expression is wrong. It should be:

int *pInt = malloc(1 * sizeof (*pInt));

if(pInt)
{
printf("Memory allocated SUCCESS [%p]\n",pInt);
free(pInt);
}

printf("Release memory previously allocated [%p]\n",pInt);



This provokes undefined behavior -- you may not reference the value of
pInt in any way after it has been passed to free().

Cheers,
- jonathan


> This is bogus -- the (int*) cast is unneeded, and the sizeof()

expression is wrong. It should be:

int *pInt = malloc(1 * sizeof (*pInt));

if(pInt)
{
printf("Memory allocated SUCCESS [%p]\n",pInt);
free(pInt);
}

printf("Release memory previously allocated [%p]\n",pInt);



This provokes undefined behavior -- you may not reference the value of
pInt in any way after it has been passed to free().



Actually its not.

if you define int *p;
it is perforectly ok to access p but not ok to access *p
after the free is performed.

Also what you seem to impliy is that the value of p
will change once passed to the function free. This is also
not true. Only the memory address that p points to may / may not be
changed. The actually value of p will remain constant.

James

--
--------------------------
Mobile: +44 07779080838
http://www.stev.org
3:00pm up 1 day, 23:42, 5 users, load average: 0.03, 0.01, 0.00


这篇关于......来自罗马的malloc ......-)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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