C - Cannot execute function with pointers

-1

Hey guys this is my first time using stack overflow :D I'm getting this error:

Unhandled exception at 0x0105F338 (ucrtbased.dll) in Assignment_5.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD.

Exception thrown at 0x0105F338 (ucrtbased.dll) in Assignment_5.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD.

This is the main:

void Test2(char* arr[], int size_arr, char* str, int size_res, char* str_cmp)
{
    int size_res_new;
    char** tempStringArr = LowerSTR(arr, size_arr, str, &size_res_new);

    if (tempStringArr == NULL)
    {
        printf("Can't allocate arr (-4)\n");
        return;
    }

    if (size_res_new != size_res)
    {
        printf("The amount of string is not correct (-4)\n");
        return;
    }

    for (int i = 0; i < size_res; i++)
    {
        if (strcmp(tempStringArr[i], str_cmp) == 0)
        {
            return;
        }
    }
    printf("No String: %s (-4)\n", str_cmp);
}

This is the function:

char** LowerSTR(char* arr[], int size_arr, char* str, int* size_res)
{
    char** newArr = NULL, counter=0;
    newArr = (char**)malloc(size_arr * sizeof(char*));
    if(newArr == NULL)
    {
        printf("Memory not allocated.\n");
        exit(0);
    }

    for(int i=0; i<size_arr; i++)
    {
        if (strcmp(arr[i], str) < 0) {
            newArr[counter] = (char*)malloc(sizeof(char) * strlen(arr[i]));
            if (newArr[counter] == NULL)
            {
                printf("Memory not allocated.\n");
                exit(0);
            }
            strcpy(newArr[counter], arr[i]);
        }
        else
            counter++;
    }
    *size_res = size_arr - counter;
    newArr = (char**)realloc(newArr, sizeof(char*) * (*size_res));
    return newArr;
}
c
arrays
pointers
function-pointers
asked on Stack Overflow Dec 26, 2019 by Ryan Otweeng • edited Dec 27, 2019 by Ryan Otweeng

2 Answers

1
        newArr[counter] = (char*)malloc(sizeof(char) * strlen(arr[i])); // allocate strlen(arr[i]) bytes
        if (newArr[counter] == NULL)
        {
            printf("Memory not allocated.\n");
            exit(0);
        }
        strcpy(newArr[counter], arr[i]); // copy 1+strlen(arr[i]) bytes. oops.

You allocate strlen(arr[i]) bytes but then you try to copy arr[i] into the buffer you allocated. But it has one byte too few because a string has a nul character on the end. You need to allocate one more byte. A tool like valgrind should have told you about this.

answered on Stack Overflow Dec 27, 2019 by David Schwartz
0

In your LowerSTR function, you modify the counter variable in the else portion, but it should be in the if portion. Otherwise, you are incrementing it leaving bad values in the newArr function, and overwriting previously good values. This results in several values being overwritten by your realloc. The function should be more like the following:

char** LowerSTR(char* arr[], int size_arr, char* str, int* size_res)
{
    char** newArr = NULL, counter=0;
    newArr = (char**)malloc(size_arr * sizeof(char*));
    if(newArr == NULL)
    {
        printf("Memory not allocated.\n");
        exit(0);
    }

    for(int i=0; i<size_arr; i++)
    {
        if (strcmp(arr[i], str) < 0) {
            newArr[counter] = (char*)malloc(sizeof(char) * (strlen(arr[i]) + 1));
            if (newArr[counter] == NULL)
            {
                printf("Memory not allocated.\n");
                exit(0);
            }
            strcpy(newArr[counter], arr[i]);
            ++counter;
        }
    }
    *size_res = counter;
    newArr = (char**)realloc(newArr, sizeof(char*) * (*size_res));
    return newArr;
}
answered on Stack Overflow Dec 27, 2019 by ChrisMM • edited Dec 27, 2019 by ChrisMM

User contributions licensed under CC BY-SA 3.0