确定 C 中动态分配内存的大小 [英] Determine size of dynamically allocated memory in C

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

问题描述

C 中有没有办法找出动态分配的内存大小?

Is there a way in C to find out the size of dynamically allocated memory?

例如之后

char* p = malloc (100);

有没有办法找出与p相关的内存大小?

Is there a way to find out the size of memory associated with p?

推荐答案

comp.lang.c FAQlist · 问题 7.27 -

问.那么我可以查询 malloc 包以找出分配的块有多大吗?

Q. So can I query the malloc package to find out how big an allocated block is?

A.不幸的是,没有标准或便携的方式.(一些编译器提供非标准的扩展.)如果你需要知道,你必须自己跟踪它.(另见问题 7.28.)

A. Unfortunately, there is no standard or portable way. (Some compilers provide nonstandard extensions.) If you need to know, you'll have to keep track of it yourself. (See also question 7.28.)

这篇关于确定 C 中动态分配内存的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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