0

Edit: The problem was that I was not allocating memory for '\0' character.

Basically, I am having core dumped and corrupted top size errors while trying to run this code, not used to malloc and dynamic memory. My goal here is to concatenate two strings and use an allocated memory to do so. The first 'if' will concatenate only the not NULL strings, if both are regular strings, it will measure the length and allocate memory for it. The code compiles but the error happens when executing it

int str_length(const char *str){
    int i=0;

    if(str == NULL) return (-1);

    while( *(str+i) != '\0') i++;

    return i;
}

This function I use to measure the size of arrays. I cant use common libraries

char* str_concatenate(const char *stra, const char *strb){
    char *concatenate;
    int i=0;

    if(stra==NULL || strb == NULL){ 
        if(stra==NULL && strb == NULL) return (concatenate = NULL);

        if(strb==NULL){
            concatenate = (char*) malloc( sizeof(char)* (str_length(stra) + 1) );   //se a segunda e NULL, copia a primeira
            do{
                concatenate[i]=stra[i];
                i++;
            }while(stra[i]!='\0');
            return concatenate;
        }

        else{
            concatenate = (char*) malloc( sizeof(char)* (str_length(strb) + 1) );   //primeira NULL copia a segunda
            do{
                concatenate[i]=strb[i];
                i++;
            }while(strb[i]!='\0');
            return  concatenate;
        }
    }
    

    int size_a = str_length(stra);
    int size_b = str_length(strb);
    int total = size_a + size_b;

    concatenate = (char*) malloc( sizeof(char)*(total + 1) );



    while(strb[i]!='\0'){
        concatenate[size_b+i] = strb[size_b+i];
        i++;
    }

    i=0;

    while(stra[i]!='\0'){
        concatenate[i] = stra[i];
        i++;
    }

    return concatenate;
}
int main(){
    char stra[] "first string"
    char strb[] "second string"

    str_concatenate(stra, strb);

    return 0;
}
Porton_
  • 113
  • 1
  • 5
  • 1
    *str_lengh is another function I created*. No incomplete and missing code please. Please provide a [complete minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). Also, suggest using a tool like [valgrind](http://valgrind.org) to help find such memory corruptions. – kaylum Feb 20 '21 at 21:00
  • 1
    One problem is that you are not NUL terminating the `concatenate` buffer. So the returned value is not a valid C string. – kaylum Feb 20 '21 at 21:04
  • You probably aren't allocating memory for the `'\0'` at the end of the string, and the code definitely isn't putting the `'\0'` at the end of the string. – user3386109 Feb 20 '21 at 21:05
  • `int total = tam_a + tam_b;` You can't be showing exact real code as `tam_a` and `tam_b` are undeclared variables. – kaylum Feb 20 '21 at 21:06
  • Don't forget that `char` strings in C are really called ***null-terminated** byte string*. You need to add the null.terminator character `'\0'` to all strings, and it of course need space allocated for it (which means a string of length `X` needs `X + 1` characters to fit the terminator). – Some programmer dude Feb 20 '21 at 21:08
  • Read this link to see how to handle malloc https://stackoverflow.com/q/605845/6699433 – klutt Feb 20 '21 at 21:19

1 Answers1

0

Note that you are resetting i so essentially you are overwriting concatenate in the second while, you should try using a different variable to keep track of the index of concatenate, for instance:

Live demo

//...
// size of char is always 1 byte, no need to include in malloc
concatenate = malloc(total + 1); // don't forget the null byte

i = 0;

while (strb[i] != '\0')
{
    concatenate[i] = strb[i];
    i++;
}

int j = 0;

while (stra[j] != '\0')
{
    concatenate[i] = stra[j];
    i++;
    j++;
}
concatenate[i] = 0; // null terminate the string, the same goes for the others
//...
anastaciu
  • 23,467
  • 7
  • 28
  • 53