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

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)是数组元素的大小。
但如何获得指针所指向的数组大小?
...全文
522 8 打赏 收藏 转发到动态 举报
写回复
用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++了,不过记得好像这个没有办法

7,540

社区成员

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

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