Search code examples
cstringstackmallocrealloc

reallocating memory doesn't work in c


What I'm trying to do in my program is to copy the content of one string to another, in reverse. This part of the program works.

However, I don't want to limit the user for input, so I want to use malloc and realloc. This is my code:

#include <stdio.h>
#include <stdlib.h>
/*copy one string to another, in reverse*/
void copyStr(char *p, char *h){

   int i=0,j=0;
   int length=0;
   length=strlen(p); int l=length;
   for (i=0; i<length; i++){
       h[i]=p[l-1];
       l--;
   }
   char *temp=&h[0];
   for (i=0; i<length; i++){
       printf("%c",temp[i]);
   }


}
main(){
    printf("please enter a string\n");
    char c; int i=0; int end=10;
    /*allocate initial memory*/
    char *p=(char*)malloc(sizeof(end)); char *temp=p;
    while (c!='\n')
    {
        /*reallocate if needed*/
        if (i==(end-1)){
            end*=2;
             temp = realloc(p,end*sizeof(temp));
            if (temp!=NULL){
                /*this is for myself, to see what the error was*/
                printf("error allocating\n");
                exit(1);
            }
            else
                free(p);
        }
        c=getchar();
        p[i]=c;
        i++;
    }

    char h [sizeof(p)];
    copyStr(p,h);
}

I found out that the realloc function doesn't work and so I am asking for your help.

The program works if the input is very short (i.e 3 chars). If it longer than 10 letters, it will not reallocate memory. If it longer than 5, it will print reversly but will send me a message called "stack smashed". Thank you.


Solution

  • In fact, there are some little tricks to change :

    • the *temp=realloc(... should become temp=realloc(...
    • The fact that temp!=NULL is the normal behavior of realloc().
    • do not forget to change p after the realloc operation if you use it after.
    • sizeof(p) is the size of a pointer, that is 4 or 8... I turned it into char h [sizeof(char)*(i+1)];
    • i also added the \0 character at the end of the string. This is useful if you wish to print it or use strlen() and #include string.h. Then you can printf("the result is :\n%s \n",h);

    Here goes the code :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*copy one string to another, in reverse*/
    void copyStr(char *p, char *h){
    
        int i=0,j=0;
        int length=0;
        length=strlen(p); int l=length;
        for (i=0; i<length; i++){
            h[i]=p[l-1];
            l--;
        }
        //keep end-of-string character
        h[length+1]='\0';
        /* char *temp=&h[0];
       for (i=0; i<length; i++){
           printf("%c",temp[i]);
       }*/
        printf("the result is :\n%s \n",h);
    
    
    
    }
    main(){
        printf("please enter a string\n");
        char c; int i=0; int end=10;
        /*allocate initial memory*/
        char *p=(char*)malloc(sizeof(end)); char *temp=p;
        //signaling end of string
        p[0]='\0';
        while (c!='\n' && c!=EOF)
        {
            /*reallocate if needed*/
            if (i==(end-2)){
                end*=2;
                temp=(char*)realloc(p,end*sizeof(char));
                if (temp==NULL){
                    /*this is for myself, to see what the error was*/
                    printf("error allocating\n");
                    exit(1);
                }
                else{
                    p=temp;
                    printf("ok here\n");
                }
            }
            c=getchar();
            p[i]=c;
            i++;
        }
        //signaling end of string 
        p[i+1]='\0';
    
        printf("INVERTING STRING\n");
        char h [sizeof(char)*(i+1)];
        copyStr(p,h);
    
               free(p);
    }
    

    ! enif krow ot smees ti

    Bye,

    Francis