Dynamic memory array / realloc

Looking for creating a dynamic array of string values.

The code example below was supposed to add a new array element (realloc) and a new line ("line 3"), which will be added to the array at runtime.

I assume the problem is pointers being misused and / or something wrong with realloc logic?

Appreciate any help.

Actual output I get:

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

The code:

#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]);
}
+3
source share
2 answers

- 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
+3

:

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

:

arrayIndexs += 1;

: sizeof - , .

EDTIED:

int arrayIndexs = 2;

int arrayIndexs = sizeof(*myArray) / sizeof(char*);
+2

All Articles