c - 动态内存/realloc 字符串数组

标签 c arrays memory

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

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

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

感谢任何帮助。

我得到的实际输出:

Before: 
Array[0]: string 1
Array[1]: string 2
After: 
Array[0]: string 1
Array[1]: string 2

代码:

#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]);
}

最佳答案

如果您曾经发现自己试图对任何动态大小的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;
}

输出

Before: 
Array[0]: string 1
Array[1]: string 2
After: 
Array[0]: string 1
Array[1]: string 2
Array[2]: string 3

关于c - 动态内存/realloc 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950388/

相关文章:

c - 获取函数的地址

具有指针元素和动态分配的 C 程序足球队结构

c - 如何用c语言制作秒表和分秒表

javascript - 如何按 javascript 中的最后一个数字字符对文本数组进行排序

c - 可变内存位置与符号 vs pmap

c - 从创建/修改 malloc realloc 的内存块中读取值?

c - 结构成员初始化之间是否有顺序点?

php - 如何通过 Laravel 中数组中的对象保存数据

c - 两个元素之间的最大差异

c - 内存寻址和指针