Jusauria
Jusauria

Reputation: 13

C: Realloc invalid pointer?

This is my task from school: Write a function ​insertString ​that inserts the string s2 into s1 at index n.s1 has been allocated using malloc and should be resized (the function is void again).

The program gave me a NULL on PC and when I switched to the phone the compilor said my realloc has a invalid pointer. But I really don't know what I did wrong.

Here is the code:

void insertString(char *str1, char *str2, int n){
    int lengStr2=strlen(str2);
    printf("%d %d ", lengStr2,n);
    printf("\nstr1= %s, str2: %s, n: %d ",str1,str2,n);
    str1=(char*)realloc(str1,lengStr2+n+1);
    if (str1==NULL){
        printf("Error\n");
        free(str1);
        return -1;
    }
        printf("\nstr1= %s, str2: %s, n: %d ",str1,str2,n);
    memcpy(str1+n,str2,lengStr2+1);
        printf("\nstr1= %s, str2: %s, n: %d ",str1,str2,n);
}
void testInsertString( char *str2, int n, char *expect){
    char*str1=(char*)malloc(3*sizeof(char));
    str1="hoi";
    printf("\nstr1= %s, str2: %s, n: %d ",str1,str2,n);
    insertString(str1,str2,n);
    printf("--> result:%s --> ",str1);
    (strcmp(str1,expect)==0)?printf("Success"): printf("Failure");
    free(str1);
    printf("\nIs Free\n");
}

Here the output:

str1= hoi, str2: Hallo, n: 1 5 1
str1= hoi, str2: Hallo, n: 1 Error
--> result:hoi --> Failure
Is Free

Process returned 0 (0x0)   

Please if you know what I did wrong can you show me the correct version? Even so I have the problem that I can't right a program right just by reading a text, I need to see how it should be written. So I need to see the right result to learn from the mistake ^^" (that's why some stuff in school is for me really hard). Thanks in advance ^^

Upvotes: 0

Views: 493

Answers (2)

dspr
dspr

Reputation: 2433

Here is a fixed version :

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

// Changing the signature to return the reallocated ptr would be safer
char * insertString(char *str1, char *str2, int n){
    int lengStr2=strlen(str2);

    //because str1 could differ from the received ptr after reallocation
    str1=(char*)realloc(str1,lengStr2+n+1); 
    if (str1==NULL){
        printf("Error\n");
        return NULL;
    }
    strcpy(str1+n,str2);
    return str1; // So we return the new ptr to the caller
}
void testInsertString( char *str2, int n, char *expect){
    char*str1=(char*)malloc(6*sizeof(char)); // The null termination must be counted
    strcpy(str1, "Hello");                   // You should use strCpy here instead of =
    str1 = insertString(str1,str2,n);        // Refreshes the ptr
    printf("\n--> result:%s --> ",str1);
    (strcmp(str1,expect)==0)?printf("Success"): printf("Failure");
    free(str1);
}

int main() {
    testInsertString(" World", 5, "Hello World"); // --> result:Hello World --> Success
    return 0;
}

Upvotes: 0

William Pursell
William Pursell

Reputation: 212654

char *str1 = malloc(3*sizeof(char));  /* Allocate memory */
str1="hoi";       /* Discard the only reference to the allocated memory */

The above two lines are similar in spirit to:

int x = 5; x = 7;

You need to copy the string "hoi" into the newly allocated memory, and you need to allocate at least 4 bytes to hold the string "hoi". For example:

char *hoi = "hoi";
char *str1 = malloc(strlen(hoi) + 1);
if( str1 == NULL ){
    perror("malloc");
    exit(EXIT_FAILURE);
}
sprintf(str1, "%s", hoi);

Upvotes: 2

Related Questions