Jared Hart
Jared Hart

Reputation: 153

C Cannot Access Memory at Address

I'm new to learning C, and I was practicing with memory allocations and pointers, however I've come a across a problem that I can't get around. When I debug the below code, I receive an error that states "Cannot Access Memory at Address" for line vowelStore[x] = vowelA;

char *DissVowel(char phrase[50])
{  
    char vowelStore[50];

    for(int x=0; x< strlen(phrase); x++)
    {
        switch(phrase[x])
        {
            char *vowelA = malloc(sizeof(*vowelA));

            case 'a':
                vowelA = phrase[x];
                vowelStore[x] = vowelA;
                free(vowelA);
                break;
            default:
                break;
        }
    }

    return vowelStore;
}

Basically, this function takes in a array of characters(a string phrase), then cycles through each character, and if the current character is "a", a memory piece is allocated, and "a" will be stored in the allocated space. Next, the address of this space containing "a" will be stored in a separate array, which will contain several address. The function then returns this array of address.

Any help is much appreciated!

Upvotes: 0

Views: 6382

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263657

There are a number of problems with your code.

char *DissVowel(char phrase[50])

This is legal but misleading. C does not actually have array parameters. If you define what looks like an array parameter, as you've done here, it's "adjusted" to become a pointer parameter -- and the length is quietly ignored. The above is exactly equivalent to:

char *DissVowel(char *phrase)


for(int x=0; x< strlen(phrase); x++)

This is legal but inefficient. strlen() has to scan the string from the beginning to find the terminating '\0' null character that marks the end of the string. You call strlen on each iteration. Either save the string length in a variable and test that in the condition, or use a different condition such as phrase[x] != '\0'. (A very minor point: i is a more conventional name for an iteration variable used as an index.)

    switch(phrase[x])
    {
        char *vowelA = malloc(sizeof(*vowelA));

        case 'a':
            /* ... */
            break;
        default:
            break;
    }

A switch statement is essentially a computed goto. When the switch is executed, it jumps directly either to the case 'a': label or to the default: label, skipping over anything in between. Since this enters a new scope, memory is allocated for the pointer object vowelA itself, but its initialization is not executed, so vowelA has an indeterminate value.

return vowelStore;

vowelStore is a local array variable. In this context, it's "converted" to a pointer to the array's initial element, equivalent to &vowelStore[0]. The array variable ceases to exist when you return from the function, so the value you're returning is a dangling pointer.

You should find out how to enable more warnings in your compiler; it should be able to warn you about at least some of these problems.

Upvotes: 1

Related Questions