免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4012 | 回复: 16
打印 上一主题 下一主题

关于malloc()分配内存的长度 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-07-21 10:08 |只看该作者 |倒序浏览
可以用size_t _msize(void *)试试。
  上次有人问这个问题,找不到那个帖子了,所以另发一个。那次有人说用strlen(),我觉得不太好,但一时没想起什么函数;今天才发现有这个函数,^_^。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-07-21 12:15 |只看该作者

关于malloc()分配内存的长度

这是什么函数,没见过。

论坛徽章:
1
15-16赛季CBA联赛之四川
日期:2016-02-15 17:12:32
3 [报告]
发表于 2004-07-21 12:17 |只看该作者

关于malloc()分配内存的长度

没见过

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2004-07-21 12:24 |只看该作者

关于malloc()分配内存的长度

不知道。man也没查到。

论坛徽章:
0
5 [报告]
发表于 2004-07-21 12:24 |只看该作者

关于malloc()分配内存的长度

我也没见过呀,解释解释

论坛徽章:
0
6 [报告]
发表于 2004-07-21 13:58 |只看该作者

关于malloc()分配内存的长度

是我迷糊了,sorry!

  我是在msdn里看到的,在windows下编了程序测试是可以的;然后在aix的头文件里也找到这个函数了(没有测试),就以为这是个通用函数。刚刚看了大伙的帖子就到saloris下找,但没有找到, 。我再看看是怎么回事。
msdn的的说明大概如下:
The _msize function returns the size, in bytes, of the memory block allocated by a call to calloc, malloc, or realloc.

When the application is linked with a debug version of the C run-time libraries, _msize resolves to _msize_dbg. For more information about how the heap is managed during the debugging process, see The CRT Debug Heap.

在aix下:
extension.h:      extern size_t _msize(void *);

论坛徽章:
0
7 [报告]
发表于 2004-07-21 14:18 |只看该作者

关于malloc()分配内存的长度

不好意思,我没有严格地在各个系统下进行测试,发了一个会误导人地帖子,本来找到这个函数还挺高兴的,han~~~sorry~~~
   关于这个_msize(),有人做了如下的说明:
This function or macro is not a standard function or macro. If
your system is to provide such a function or macro, you may need
to consult your systems documentation on it, but here we cannot
help you. Probably you want to ask about this in a system related
newsgroup.

OTOH, if you want your program to be portable, you shouldn't be
using such non-standard features. For this peculiar case, there
is no standard way to get the size of a malloced array after the
malloc
, but you should remember the requested size somehow in
your program.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
8 [报告]
发表于 2004-07-21 14:37 |只看该作者

关于malloc()分配内存的长度

原帖由 "soloarrow" 发表:
OTOH, if you want your program to be portable, you shouldn't be
using such non-standard features. For this peculiar case, there
is no standard way to get the size of a malloced array after the
malloc, but you should remember the requested size somehow in
your program.


没有标准的方法得到,那么这个函数的内部是怎么实现的呢?

论坛徽章:
0
9 [报告]
发表于 2004-07-21 14:58 |只看该作者

关于malloc()分配内存的长度

我觉得malloc()函数好像只是分配了一块内存而已,linux2.0.3.9中的函数在下面,它没有做验证,其他系统的不太清楚,哪位老大能说明一下?

  1. static void *malloc(int size)
  2. {
  3.         void *p;

  4.         if (size <0) error("Malloc error\n");
  5.         if (free_mem_ptr <= 0) error("Memory error\n");

  6.         free_mem_ptr = (free_mem_ptr + 3) & ~3;        /* Align */

  7.         p = (void *)free_mem_ptr;
  8.         free_mem_ptr += size;

  9.         if (free_mem_ptr >;= free_mem_end_ptr)
  10.                 error("\nOut of memory\n");

  11.         return p;
  12. }
复制代码

论坛徽章:
0
10 [报告]
发表于 2004-07-21 15:14 |只看该作者

关于malloc()分配内存的长度

我想再請教各位一個malloc的問題,
當malloc左一個位置後, 是否需要用memcpy來初始化呢, 當用完後是否需要free出來給系統呢? 請各位賜教. thanks.
例如:
char *abc;
abc = (char *)malloc(100);
memcpy(abc, 0, 100);
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP