I need help with this program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I need help with this program

https://code.sololearn.com/cooQl53Ez9lV/?ref=app The program Just needs to concatenate two strings, and the new string made by the two previous strings needs to be allocated in dynamic memory

2nd Apr 2020, 9:05 AM
Massimiliano Messina
Massimiliano Messina - avatar
11 Answers
+ 2
malloc() is very similar to new(), with the difference being that new() expects the number of objects you want to allocate space for, while malloc() needs the actual size of the requested storage in bytes, e.g. to obtain space for five integers, you pass five to new(), i.e. "new int[ 5 ];", but to malloc() five multiplied by the size of an integer, i.e. "malloc( 5 * sizeof( int ) );". Either way, your allocation process is wrong. First of all, "char array[ size ];" is a static allocation, and moreover, size hasn't even been initialized, so the array might be of whatever size. Then you allocated space for a single character, not an array, and have the pointer point to the statically allocated array, which leaves you with unaccessible dynamic memory (a memory leak), and a variable-sized array of unknown length, which is standard-wise okay for C, but not really for C++. As a suggestion, you might want to revisit some tutorials on dynamic memory allocation. MWE: https://code.sololearn.com/cb4TlwxsC8Iy/?ref=app
2nd Apr 2020, 10:14 AM
Shadow
Shadow - avatar
+ 2
I just provided a minimal working example as a reference for you. Simply put the for-loops that do the actual copying into a function with three parameters like you did in your original code, I updated my sample that way for your convenience. If you want to label it C++, just use the corresponding C++ headers instead of the C headers and maybe std::cin and std::cout for the input and output, and voila, valid C++ that you should be good to go with. I'm sure you can do the porting on your own. Not sure why to use C++ in the first place though if you then do all the stuff with C anyway instead of using modern C++ features.
2nd Apr 2020, 11:59 AM
Shadow
Shadow - avatar
+ 2
I already figured as much, it was not targeted towards you in any way, sorry if it sounded like it was. :)
2nd Apr 2020, 12:05 PM
Shadow
Shadow - avatar
+ 1
Is this an assignment? is there a restriction that prevents you from using C++ string? I see you're doing C string works in your attempt for concatenating two strings. This isn't necessary with C++ string.
2nd Apr 2020, 9:26 AM
Ipang
+ 1
Let me check the code, I'll get back to you later ... BTW, do you really have to write this in C++ or a C code be acceptable by instructors? In the meantime, here's a reference for `malloc` http://www.cplusplus.com/reference/cstdlib/malloc/
2nd Apr 2020, 10:13 AM
Ipang
+ 1
I don't choose how to do this
2nd Apr 2020, 12:01 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
yes it is, I have to use c strings, But i should also have to use malloc() but i don't understand how to use it, so i used new
2nd Apr 2020, 9:29 AM
Massimiliano Messina
Massimiliano Messina - avatar
0
I need to this in C++ and with functions
2nd Apr 2020, 11:41 AM
Massimiliano Messina
Massimiliano Messina - avatar
0
It's ok
2nd Apr 2020, 12:07 PM
Massimiliano Messina
Massimiliano Messina - avatar
0
Shadow i don't understand how did you allocated the string, don't have to use sizeof? shouldn't i also free the Memory? and then also have in the malloc() the lenght of s1, S2 but why +1? what Is size_t i? what type Is It?
3rd Apr 2020, 11:56 AM
Massimiliano Messina
Massimiliano Messina - avatar
0
Massimiliano Messina Of course you should free the memory, my bad. I totally forgot about that when writing the code. The size of a character is usually exactly one byte, so you would multiply the number of characters by one, which is the same as omitting the multiplication. Using sizeof is not mandatory, but usually a good thing because data types like int are not required to be four bytes long, depending on the machine (usually they are, though). I allocated one additional character for the null character at the end of the string, which is mandatory for C-style strings. size_t is a special data type commonly used for things such as array indexing, looping, and similar tasks because it guarantees to be big enough to be able to store the size of the biggest object the host environment can handle, plus it's never negative. It's usually a typedef for either unsigned int or unsigned long long, depending on the compiler: https://en.cppreference.com/w/c/types/size_t
3rd Apr 2020, 1:36 PM
Shadow
Shadow - avatar