0

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.

4
  • 2
    Here is your problem string = argv[1]; You are assigning pointer to something that was not mallocated Commented Jun 11, 2019 at 0:16
  • Have you made sure the pointer to the memory you're trying to reallocate is the same as the pointer you got back when you allocated memory? Commented Jun 11, 2019 at 0:16
  • Note that in C sizeof(char) is guaranteed to be equal to one. Commented Jun 11, 2019 at 0:50
  • How to debug: Huh realloc crash. I get a message saying that the data was not allocated by malloc. So the pointer passed to realloc must be bad. Look at the function call, looks ok. Could there be a problem with malloc? Check the malloc code. And somewhere around there you should hear bells and whistles as you glance upon two adjacent lines in your code, the first one string = malloc ... and the next one string = something else. Commented Jun 11, 2019 at 7:46

2 Answers 2

4

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]);
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.