Jump to content

Hello guys! 
I'm starting to loose my mind over this little, simple task, but I fail to successfully resolve my problem. Here is my function:

 

char *strcat(const char *p1, const char *p2)
    {
       int len1 = strlen(p1);
       int len2 = strlen(p2);
       char rerun = new char[44];
        std::cout << "rerun legth/len1+len2 "<< strlen(rerun)<< " / " << len1+len2<< std::endl;
       strcpy(rerun, p1);
       strcat(rerun, p2);
       return rerun;
    }

 

here is what operates this in the main compartment: 


 

char *str = sajat::strcat("Small line.\n", "A little longer line.\n");
       cout << str;
       delete[] str;  

 

 

Thank you in advance :)

Link to comment
https://linustechtips.com/topic/739927-c-dynamic-char/
Share on other sites

Link to post
Share on other sites

To get your program to compile, "rerun" must be a pointer:

char *rerun = new char[44];

You must initialize the allocated memory, it is not guaranteed to contain zeros or anything sensible. The following call:

strlen(rerun)

could return anything as a result.

 

 

Link to comment
https://linustechtips.com/topic/739927-c-dynamic-char/#findComment-9383545
Share on other sites

Link to post
Share on other sites

42 minutes ago, Unimportant said:

To get your program to compile, "rerun" must be a pointer:


char *rerun = new char[44];

You must initialize the allocated memory, it is not guaranteed to contain zeros or anything sensible. The following call:


strlen(rerun)

could return anything as a result.

 

 

 

Yes, that's a pointer, I just missed it from here. How should I initialize the memory? what I wanted to do is:

 

char* rerun = new char[len1+len2];

 

but this fails miserably, gets into an infinite loop until windows terminates the process. Any idea why is this not working? 

Check attachment of the code and error.

 

Képkivágás.PNG

Link to comment
https://linustechtips.com/topic/739927-c-dynamic-char/#findComment-9383686
Share on other sites

Link to post
Share on other sites

6 hours ago, thewipyk said:

 

-snip-
 

what's in your main()?

 

edit:

Your rerun should contain enough space for both the strings and a  null terminating symbol '\0' since strcat adds it

char *rerun = new char[len1 + len2 + 1];

and as pointed out strlen(rerun) is not guaranteed to return what you expect since it has nothing in it yet.

 

without some kind of a loop I don't know why you get several lines in the console.

And also is "sajat" a namespace?

 

edit2:

your code works fine when i compile it, except i dont use "sajat"

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/739927-c-dynamic-char/#findComment-9384021
Share on other sites

Link to post
Share on other sites

6 hours ago, MisterWhite said:

what's in your main()?

 

edit:

Your rerun should contain enough space for both the strings and a  null terminating symbol '\0' since strcat adds it


char *rerun = new char[len1 + len2 + 1];

and as pointed out strlen(rerun) is not guaranteed to return what you expect since it has nothing in it yet.

 

without some kind of a loop I don't know why you get several lines in the console.

And also is "sajat" a namespace?

 

edit2:

your code works fine when i compile it, except i dont use "sajat"

No, you were correct. There is indeed not enough space in the buffer for the terminating 0, and thus the code is going out of bounds on the buffer, which is undefined behavior. Undefined behavior can appear  to work but there might just as well be nasal demons .

Link to comment
https://linustechtips.com/topic/739927-c-dynamic-char/#findComment-9385926
Share on other sites

Link to post
Share on other sites

8 hours ago, thewipyk said:

Yes, that's a pointer, I just missed it from here. How should I initialize the memory? what I wanted to do is:

 


char* rerun = new char[len1+len2];

 

but this fails miserably, gets into an infinite loop until windows terminates the process. Any idea why is this not working? 

Check attachment of the code and error.

 

Képkivágás.PNG

As pointed out by @MisterWhite, you allocate 1 byte short for the buffer, which causes undefined behavior.

 

Also some other observations:

-in C++, one should use std::string for string manipulation. This is just C code (except for the 'new' and 'delete').

-naked new/delete have been a big nono for a long time now, use std::unique_ptr (c++11) to wrap the pointer (RAII), preferable in combination with std::make_unique (c++14). This makes it exception safe, automatically cleans up the memory when the pointer goes out of scope and makes only one entity the clear and only owner of the pointer. make_unique will also initialise the memory to 0.

Link to comment
https://linustechtips.com/topic/739927-c-dynamic-char/#findComment-9385954
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×