是否可以将'内存地址'传递给free() [英] Is it ok to pass 'memory address' to free()

查看:48
本文介绍了是否可以将'内存地址'传递给free()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我对C编程比较陌生,我很惊讶

动态内存分配问题。

我想知道是否可以通过malloc()传递''内存地址''返回

(不存储在指针变量中,而是作为
$ b $的元素) b类型int *)直接到free()。我的程序分配和

以这种方式释放内存正在破坏堆。

任何人都可以指导我这个。


谢谢你提前

DSKR

Hello All,
I am relatively new to C programming and I am struck with a problem in
dynamic memory allocation.
I would like to know if it is ok to pass the ''memory address'' returned
by malloc()(stored not in a pointer variable but as an element of an
array of type int*) directly to free(). My program allocating and
freeing memory this way is corrupting the heap.
Can anybody guide me on this.

Thanks in advance
DSKR

推荐答案

2006年8月17日02:57:28 -0700,FI ; < fi ********** @ myrealbox.com>

写道:
On 17 Aug 2006 02:57:28 -0700, "FI" <fi**********@myrealbox.com>
wrote:

> Hello All,<我对C编程比较陌生,我对动态内存分配问题感到震惊。
我想知道是否可以传递''内存地址''返回<通过malloc()(不存储在指针变量中但作为int *类型的元素的元素存储)直接到free()。我的程序以这种方式分配和释放内存正在破坏堆。
任何人都可以指导我。
>Hello All,
I am relatively new to C programming and I am struck with a problem in
dynamic memory allocation.
I would like to know if it is ok to pass the ''memory address'' returned
by malloc()(stored not in a pointer variable but as an element of an
array of type int*) directly to free(). My program allocating and
freeing memory this way is corrupting the heap.
Can anybody guide me on this.



您的问题的答案可能是肯定的,但没有看到您的

代码,这很难说。如果可以的话,发布产生问题的最小代码片段

代码。


以下是标准C,无论malloc()是否返回

是否为NULL:


#include< stdlib.h>

extern size_t n;

int main(void)

{

void * p = malloc(n);

free(p);

返回0;

}


最好的问候

-

jay

The answer to your question could be yes, but without seeing your
code, it''s hard to tell. If you can, post a minimal snippet of source
code that produces the problem.

The following is Standard C, regardless of whether malloc() returns
NULL or not:

#include <stdlib.h>
extern size_t n;
int main(void)
{
void *p = malloc(n);
free(p);
return 0;
}

Best regards
--
jay


FI写道:
FI wrote:

Hello All,

我对C编程比较陌生我很震惊

动态内存分配问题。

我想知道是否可以传递''内存地址''返回

by malloc()(不是存储在指针变量中,而是作为int *类型的
数组的元素存储)直接到free()。我的程序分配和

以这种方式释放内存正在破坏堆。

任何人都可以指导我这个。
Hello All,
I am relatively new to C programming and I am struck with a problem in
dynamic memory allocation.
I would like to know if it is ok to pass the ''memory address'' returned
by malloc()(stored not in a pointer variable but as an element of an
array of type int*) directly to free(). My program allocating and
freeing memory this way is corrupting the heap.
Can anybody guide me on this.



您只能传递NULL或从

malloc,calloc或realloc获得的值。

You can only pass it NULL or a value you got from
malloc,calloc or realloc.


FI写道:
FI wrote:

Hello All,

我对C编程比较陌生,我很震惊问题在

动态内存分配。

我想知道是否可以通过''内存地址''返回

by malloc()(不是存储在指针变量中,而是作为int *类型的
数组的元素)直接存放到free()。我的程序分配和

以这种方式释放内存正在破坏堆。

任何人都可以指导我这个。
Hello All,
I am relatively new to C programming and I am struck with a problem in
dynamic memory allocation.
I would like to know if it is ok to pass the ''memory address'' returned
by malloc()(stored not in a pointer variable but as an element of an
array of type int*) directly to free(). My program allocating and
freeing memory this way is corrupting the heap.
Can anybody guide me on this.



我们可能会。发布代码,而不是模糊的代码描述。


Brian

We probably could. Post the code, not a vague description of the code.


Brian


这篇关于是否可以将'内存地址'传递给free()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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