Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

c - Why can't I give the results from realloc back? (you get the question when you see the code...)

The task is to make 2 different functions. 1 where malloc creates a length for char and put in their 2 Strings. The other function can't give a return type. It should change the lengh of the 2 fused Strings to fit in a 3rd one. But in some it doesn't give the results like it should. The first "Failure" should be one but the other 3 once shouldn't be one...

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

char *concatStrings( char* concaStr,char*str);
void concatStrings2( char* newString,char*str);
void testConcatStrings(char* concaStr,char*str, char*str2, char*expected);

int main()
{
    char *str="Dynamic";
    testConcatStrings("Memory", str,"Management","DynamicMemoryManagement");
    testConcatStrings("Memory ", str,"Management","DynamicMemoryManagement");
    testConcatStrings("Memory", str,"","DynamicMemory");
    testConcatStrings("Memory","" ,"Management","MemoryManagement");
    testConcatStrings("", str,"Management","DynamicManagement");
    testConcatStrings("Memory", "","","Memory");
    testConcatStrings("", str,"",str);
    testConcatStrings("", "","Management","Management");
    testConcatStrings("", "","","");

    return 0;
}

char *concatStrings( char* concaStr,char*str){
    //calculate suze for memory...
    int lenStr=strlen(str);
    int lenConcaStr=strlen(concaStr);
    //+1 byte for 0x00
    int len=lenStr+lenConcaStr+1;
    //reserve memory
    char *result =malloc(len);
    //concatenate string
    memcpy( result,str,lenStr);
    memcpy( result+lenStr,concaStr,lenConcaStr+1);
    printf("Hoi");
    return result;
}

void concatStrings2( char* newString,char*str){
    //calculate suze for memory...
    int lenStr=strlen(str);
    int lenConcaStr=strlen(newString);
    //+1 byte for 0x00
    int len=lenStr+lenConcaStr+1;
    //reserve memory
    printf("
Len: %d",len);
    newString=(char*)realloc(newString,len);
    printf(" %s %s ",newString,str);
    //concatenate string
    memcpy(newString+lenConcaStr,str,lenStr+1);
    printf(" %s %s ",newString,str);
}

void testConcatStrings(char* concaStr,char*str, char*str2, char*expected){
    //concatenated string by dynamic memory management(alloc)
    char *result = concatStrings(concaStr,str);
    concatStrings2(result,str2);
    printf("
str= %s, concat: %s, str2: %s--> result:%s -->", str,concaStr,str2,result);
    //check result
    (strcmp(result,expected)==0)?printf("Success"): printf("Failure");
    //never forget free!!!!
    free(result);
    printf("
Is free
");
}

Results:

Hoi
Len: 24 DynamicMemory Management  DynamicMemoryManagement Management
str= Dynamic, concat: Memory, str2: Management--> result:DynamicMemoryManagement -->Success
Is free
Hoi
Len: 25 DynamicMemory  Management  DynamicMemory Management Management
str= Dynamic, concat: Memory , str2: Management--> result:DynamicMemory Management -->Failure
Is free
Hoi
Len: 14 DynamicMemory   DynamicMemory
str= Dynamic, concat: Memory, str2: --> result:DynamicMemory -->Success
Is free
Hoi
Len: 17 Memory Management  MemoryManagement Management
str= , concat: Memory, str2: Management--> result:╚d -->Failure
Is free
Hoi
Len: 18 Dynamic Management  DynamicManagement Management
str= Dynamic, concat: , str2: Management--> result: -->Failure
Is free
Hoi
Len: 7 Memory   Memory
str= , concat: Memory, str2: --> result:Memory -->Success
Is free
Hoi
Len: 8 Dynamic   Dynamic
str= Dynamic, concat: , str2: --> result:Dynamic -->Success
Is free
Hoi
Len: 11  Management  Management Management
str= , concat: , str2: Management--> result:╚d -->Failure
Is free
Hoi
Len: 1
str= , concat: , str2: --> result: -->Success
Is free

(the results look so messy bc I tried to see exactly what happen in the functions and where exactly the error starts.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In concatStrings2 the variable newString is local. If realloc changes the location on the heap, then the new location is not passed back to the callee; you need to explicitly return the new location. You only had successes because realloc happened to be able to keep the char array in place (and enlarge it). Try to print the address on newString before and after the realloc, and you'll see that you have failures exactly when realloc changes the location.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...