释放记忆....部分 [英] freeing memory....partially

查看:59
本文介绍了释放记忆....部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i希望分配两个结构,只进行一次malloc调用。

所以我做prt = malloc(sizeof(struct1)+ sizeof(struct2)) ;

在这个操作之后,我指向第一个结构的两个指针(ptr1 = ptr),

另一个指向第二个结构(ptr2 = ptr + sizeof(struct2)) 。

这两个指针后来被两个线程用于互斥,所以

thread1无法访问ptr2,而thread2无法访问ptr1。

在某些时候,thread2想要释放他的部分内存,但是免费(ptr2)或免费((struct struct2 *)ptr)或者类似的东西会导致

a SEGV。我认为这种情况发生了,因为我必须传递给前一个malloc调用返回的free()一个指针

。但为什么会出现这种限制?

是否有可能以某种方式释放分配的内存部分

by malloc?

记住我不能使用realloc因为thread2无法访问ptr1。


非常感谢

Francesco Oppedisano

Hi,
i would like to allocate two structures making only one malloc call.
So i do prt=malloc(sizeof(struct1)+sizeof(struct2));
After this operation i make two pointers one to the first struct (ptr1=ptr),
the other to second struct(ptr2=ptr+sizeof(struct2)).
These two pointer are later used by two threads in mutual exclusion so
thread1 can''t access ptr2 and thread2 can''t access ptr1.
At some time thread2 wants to free his part of memory but a
free(ptr2) or free((struct struct2 *)ptr) or something similar leads to
a SEGV. This happens, i think, because i must pass to free() a pointer
returned by a previous malloc call. But why this limitation?
Is it possible, in some way, to free only a part of the memory allocated
by malloc?
Remember that i cannot use realloc beacause thread2 cannot access ptr1.

Thank u very much
Francesco Oppedisano

推荐答案

2003年10月4日星期六16:12:01 GMT,f。********** @ tiscalinet.it

< f.**********@tiscalinet.it>写道:
On Sat, 04 Oct 2003 16:12:01 GMT, "f.**********@tiscalinet.it"
<f.**********@tiscalinet.it> wrote:

我想分配两个结构只进行一次malloc调用。
所以我做prt = malloc(sizeof(struct1)+ sizeof (struct2));
在这个操作之后,我给第一个结构(ptr1 = ptr)做了两个指针,
另一个到第二个结构(ptr2 = ptr + sizeof(struct2))。


如果ptr是类型(struct1 *),那么

struct2 * ptr2 =(void *)(ptr + 1);

就足够了。

这两个指针稍后被两个线程用于互斥,因此
thread1无法访问ptr2,而thread2无法访问ptr1。
在某些时候,thread2想要释放他的部分内存,但是
free(ptr2)或free((struct struct2 *)ptr)或者类似的东西导致了一个SEGV。我认为这种情况发生了,因为我必须传递给前一个malloc调用返回的free()一个指针
。但为什么这个限制呢?


简单:free()期望任何malloc()返回,没有别的(禁止NULL,

ofcourse)。否则它是未定义的行为。

是否有可能以某种方式释放malloc分配的部分内存?


嗯,是的,做一个realloc()来缩小内存。

记住我不能使用realloc因为thread2无法访问ptr1。
Hi,
i would like to allocate two structures making only one malloc call.
So i do prt=malloc(sizeof(struct1)+sizeof(struct2));
After this operation i make two pointers one to the first struct (ptr1=ptr),
the other to second struct(ptr2=ptr+sizeof(struct2)).
If ptr is of type (struct1 *), then
struct2 *ptr2 = (void *) (ptr+1);
will suffice.
These two pointer are later used by two threads in mutual exclusion so
thread1 can''t access ptr2 and thread2 can''t access ptr1.
At some time thread2 wants to free his part of memory but a
free(ptr2) or free((struct struct2 *)ptr) or something similar leads to
a SEGV. This happens, i think, because i must pass to free() a pointer
returned by a previous malloc call. But why this limitation?
Easy: free() expects whatever malloc() returned, and nothing else (barring NULL,
ofcourse). It''s undefined behaviour otherwise.
Is it possible, in some way, to free only a part of the memory allocated
by malloc?
Hmm, yes, do a realloc() to shrink the memory.
Remember that i cannot use realloc beacause thread2 cannot access ptr1.




然后所有的赌注都关闭了。尝试重新设计代码!



Then all bets are off. Try redesigning your code!


2003年10月4日星期六21:28:36 +0500,rihad< ri *** @ mail.ru>写道:
On Sat, 04 Oct 2003 21:28:36 +0500, rihad <ri***@mail.ru> wrote:
简单:free()期望任何malloc()返回,没有别的(禁止NULL,
当然)。
Easy: free() expects whatever malloc() returned, and nothing else (barring NULL,
ofcourse).




括号中的部分是多余的。让我们优化它:


简单:free()期望任何malloc()返回,没有别的。



The part in parentheses is superfluous. Let''s optimize it away:

Easy: free() expects whatever malloc() returned, and nothing else.




< f。********** @ tiscalinet.it>在消息中写道

news:pa **************************** @ tiscalinet.it。 ..

<f.**********@tiscalinet.it> wrote in message
news:pa****************************@tiscalinet.it. ..

我想分配两个只进行一次malloc调用的结构。
所以我做prt = malloc(sizeof(struct1)+ sizeof(struct2)) ;在这个操作之后,我给第一个struct
(ptr1 = ptr)做了两个指针,另一个指向第二个struct(ptr2 = ptr + sizeof(struct2))。
如果ptr是类型(struct1 *),并且结构对齐正确

(你不能以独立于平台的方式找到它),正确的

这样做的方法是:

ptr2 =(struct2 *)(ptr + 1);


如果你想做的话正在巩固分配,更清洁的方式将是:b
$ b struct combined_struct {

struct1 s1;

struct2 s2;

};

struct combined_struct * p = malloc(sizeof * p);

/ *不要忘记malloc故障检查* /

ptr =& p-> s1;

ptr2 =& p-> s2;

这样做的好处是你不必担心对齐,因为

编译器会为你处理这个问题。


但是,这并没有解决下一个要求:

这两个指针后来被两个线程用于互斥,所以
thread1无法访问ptr2和thread2 c不要访问ptr1。
在某些时候,thread2想要释放他的部分内存,但是
free(ptr2)或free((struct struct2 *)ptr)或者类似的东西导致
一个SEGV。我认为这种情况发生了,因为我必须传递给前一个malloc调用返回的free()一个指针
。但是为什么这个限制?
是否有可能以某种方式释放malloc分配的内存的一部分?
Hi,
i would like to allocate two structures making only one malloc call.
So i do prt=malloc(sizeof(struct1)+sizeof(struct2));
After this operation i make two pointers one to the first struct (ptr1=ptr), the other to second struct(ptr2=ptr+sizeof(struct2)). If ptr is of type (struct1*), and the structure alignments come out correct
(which you can''t really find out in a platform independent way), the correct
way to do this is:
ptr2 = (struct2*)(ptr + 1);

If all you wanted to do was consolidate the allocations, a cleaner way would
be:

struct combined_struct {
struct1 s1;
struct2 s2;
};
struct combined_struct *p = malloc( sizeof *p );
/* Don''t forget the malloc failure check */
ptr = &p->s1;
ptr2 = &p->s2;

This has the advantage that you don''t have to worry about alignment, as the
compiler will take care of that for you.

However, this doesn''t address the next requirement:
These two pointer are later used by two threads in mutual exclusion so
thread1 can''t access ptr2 and thread2 can''t access ptr1.
At some time thread2 wants to free his part of memory but a
free(ptr2) or free((struct struct2 *)ptr) or something similar leads to
a SEGV. This happens, i think, because i must pass to free() a pointer
returned by a previous malloc call. But why this limitation?
Is it possible, in some way, to free only a part of the memory allocated
by malloc?



不是。它不是可能。除了realloc之外,免费分配

内存的唯一方法是免费拨打电话,这将释放malloc

分配的所有内存。要做你想做的事,你需要:


- 单独的malloc struct1和struct2,或者

- 使用提供附加功能的内存分配器功能你

想要。


BTW:为什么分别分配这两个结构很难?

-

poncho


No. it is not possible. Apart from realloc, the only way to free allocated
memory is to call free, which will free all the memory that malloc
allocated. To do what you want to do, you either need to:

- Malloc struct1 and struct2 separately, or
- Use a memory allocator that provides the additional functionality you
want.

BTW: why is allocating the two structures separately difficult?
--
poncho


这篇关于释放记忆....部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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