Alex Stiles
Alex Stiles

Reputation: 161

Why can't I use realloc on pointer to array in function?

I want to be able to pass a string into a function that takes the array of characters that form the string and change it to the other string provided. The issue is that I get an error telling me that the pointer being reallocated (using realloc) has not been allocated (using malloc).

I made sure that the pointer being used in the function is the same as the one being used in the main routine as shown by the pointer print statements. I have tried not passing in the string char array address but just itself. None of these have worked.

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

void change(char **old_string, char *new_string);

int main(int argc, char *argv[]) {

    if (argc == 2) {
        char *string;
        string = malloc((strlen(argv[1]) + 1) * sizeof(char)); // + 1 for \0
        string = argv[1];
        printf("Pointer outside Function: %p\n", string);
        printf("String Before: %s\n", string);
        change(&string, "New String");     
        printf("String After: %s\n", string);
    } else {
        printf("Please enter a string to have changed.\n");
    }
    return 0;
}

void change(char **old_string, char *new_string) {
    printf("Pointer in Function: %p\n", *old_string);
    *old_string = realloc(*old_string, (strlen(new_string) + 1) * sizeof(char)); // + 1 for \0
    strcpy(*old_string, new_string);
}

Current results when run are:

Pointer outside Function: 0x7ffeed590c88 String Before: test Pointer in Function: 0x7ffeed590c88 a.out(20309,0x111496d40) malloc: * error for object 0x7ffeed590c88: pointer being realloc'd was not allocated a.out(20309,0x111496d40) malloc: * set a breakpoint in malloc_error_break to debug Abort trap: 6

What should happen:

Two strings should be provided to the function. The first argument being the pointer to chars and with the second being a string literal. The pointer to chars provided should be useable in the main routine.

Upvotes: 0

Views: 233

Answers (2)

John Bollinger
John Bollinger

Reputation: 181459

You allocated space and assigned string to point to it:

        string = malloc((strlen(argv[1]) + 1) * sizeof(char)); // + 1 for \0

Then, you replaced that pointer with a copy of the pointer stored in argv[1]:

        string = argv[1];

thereby leaking the allocated memory and making string point to space that your program did not allocate. It is thus perfectly natural that realloc() failed, since its pointer argument must be a valid pointer to the beginning of an allocated block. The error message is on point here.

It looks like instead of leaking the allocated memory, you meant to copy the contents of argv[1] into it. Instead of assignment (of the pointer), that would be

        strcpy(string, argv[1]);

Upvotes: 3

PeterSW
PeterSW

Reputation: 5271

This code:

string = argv[1];

sets the string char pointer to point to the same place that argv[1] points.

Instead you should do:

strcpy(string, argv[1]);

Upvotes: 4

Related Questions