Thread: Ussing reaalloc

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    19

    Ussing reaalloc

    I need to use reaalloc to extend the memory when it goes out of bounds but I am not getting a correct output.


    this is how i am trying to realloc
    Code:
    if(strlen(theString) - strlen(searchFor) + strlen(replace)  >= size -1)
                        {
                            size += 80;
                         //   theString = strcpy(theString,theString);
                            theString = (char*)realloc(theString,size);
                            printf("realloc worked! size should be more");
                        }
    my declarations:
    stringHolder = theString, searchString = searchFor, substituteString= replace.
    Code:
        size = 6;
        stringHolder = (char*) calloc(size,sizeof(char));//, sizeof(char));
        searchString = (char*) calloc(10, sizeof(char));//, sizeof(char));
        substituteString = (char*) calloc(10,sizeof(char));//, sizeof(char));
    how i am printing it:
    Code:
        printf("\n%s", theString);
        printf("\n%d", strlen(theString));
    output:

    Please enter a string that is less then 80 characters:
    hello
    Please enter a substring to search:
    e
    Enter a string less then 10 characters to replace
    aaa
    realloc worked! size should be more
    `☼j
    3
    Process returned 0 (0x0) execution time : 6.906 s
    Press any key to continue.

    output should be:
    haaallo
    7

    Anyone advice or help?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Is the realloc in a different function from the declarations and printf?

    If it is, are you returning the realloc'd pointer from the function? If you don't return it somehow (plain return or pass a pointer to a pointer) then the calling function will still have the address of the old buffer.

    You should probably check that realloc actually did succeed before printing that it did

    It'd be helpful if you could post your whole source file, so we could see how functions are called. I guess you call realloc before trying to expand the string?

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    I checked if the value of size is getting passed to other functions and yes, using the same sample output i get the length of theString = 7(in the function), size = 86

    Both seem correct but length of theString (in main, and my print function) is 3 and not sure why. If i take out this realloc and use the same sample output I get the theString length = 7 and size = 6 which is correct.

    Not sure how realloc is screwing up length of theString which is the main string i am trying to increase the size of

    output without realloc:

    Please enter a string that is less then 80 characters:
    hello
    Please enter a substring to search:
    e
    Enter a string less then 10 characters to replace
    aaa
    5, 3, 6
    5, 3, 6
    6, 3, 6
    7, 3, 6
    7, 3, 6
    7, 3, 6
    6
    7
    haaallo
    7
    Process returned 0 (0x0) execution time : 5.012 s
    Press any key to continue.



    output with realloc:

    Please enter a string that is less then 80 characters:
    hello
    Please enter a substring to search:
    e
    Enter a string less then 10 characters to replace
    aaa
    5, 3, 6
    realloc worked! size should be more
    5, 3, 86 // printf("%d, %d, %d\n", strlen(theString), strlen(replace), *size);
    6, 3, 86
    7, 3, 86
    7, 3, 86
    7, 3, 86
    86 //this is in main. This is *size
    3 // this is strlen(theString)
    `☼6 //what theString %s prints
    3 //This is in my print function strlen(theString)
    Process returned 0 (0x0) execution time : 3.097 s
    Press any key to continue.
    Last edited by plot; 02-24-2013 at 09:38 PM. Reason: to provide outputs

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you post the whole code?
    This is especially important if you have functions doing the realloc for you - it seems you're not returning the pointer correctly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You should post a compilable small program which demonstrates your problem. Otherwise we can't help you.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed