English 中文(简体)
C 护卫
原标题:C string append

I m looking for an efficient method for appending multiple strings.
The way it should work is C++ std::string::append or JAVA StringBuffer.append.

我写了一种功能,实际上真实地真实了原发源人,确实是cat。

我认为,这不是一种有效的方法,因为汇编者可以实施这种免费的小型活动。

Other way I could think of (like std::vector) is allocate memory in bulk (1KB for eg) and do strcpy. In that case every append call will check if the total required allocation is more than (1200 bytes) the amount allocated in bulk, realloc to 2KB. But in that case there will be some memory wasted.

我期望在以上两点之间取得平衡,但优先选择是业绩。

还有其他可行的办法。 建议。

最佳回答

I would add each string to a list, and add the length of each new string to a running total. Then, when you re done, allocate space for that total, walk the list and strcpy each string to the newly allocated space.

问题回答

传统做法是每当缓冲区太小时翻一番。

最初是“合理的”缓冲,因此,你不必填写<代码>realloc(>,其大小为1、2、4、8、16,这些大小将受到你大量扼杀。

从1024年开始,如果你在2048年点击,你就会有一个<代码>realloc(>,第二个如果你打了4096,等等。 如果大量记忆消耗给你,那么,一旦出现像65536.或任何东西一样的相当大的点子,增长率就会受到限制,这取决于你的数据和记忆宽容。

还确保你缓冲目前的时间长度,因此,你可以做到strcpy(,而不必步行,首先寻找时间。

2. 组合体积的样本功能

void
addToBuffer(char **content, char *buf) {
    int textlen, oldtextlen;
    textlen =  strlen(buf);
    if (*content == NULL)
        oldtextlen = 0;
    else
        oldtextlen = strlen(*content);
    *content = (char *) realloc( (void *) *content, (sizeof(char)) * (oldtextlen+textlen+1));
    if ( oldtextlen != 0 ) {
        strncpy(*content + oldtextlen, buf, textlen + 1);
    } else {
        strncpy(*content, buf, textlen + 1);
    }
}

int main(void) {
    char *content = NULL;
    addToBuffer(&content, "test");
    addToBuffer(&content, "test1");
}

我愿这样做:

typedef struct Stringbuffer {
    int capacity;     /* Maximum capacity. */
    int length;       /* Current length (excluding null terminator). */
    char* characters; /* Pointer to characters. */
} Stringbuffer;

BOOL StringBuffer_init(Stringbuffer* buffer) {
    buffer->capacity = 0;
    buffer->length = 0;
    buffer->characters = NULL;
}

void StringBuffer_del(Stringbuffer* buffer) {
    if (!buffer)
        return;

    free(buffer->characters);

    buffer->capacity = 0;
    buffer->length = 0;
    buffer->characters = NULL;
}

BOOL StringBuffer_add(Stringbuffer* buffer, char* string) {
    int len;
    int new_length;

    if (!buffer)
        return FALSE;

    len = string ? strlen(string) : 0;

    if (len == 0)
        return TRUE;

    new_length = buffer->length + len;

    if (new_length >= new_capacity) {
        int new_capacity;

        new_capacity = buffer->capacity;

        if (new_capacity == 0)
            new_capacity = 16;

        while (new_length >= new_capacity)
            new_capacity *= 2;

        new_characters = (char*)realloc(buffer->characters, new_capacity);
        if (!new_characters)
            return FALSE;

        buffer->capacity = new_capacity;
        buffer->characters = new_characters;
    }

    memmove(buffer->characters + buffer->length, string, len);
    buffer->length = new_length;
    buffer->characters[buffer->length] =   ;

    return TRUE;
}




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签