替换字符串0终止 [英] substitute for string 0 termination

查看:77
本文介绍了替换字符串0终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C-faq说malloc / free实现会记住分配和返回的每个块的大小

,因此没有必要提醒

它释放时的大小。


这长度信息是否可以用来代替

0字符串终止?


Felix

The C-faq says that "The malloc/free implementation remembers the size
of each block allocated and returned, so it is not necessary to remind
it of the size when freeing."

Could that length information somehow be used as a substitute for
0-termination of strings?

Felix

推荐答案

Felix Kater写道:
Felix Kater wrote:
C-faq说The malloc / free实现记住了分配和返回的每个块的大小
,因此没有必要在释放时提醒它的大小。

这个长度信息是否可以以某种方式用作替换字符串的0终止?

Felix
The C-faq says that "The malloc/free implementation remembers the size
of each block allocated and returned, so it is not necessary to remind
it of the size when freeing."

Could that length information somehow be used as a substitute for
0-termination of strings?

Felix




实现跟踪的长度是malloc()分配的

块的大小,假设成功,将至少为您请求的大小
。当然,你可以毫不费力地存储比这更短的字符串,并且长度会有所不同。

此外,实现决定跟踪的方式malloc()返回的块的
大小取决于实现。所以

您发现任何用于阅读实际分配大小的hack

完全不可移植。但是,您可以使用存储

长度(字符串)的结构,并使用该结构而不是终止

0.但是,自C库以来函数需要0终止

字符串,你将把这些函数传递给

这个函数要困难得多。但是,如果你预期对strlen()的重复调用,当长度为
不变时,那么跟踪

长度是有意义的,从而避免不必要的O( n)操作。


希望有所帮助,

- 约翰



The length that the implementation keeps track of is the size of the
block allocated by malloc() which, assuming success, will be at least
the size you requested. Of course, you can store strings which are
shorter than this without any trouble, and the length will be different.
Furthermore, the way the implementation decides to keep track of the
size of the block returned by malloc() is up to the implementation. So
any hack which you discover for reading the actual size allocated is
completely non-portable. You could, however, use a structure where the
length (of the string) is stored, and use that instead of a terminating
0. However, since the C library functions require 0 termination for
strings, you''ll have a much more difficult time passing the string to
these functions. It would make sense, though, to keep track of the
length if you anticipate repetetive calls to strlen() when the length is
not changing, thus avoiding needless O(n) operations.

Hope that helps,
--John


是的,你可以通过存储

第一个字符之前的长度来加快长度测试。 快速字符串仍然是

零终止,所以它们使用CRT字符串函数。


char * makeFastString(char * regularString)

{

size_t len,* p;

len = strlen(regularString);

p = malloc(len + 1 + sizeof(size_t) ));

if(!p)

返回NULL;

* p = len;

strcpy( (char *)(p + 1),regularString);

return(char *)(p + 1);

}


size_t lengthOfFastString(char * fastString)

{

size_t * p =(size_t *)fastString;

return *(p - 1);

}


void deleteFastString(char * fastString)

{

size_t * p =(size_t *)fastString;

免费(p);

}

Yes, you could speed up length tests by storing the length before the
first character like this. The "fast" strings are still
zero-terminated, so they work with the CRT string functions.

char * makeFastString( char *regularString )
{
size_t len, *p;
len = strlen( regularString );
p = malloc( len + 1 + sizeof( size_t ) );
if( !p )
return NULL;
*p = len;
strcpy( (char *)( p + 1 ), regularString );
return (char *)( p + 1 );
}

size_t lengthOfFastString( char *fastString )
{
size_t *p = (size_t *) fastString;
return *( p - 1 );
}

void deleteFastString( char *fastString )
{
size_t *p = (size_t *) fastString;
free( p );
}


哎呀错字:免费(p)应该是免费的(p - 1)

Oops typo: free( p ) should be free( p - 1 )


这篇关于替换字符串0终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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