Realloc和strcpy

时间:2011-12-21 05:19:33

标签: c segmentation-fault realloc

我的代码给了我段错误:我不明白,调试器说错误来自于从stored_

打印值
char *stored_ = NULL;
char testMessage[15];

//strcpy(stored_, testMessage);

for (int a = 0;a < 10; a++)
{
    sprintf(testMessage,"Message::%i\n",a);
    printf("string is:%s;length is %i\n",testMessage,strlen(testMessage));

    stored_ = (char*) realloc (stored_, sizeof(char) * (strlen(testMessage) * (a+1) ));

    strcpy(&stored_[a], testMessage);
} 

for (int b = 0;b < 10; b++)
{
    printf("inside:|%s|\n",stored_[b]);
}

2 个答案:

答案 0 :(得分:5)

拳头向上,sizeof(char) 总是 1,你不需要乘以它。

其次,当你为字符串分配空间时,你必须使用:

malloc (strlen (string) + 1);

换句话说,你最后需要空字节。

第三,你似乎在字符指针和字符指针指针之间感到困惑。 stored_是一个字符块,stored_[1]只是stored_[0]之外的一个字节,这意味着您没有足够的空间来存储字符串。

stored_[n], n=:   0   1   2   3
                +---+---+---+---+
                |   |   |   |   |...
                +---+---+---+---+
                each of these cells is a single byte.

你要么必须自己管理单个字符块,为每个元素留下足够的空间(通过使用稀疏索引),或者有一个带有索引0,1,2等的字符指针块,但是你然后必须分别管理字符串分配。

以下代码显示了如何执行此操作:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void) {
    // An array of char pointers (C strings).

    char **stored_ = NULL;
    char testMessage[15];
    int i;

    // Populate them.

    for (i = 0; i < 10; i++) {
        sprintf (testMessage,"Message::%i",i);
        printf ("string is:%s;length is %i\n",testMessage,strlen(testMessage));

        // Reallocate array of char *, allocate room for string, then store it.

        stored_ =  realloc (stored_,sizeof (char*) * (i + 1));
        stored_[i] = malloc (strlen (testMessage) + 1);
        strcpy (stored_[i], testMessage);
    }

这就是它的核心,字符指针数组与形成C字符串的实际字符数组分开

然后下面的代码打印出来并清理。

    // Print them.

    for (i = 0; i < 10; i++) {
        printf("inside:|%s|\n",stored_[i]);
    }

    // Free all memory and return.

    for (i = 0; i < 10; i++) {
        free (stored_[i]);
    }
    free (stored_);

    return 0;
}

输出正如预期的那样:

string is:Message::0;length is 10
string is:Message::1;length is 10
string is:Message::2;length is 10
string is:Message::3;length is 10
string is:Message::4;length is 10
string is:Message::5;length is 10
string is:Message::6;length is 10
string is:Message::7;length is 10
string is:Message::8;length is 10
string is:Message::9;length is 10
inside:|Message::0|
inside:|Message::1|
inside:|Message::2|
inside:|Message::3|
inside:|Message::4|
inside:|Message::5|
inside:|Message::6|
inside:|Message::7|
inside:|Message::8|
inside:|Message::9|

使用此方法,每个单元格都是一个指向字符数组的指针,单独分配(保存C字符串):

stored_[n], n=:   0   1   2   3
                +---+---+---+---+
                |   |   |   |   |...
                +---+---+---+---+
                  |   |   |   |     +----------------------+
                  |   |   |   +---> | character array here |
                  |   |   |         +----------------------+
                  |   |   |         +----------------------+
                  |   |   +-------> | character array here |
                  |   |             +----------------------+
                  |   |             +----------------------+
                  |   +-----------> | character array here |
                  |                 +----------------------+
                  |                 +----------------------+
                  +---------------> | character array here |
                                    +----------------------+

答案 1 :(得分:0)

您似乎没有正确计算stored_的字符串长度。

您为testMessage分配&stored_[loopindex]的每个循环。我不确定这是否是预期的行为,但这是你正在做的事情,所以我希望你的第10次迭代给出字符串"MMMMMMMMMessage::9\n"

无论如何,testMessage始终是相同的字符数,因此stored_所需的存储空间可以计算为:

strlen(testMessage) // length of str to place at &stored_[a]
+ a                 // the loop index, where you're inserting testMessage
+ 1                 // important! extra char to hold the null terminator

不要忘记+1,C中的每个字符串都必须有null terminator的空格。