内存分配包装器 [英] memory allocation wrapper

查看:61
本文介绍了内存分配包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello all


我为malloc和朋友写了一个包装器。它的

来自

http://www.lelanthran.com/downloads/os_mem/index.php

写这篇文章的原因让新手

最终可以得到以下答案

新手问题:

1.我怎么知道指针指向多少内存?

2.如何判断指针是否有效?


我知道大部分注册表都不需要这个,但是

a好撒玛利亚人并指出错误:-)


评论和批评(sp?)欢迎;请注意,它只针对c99合规目标




鹅,

解决方案

< BLOCKQUOTE>"鹅" < lk *** @ webmail.co.zawrote in message


Hello all


我写了一个malloc和朋友的包装。它的

来自

http://www.lelanthran.com/downloads/os_mem/index.php

写这篇文章的原因让新手

最终可以得到以下答案

新手问题:

1.我怎么知道指针指向多少内存?

2.如何判断指针是否有效?


我知道大部分注册表都不需要这个,但是

a好撒玛利亚人并指出错误:-)


评论和批评(sp?)欢迎;请注意,它仅用于符合c99标准的



鹅,



有''更容易实现你想做的事情。

只需在分配的内存开头放一个结构,添加你的

控制信息,并立即返回指针之后。

然后当用户调用free时,减去控件的大小

结构,并且空闲。对于访问功能也是如此。


有一个难看的障碍。 malloc()必须为任何目的返回内存对齐。

实际上你可以得到足够好的内容。通过填充你的结构可移植性

到偶数个双打。在标准领域,你可能会冒着bBBB UB的恐怖风险,并且无法在ANSI C中实现完全兼容的程序。


-
www.personal.leeds.ac.uk/~bgy1mm

免费软件游戏下载。


Malcolm写道:


"鹅" < lk *** @ webmail.co.zawrote in message


>> Hello all

我写了一个malloc和朋友的包装。


http ://www.lelanthran.com/downloads/os_mem/index.php

写这篇文章的原因是为了让新手能够最终得到以下内容的答案新手问题:
1。我怎么知道指针指向多少内存?
2。我怎么知道指针是否有效?

我知道大多数reg不需要这个,但是要成为一个好撒玛利亚人并指出错误: - )

评论和批评(sp?)欢迎;请注意,它仅用于符合c99标准。

goose,



有一种更容易实现你的方法想要做。

只需在分配的内存开头放一个结构,添加你的

控制信息,然后立即返回指针。

然后当用户调用free时,减去控件的大小

结构,并且空闲。同样的访问功能。



我最初用这种方式做到了但是我无法确定
如何确定是否指针有效。另外

我想要的东西是能够在任何给定的时间告诉

程序使用的总内存量是多少并允许

解冻所有已分配内存的程序。


我这样做的最后(也是最重要的)原因

是因为我正在设计自己的语言[1]并在C中实现

并且希望自动使用我的语言GC。


我自己的玩具语言如果我只使用os_mem_ *函数

我可以更容易地实现gc。

[1]每个程序员都经历了思考阶段

肯定必须有更好的方式然后关闭

设计他们自己的语言,所有漂亮的功能
他们想要的
。这可能是一个通过仪式,或者

的东西:-)


鹅,


< blockquote>

goose写道:


Hello all



良好开端。您可以考虑添加:


(1)一个哨兵字段,在每个

块之前和之后说一个char [4] ==''HEAP'' ,所以你可以检测到块写入溢出。


(2)让你的free()将块数据清零,以确保程序

赢得''即使它在

指针被释放后访问数据也继续运行。


(3)让你的free()成为一个宏,所以你可以附加一个ptr = NULL

语句,用户不能使用悬空指针。


Hello all

I''ve written a wrapper for malloc and friends. Its
available from

http://www.lelanthran.com/downloads/os_mem/index.php

The reason for doing writing this so that newbies
can finally get answers to the following
newbie questions:
1. How do I know how much memory a pointer points to?
2. How can I tell if a pointer is valid?

I know most of the regs don''t need this, but be
a good samaritan and point out the bugs :-)

Comments and criticisms(sp?) welcome; note that it aims
for c99 compliance only.

goose,

解决方案

"goose" <lk***@webmail.co.zawrote in message

Hello all

I''ve written a wrapper for malloc and friends. Its
available from

http://www.lelanthran.com/downloads/os_mem/index.php

The reason for doing writing this so that newbies
can finally get answers to the following
newbie questions:
1. How do I know how much memory a pointer points to?
2. How can I tell if a pointer is valid?

I know most of the regs don''t need this, but be
a good samaritan and point out the bugs :-)

Comments and criticisms(sp?) welcome; note that it aims
for c99 compliance only.

goose,

There''s a much easier way of achieveing what you want to do.
Simply put a structure at the start of the allocated memory, add your
control information, and return the pointer immediately after.
Then when the user calls the free, subtract the size of the control
structure, and free. Similarly for the access functions.

There is a niggly snag. malloc() must return memory aligned for any purpose.
In practise you can get "good enough" portability by padding your structure
out to an even number of doubles. In standards land, you risk the horrors of
UB, and there is no way of achieving a fully compliant program in ANSI C.

--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


Malcolm wrote:

"goose" <lk***@webmail.co.zawrote in message

>>Hello all

I''ve written a wrapper for malloc and friends. Its
available from

http://www.lelanthran.com/downloads/os_mem/index.php

The reason for doing writing this so that newbies
can finally get answers to the following
newbie questions:
1. How do I know how much memory a pointer points to?
2. How can I tell if a pointer is valid?

I know most of the regs don''t need this, but be
a good samaritan and point out the bugs :-)

Comments and criticisms(sp?) welcome; note that it aims
for c99 compliance only.

goose,


There''s a much easier way of achieveing what you want to do.
Simply put a structure at the start of the allocated memory, add your
control information, and return the pointer immediately after.
Then when the user calls the free, subtract the size of the control
structure, and free. Similarly for the access functions.

I did initially have it this way but I wasn''t able to
figure out how determine if a pointer is valid. The other
things I wanted was to be able to tell at any given time
how much total memory the program is using and to allow
the program to free all allocated memory.

The last (and most important) reason I did it this way
is because I''m designing my own language[1] and implementing
it in C and would like to have my language automatically GC.

Within My Own Toy Language if I only use os_mem_* functions
I can implement the gc much easier.
[1] Every programmer goes through the phase of thinking
"surely there must be a better way" and then goes off
designing their own language with all the nifty features
they''d like to have. It''s probably a rite of passage, or
something :-)

goose,



goose wrote:

Hello all

Good start. You might consider adding:

(1)A sentinel field, say a char[4] == ''HEAP'' before and after each
block, so you can detect block write overruns.

(2) Have your free() zero out the block data, to ensure the program
won''t continue running okay even if it accesses the data after the
pointer is freed.

(3) make your free() a macro, so you can append a "ptr = NULL"
statement sot he user can''t use a dangling pointer.


这篇关于内存分配包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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