如何获得指针所指向的数组大小?

manofstraw 2003-05-24 10:22:04
char* pc = "abc";
int* pi;

pi = new int[4];

如何获得pc,pi指针所指向的数组大小?
sizeof(pc) 和sizeof(pi)获的是指针本身的大小。
sizeof(*pc)和sizeof(*pi)是数组元素的大小。
但如何获得指针所指向的数组大小?
...全文
539 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
13520089720tianyu 2003-05-27
  • 打赏
  • 举报
回复
up
DangerousWang 2003-05-27
  • 打赏
  • 举报
回复
感谢thxlp(tiamo)

char* pc = "abc";
int* pi;

= new int[4];

int nsize = strlen(pc); //nsize = 3
size_t size = _msize(pi); //size = 16



thxlp 2003-05-27
  • 打赏
  • 举报
回复
sign.............

看这个函数

_msize

嘿嘿

在win95 a developers guide一书里面看到的.

--------------------------------

msize
Returns the size of a memory block allocated in the heap.


Routine Required Header
_msize <malloc.h>


size_t _msize( void *memblock );
Parameters

memblock
Pointer to memory block
Libraries

All versions of the C run-time libraries.

Return Values

_msize returns the size (in bytes) as an unsigned integer.

Remarks

The _msize function returns the size, in bytes, of the memory block allocated by a call to 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 Using C Run-Time Library Debugging Support.

Example

/* REALLOC.C: This program allocates a block of memory for
* buffer and then uses _msize to display the size of that
* block. Next, it uses realloc to expand the amount of
* memory used by buffer and then calls _msize again to
* display the new amount of memory allocated to buffer.
*/

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

void main( void )
{
long *buffer;
size_t size;

if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
exit( 1 );

size = _msize( buffer );
printf( "Size of block after malloc of 1000 longs: %u\n", size );

/* Reallocate and show new size: */
if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))
== NULL )
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
size );

free( buffer );
exit( 0 );
}
Output

Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000
See Also

malloc, realloc



Built on Thursday, February 01, 2001

用msdn查就可以了
这个是c的运行库...........
sun_730531 2003-05-27
  • 打赏
  • 举报
回复
strlen(pc)
真相重于对错 2003-05-26
  • 打赏
  • 举报
回复
win32 api GlobalSize(Handle);
ynlg 2003-05-26
  • 打赏
  • 举报
回复
No Way!
DangerousWang 2003-05-26
  • 打赏
  • 举报
回复
不能!!!
yqdeng 2003-05-24
  • 打赏
  • 举报
回复
很久没用C++了,不过记得好像这个没有办法
WPS Office 2019 是一款功能丰富的办公软件套装,它整合了文字、表格、演示、PDF 等组件,具备体积小巧、功能丰富、兼容性强等特点,能满足日常办公的多种需求。 功能组件丰富:包含 WPS 文字、WPS 演示、WPS 表格、WPS PDF 等组件,可用于文档编辑、表格计算、幻灯片制作、PDF 处理等,支持 DOC、DOCX、XLS、XLSX、PPT 等多种格式,能与 Microsoft Office 较好地兼容。 支持云存储与协作:用户登录 WPS 账号后,可实现多终端、跨平台的数据同步,还能与他人协同办公,文档可通过微信、QQ 等社交平台一键分享。 PDF 功能强大:提供沉浸式 PDF 阅读体验和稳定的编辑服务,支持一键编辑 PDF 文档内容。借助 OCR 技术,可精准转换文档、表格、PPT、图片等格式的文件。 界面设计优化:具有全新视觉设计,默认皮肤舒适清爽,还支持桌面背景、界面字体、皮肤、格式图标等个性化设置。同时,文档标签可拖拽成独立窗口或合并,用户可自主选择文档显示方式,还能将文件放置在不同工作区,便于分类浏览与管理。 内置实用工具:内置了简单的网页浏览器,点击文档中的链接会默认用其打开,可进行网页添加到首页、另存为 PDF 等操作。此外,“应用中心” 集成了输出转换、文档助手、安全备份等多种实用功能。 模板资源丰富:稻壳商城提供了大量模板、范文、图片等素材资源,涵盖求职简历、总结计划、合同协议等常见文档类型,方便用户快速创建专业文档。 表格功能特色突出:支持通过关键词快速切换工作表,设有阅读模式和护眼模式。还可分类合并单元格,以及拆分合并单元格并填充内容。公式编辑器中可轻松完成公式嵌套,筛选时可通过 “仅筛选此项” 一步完成特定值筛选,还能根据筛选条件生成动态分析图表。另外,“特色功能” 和 “智能工具箱” 菜单中包含 PDF

7,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧