C字符串函数 [英] C string functions

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

问题描述




我需要能为我返回空终止字符串的函数,对于

例如:


char * HTMLi(con​​st char * string)


此函数接收指向空终止字符串的指针,并且

应该返回< i>这是字符串< / i> ;.


为了使它更复杂,我甚至可以将上述

函数的结果返回到:


char * HTMLb(const char * string)


结果,这是我期望的< b>< i>这是

字符串< / i>< / b>


如何分配内存;我假设如果我在这些

函数中分配内存,并且从不释放它,这就是所谓的内存泄漏。


有人有一个示例代码吗?我有相同的逻辑吗?您的

反馈非常感谢!

谢谢,

Hi,

I need functions that would return null terminated strings for me, for
example:

char *HTMLi(const char *string)

This function receives a pointer to a null terminated string, and
should return <i>This was the string</i>.

To make it more complex, I could even return the result of the above
function to this:

char *HTMLb(const char *string)

As a result, this is what I expect to have <b><i>This was the
string</i></b>

How about allocating memory; I assume if I allocate memory in these
functions, and never free it, this is a so called memory leak.

Does somebody has a sample code for me having the same logic? Your
feedback is really appreciated!
Thanks,

推荐答案

< a href =mailto:gc ****** @ belgacom.net> gc ****** @ belgacom.net 于01/06/05写道:
gc******@belgacom.net wrote on 01/06/05 :


我需要为我返回空终止字符串的函数,例如:

char * HTMLi(con​​st char * string)

这个函数接收一个指向空终止字符串的指针,
应该返回< i>这是字符串< / i> ;.

为了使它更复杂,我甚至可以将上述
函数的结果返回到:

char * HTMLb(const char * string)

结果,这就是我所期望的要有< b>< i>这是
字符串< / i>< / b>

如何分配内存;我假设如果我在这些
函数中分配内存,并且从不释放它,这就是所谓的内存泄漏。

有人为我提供了具有相同逻辑的示例代码吗?您的
反馈非常感谢!

谢谢,
Hi,

I need functions that would return null terminated strings for me, for
example:

char *HTMLi(const char *string)

This function receives a pointer to a null terminated string, and
should return <i>This was the string</i>.

To make it more complex, I could even return the result of the above
function to this:

char *HTMLb(const char *string)

As a result, this is what I expect to have <b><i>This was the
string</i></b>

How about allocating memory; I assume if I allocate memory in these
functions, and never free it, this is a so called memory leak.

Does somebody has a sample code for me having the same logic? Your
feedback is really appreciated!

Thanks,




显而易见的方法:该函数返回一个已分配的块

使用后免费。


另一种解决方案可以适合。


定义尺寸:


size_t size = 128;

定义一个字符串

char * s = malloc(size + 1);


通过转换器功能保持大小并分配内存




s = HTMLi(s,& size,Hello world) ;

printf("%s",s);

fflush(stdout);


s = HTMLb(s, & size,s);

printf("%s",s);

fflush(stdout);


使用memmove()根据请求制作副本和realloc()(并相应地更新

大小)。在realloc()错误,只是exit(),或free()和

返回NULL,但应用程序必须检查每个

时间...(...可以简单地使用一些嵌入式转到... ...


#define CHK \

do {if(s == NULL)goto err;} while(0)

s = HTMLi(s,& size,Hello world);

CHK;

printf(" %s",s);

fflush(stdout);


s = HTMLb(s,& size,s);

CHK;

printf("%s",s);

fflush(stdout);


错误:


一旦完成,只需释放分配的集团。


-

Emmanuel

C-FAQ: http:// www。 eskimo.com/~scs/C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html


&qu ot; Mal nommer les choses c''est ajouter du malheur au

monde。 - Albert Camus。



The obvious way : the function returns an allocated bloc that you have
to free after use.

Another solution could fit.

define a size:

size_t size = 128;
define a string
char *s = malloc(size + 1);

Maintain the size and allocated memory through the converter functions
:

s = HTMLi (s, &size, "Hello world");
printf ("%s", s);
fflush (stdout);

s = HTMLb (s, &size, s);
printf ("%s", s);
fflush (stdout);

Use memmove() to make the copies and realloc() on request (and update
size accordingly). On realloc() error, just exit(), or free() and
return NULL, but the application would have to check against NULL each
time... (can be done simply with some embedded goto...)

#define CHK\
do {if (s==NULL) goto err;} while (0)
s = HTMLi (s, &size, "Hello world");
CHK;
printf ("%s", s);
fflush (stdout);

s = HTMLb (s, &size, s);
CHK;
printf ("%s", s);
fflush (stdout);

err:

Once finished, just free the allocated bloc.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c''est ajouter du malheur au
monde." -- Albert Camus.


2005年6月1日12:31:42 -0700, gc ****** @ belgacom.net 写道:
On 1 Jun 2005 12:31:42 -0700, gc******@belgacom.net wrote:
有人为我提供了具有相同逻辑的示例代码吗?您的
反馈非常感谢!
Does somebody has a sample code for me having the same logic? Your
feedback is really appreciated!




有几种方法可以在C中完成,但我想指出C

并不完全是字符串操作的首选语言(总是等于C的弱点)所以,也许你应该考虑另一个

编程您的应用程序的语言,如果它包含大量的

字符串处理(例如Perl)。



There are several ways to do it in C but I''d like to point out that C
is not exactly a language of choice for string operations (always been
a bit of a weak point of C) so, perhaps, you should consider another
programming language for your application if it contains lots of
string handling (Perl, for example).


Salut Emmanuel!


感谢您的明确解释!


我只有一个问题;如果我需要在函数中执行realloc,

这不会花费很多性能吗?

问候

Dirk

Salut Emmanuel!

Thank you for your clear explanation!

I have only one question; if I need to do a realloc in a function,
won''t this cost a lot in performance?
Regards
Dirk


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

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