Thursday, February 10, 2011

想起来昨天有几个问题没有答好

1. 在C里面,string的data structure是什么?

我想来想去,C的string不就是用char *来表示的吗?比如以下的code:
---
char * pStr = malloc(SOME_LEN + 1); /* + 1 for the ending 0x00 byte */
strcpy(pStr, ...);
---
不就是一块continuous memory area吗?

Scott似乎对这个答案不满意,但我实在也没想起来是什么。后来他告诉我C里面string是通过character array实现的。嗯,从数据结构的角度讲是这样的,不知为什么我总是没想起来。不过后来他问我这个array到什么时候终止我倒是答上来了,terminated with null character (0x00),这个简单。



2. 同样是在C语言里面,除了strcat()以外,有没有其他concatenate string的方法?

我当时只能想出strcat()的方法,比如下面的帖子:
---
In C, strings are just char arrays. So you can't add to them directly.

Here is an example from cplusplus.com:

char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");

So in your example, you would want:

char *foo = "foo";
char *bar = "bar";
char str[80];
strcpy (str, "TEXT ");
strcat (str, foo);
strcat (str, bar);

The return value of strcat can just be ignored, it returns the same pointer as was passed in for the first argument. You don't need this value. The return value is simply for convenience, so it allows you to chain the calls into one line of code.

For the first parameter, you need to have the destination buffer itself. The destination buffer must be a char array buffer. Example char buffer[1024];

Be very careful that the first parameter has enough space for what you're trying to copy in. If available to you, it is safer to use functions like: strcpy_s and strcat_s which explicitly specify the size of the destination buffer.

You can never use a string literal as a buffer, you must always use your own buffer.

by Brian R. Bondy in http://stackoverflow.com/questions/308695/c-string-concatenation
---
对我提的strcat()的答案,Scott应该是不满意的,但他当时也没有说成还有什么别的方法。


今天早上去跑步的时候,想到这个问题还是应该再琢磨一下,就去网上搜索了一下,看到下面的帖子:
---
Avoid using strcat. The cleanest and, most importantly, the safest way is to use snprintf:

char buf[256];
snprintf(buf, sizeof(buf), "%s%s%s%s", str1, str2, str3, str4);
 
by Alex B in http://stackoverflow.com/questions/308695/c-string-concatenation
 
sizeof() only works here for char buf[...]. NOT for char * buf = malloc(...). There aren't many differences between arrays and pointers, but this is one of them!
 
by Mr.Ree in http://stackoverflow.com/questions/308695/c-string-concatenation
 
Here's my problem: I have an array, which contains a command a[1], followed by several command args a[2], a[3], ...

What I need to do is the following

    •Create a string, consisting of the cmd and a combination of args e.g.: cmd arg1 arg2 arg3
    •Execute that command-string

Here's how I woud do it (pseudo-code):

    1.Precompute the length of each arg and store it in an array
    2.Get a combination (using GNU Scientific Library)
    3.Compute the size in bytes needed to allocate the string (length of cmd + 1 + lengthof arg1 + 1 + argn-1 + 1) (the +1 generally for the for the blank and at the end for the \0)
    4.Build the string by using strcat
    5.Execute the command-string

Well, it works but I wonder if using strcat that deliberately is actually efficient/the right way to do it. Any suggestions?

by Alok in http://stackoverflow.com/questions/2272418/efficient-string-concatenation-in-c

No, using strcat() is not efficient since it has to step through the string to find the end each time you call it.

Much better to either do it all at once using snprintf() if you have it (and can squeeze your arguments in there), or do it yourself using direct pointer manipulation.

Of course, for this to matter in practice you need to be running this command very often indeed.

by unwind in http://stackoverflow.com/questions/2272418/efficient-string-concatenation-in-c

strcat(), as well as all string manipulation functions from the standard library, is inefficient. this is due to way strings are stored in C, namely zero-terminated, thus each function has to find the end of the string by iterating over each character.

anyway, you are doing a premature optimization: the multiple strcat() calls here will execute really fast compared to the command execution, so you should not worry about the efficiency of your way of concatenating.

before optimizing part of a code, you have to show that it is a bottleneck and that optimizing will really improve execution time. in most cases, there is no need to optimize: it is simply not worth the time spent.

by Adrien Plisson in http://stackoverflow.com/questions/2272418/efficient-string-concatenation-in-c
---
哈,看到这些帖子我才想起来,以前我在写code的时候,确实在strcat()以外,还用过snprintf()来concatenate strings,昨天电话面试的时候,没想起来的原因可能是因为有一段时间没写code了,多少有些生疏所致。

No comments:

Post a Comment