动态内存/重新分配字符串数组

MrD*_*rDB 5 c memory arrays

希望创建一个字符串值的动态数组。

在下面的示例代码中,目的是在运行时添加一个新的数组项(realloc)和一个新的字符串(“string 3”)。

我想问题是指针使用不当和/或 realloc 逻辑有问题?

感谢任何帮助。

我得到的实际输出:

Before: 
Array[0]: string 1
Array[1]: string 2
After: 
Array[0]: string 1
Array[1]: string 2
Run Code Online (Sandbox Code Playgroud)

编码:

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

char **myArray;

main(int argc, char *argv[])
{
    int i = 0;
    myArray = malloc(2 * sizeof(char*));
    int arrayIndexs = sizeof(*myArray) / sizeof(char*);

    //Allocate memory for each [x]
    for (i = 0; i <= arrayIndexs; i++)
        myArray[i] = malloc(254 * sizeof(char*));
    //Populate initial values
    if(myArray != NULL)
    {
        strcpy(myArray[0], "string 1");
        strcpy(myArray[1], "string 2");
    }
    //Print out array values
    printf("Before: \n");
    for (i = 0; i <= arrayIndexs; i++)
        printf("Array[%d]: %s\n",i, myArray[i]);

    //Expand array to allow one additional item in the array
    myArray = (char **)realloc(myArray, sizeof(myArray)*sizeof(char*));

    //Allocate memory for the new string item in the array
    myArray[arrayIndexs+1] = malloc(254 * sizeof(char*));

    //Populate a new value in the array
    strcpy(myArray[arrayIndexs+1], "string 3"); //

    arrayIndexs = sizeof(*myArray)/sizeof(char*);

    //Print out array values
    printf("After: \n");
    for (i = 0; i <= arrayIndexs; i++)
        printf("Array[%d]: %s\n",i, myArray[i]);
}
Run Code Online (Sandbox Code Playgroud)

Who*_*aig 5

如果你曾经发现自己试图使用sizeof()东西,其大小动态,你这样做是错误的。记在脑子里。sizeof()在这段代码中被错误地重复使用。动态大小计数必须由管理;可以使用这些计数结合sizeof()所存储项目的基本类型来管理以字节为单位的分配大小要求。

鉴于:

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

int main(int argc, char *argv[])
{
    int i = 0;
    int arrayIndexes = 2;
    char ** myArray = malloc(arrayIndexes * sizeof(*myArray));

    //Allocate memory for each [x]
    for (i = 0; i < arrayIndexes; i++)
    {
        myArray[i] = malloc(254 * sizeof(char));
        sprintf(myArray[i], "string %d", i+1);
    }

    //Print out array values
    printf("Before: \n");
    for (i = 0; i < arrayIndexes; i++)
        printf("Array[%d]: %s\n", i, myArray[i]);

    // TODO: Fix this to check the result before orphaning the old
    //  value of myArray if an allocation failure ensues.
    myArray = realloc(myArray, (arrayIndexes+1) * sizeof(*myArray));
    ++arrayIndexes;

    //Allocate memory for the new string item in the array
    myArray[arrayIndexes-1] = malloc(254 * sizeof(char*));

    //Populate a new value in the array
    strcpy(myArray[arrayIndexes-1], "string 3"); //

    //Print out array values
    printf("After: \n");
    for (i = 0; i < arrayIndexes; i++)
        printf("Array[%d]: %s\n",i, myArray[i]);

    // TODO: write proper cleanup code just for good habits.
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出

Before: 
Array[0]: string 1
Array[1]: string 2
After: 
Array[0]: string 1
Array[1]: string 2
Array[2]: string 3
Run Code Online (Sandbox Code Playgroud)