需要知道char *指向的内存块的大小 [英] Need to know the size of the memory block pointed to by a char*

查看:101
本文介绍了需要知道char *指向的内存块的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要知道接收此指针的

函数中char *指向的内存块的大小。通常,此指针指向

字符串。 strlen()不会完成这项工作,因为有时字符串不是

null终止。

即:

int foo(void)< br $>
{

/ * ...... * /

char buf [20];

a(buf );

/ * ....... * /

返回0;

}

无效a(char * ptr)

{

/ *这里,我需要ptr指向的mem块的长度* /

}

Hi
I need to know the size of the memory block pointed to by a char* in a
function receiving this pointer. Typically, this pointer points to a
string. strlen() will not do the job since sometimes the string is not
null terminated.
That is:
int foo(void)
{
/* ...... */
char buf[20];
a(buf);
/* ....... */
return 0;
}
void a(char* ptr)
{
/* Here, I need the length of the mem block pointed at by ptr */
}

推荐答案

Frodo Baggins写道:
Frodo Baggins wrote:



我需要知道接收此指针的

函数中char *指向的内存块的大小。通常,此指针指向

字符串。 strlen()不会做这个工作,因为有时字符串不是

null终止。

那是:


int foo (无效)

{

/ * ...... * /

char buf [20];

a(buf);

/ * ....... * /

返回0;

}

void a(char * ptr)

{

/ *这里,我需要ptr指向的mem块的长度* /

}
Hi
I need to know the size of the memory block pointed to by a char* in a
function receiving this pointer. Typically, this pointer points to a
string. strlen() will not do the job since sometimes the string is not
null terminated.
That is:
int foo(void)
{
/* ...... */
char buf[20];
a(buf);
/* ....... */
return 0;
}
void a(char* ptr)
{
/* Here, I need the length of the mem block pointed at by ptr */
}



对于声明为数组的对象,sizeof(a)可以正常工作。


通过使用malloc()获得的内存,你必须自己跟踪

的长度。在指针上使用sizeof只能为指针类型提供

字节数。

For objects declared as arrays, sizeof(a) will work just fine.

For blocks of memory obtained by using malloc(), you must keep track of
the length yourself. Using sizeof on a pointer only gives you the
number of bytes for the pointer type.


Frodo Baggins写道:


(小心!记录显示今年你将不得不离开

回家,背负沉重的负担,遭受背叛和损失,吃得不好,

然后回来发现生活已经不再令人满意了。你可以避免这一切,但是,如果你只是把你的戒指借给我一下......)
Frodo Baggins wrote:

(Watch out! Records suggest that this year you will have to leave
home, carry a heavy burden, suffer betrayal and loss, eat poorly,
and return to find that life is no longer satisfying. You can
avoid all this, however, if you''ll just lend me your ring a moment ...)

我需要知道接收此指针的

函数中char *指向的内存块的大小。
I need to know the size of the memory block pointed to by a char* in a
function receiving this pointer.



传递大小作为参数。


或者,使用带有大小和指针的结构,而不是

只是char *值。


你不能从指针中找出有多少项目。

Pass in the size as a parameter.

Or, use structs with both the size and the pointer, rather than
just char* values.

You can''t find out just from the pointer how many items there are.


通常,此指针指向

字符串。 strlen()不会完成这项工作,因为有时字符串不是

null终止。
Typically, this pointer points to a
string. strlen() will not do the job since sometimes the string is not
null terminated.



如果它不是nul-terminated,那就不是C字符串。

If it''s not nul-terminated, it isn''t a C string.


那就是:


int foo(无效)

{

/ * ...... * /

char buf [20];

a(buf);

/ * ....... * /

返回0;

}

无效(char * ptr)

{

/ *这里,我需要由ptr * /

}指向的mem块的长度
That is:

int foo(void)
{
/* ...... */
char buf[20];
a(buf);
/* ....... */
return 0;
}
void a(char* ptr)
{
/* Here, I need the length of the mem block pointed at by ptr */
}



void a(int length,char * ptr)...


.... a(sizeof(buf),buf)...


-

克里斯希望不是Pyecroft Dollin

最好在朋友之间进行挑选。

void a( int length, char *ptr ) ...

.... a( sizeof (buf), buf ) ...

--
Chris "hopefully not Pyecroft" Dollin
Nit-picking is best done among friends.


1月2日晚上1点10分,dbtid< dbt ... @nospam.gmail.comwrote:
On Jan 2, 1:10 pm, dbtid <dbt...@nospam.gmail.comwrote:

对于声明为数组的对象,sizeof(a)将正常工作。
For objects declared as arrays, sizeof(a) will work just fine.



不,它不会。传递给函数时数组会衰减为指针,

因此,一旦在函数内部,获取大小信息为时已太晚了

(参见 http://c-faq.com/aryptr/aryptrparam.html 了解更多详情) 。正如

克里斯建议的那样,将大小作为参数传递给被调用函数。

-

WYCIWYG - 你得到的是什么

No, it will not. Arrays decay into pointers when passed to a function,
so once inside a function, it''s too late to get the size information
(see http://c-faq.com/aryptr/aryptrparam.html for more detail). As
Chris suggested, pass the size as a parameter to the called function.
--
WYCIWYG - what you C is what you get


这篇关于需要知道char *指向的内存块的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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