Memory Allocation , function array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Memory Allocation , function array

What is the problem with memory allocation here ? Also , if i want to use this code for a sentence what to do ? https://code.sololearn.com/c6pFz8Ku2Mz8/?ref=app

5th Feb 2023, 7:06 PM
Shimaa
Shimaa - avatar
29 Answers
+ 5
Dark beware that the ToPigLatin function is writing outside the bounds by 1 character of the allocated space for word. The strcat function presumes that you have allocated enough space for the original string plus its concatenation.
5th Feb 2023, 9:59 PM
Brian
Brian - avatar
+ 4
Dark the realloc line needs a type specifier. Insert (char *) in front of realloc.
5th Feb 2023, 9:56 PM
Brian
Brian - avatar
+ 4
Dark the C++ compiler is picky about having different data types on both sides of the assignment operator. Is just ensuring that it was an intentional conversion (from void* to char*).
5th Feb 2023, 10:03 PM
Brian
Brian - avatar
+ 3
Dark To resolve the out-of-bounds issue, maybe add 1 to the size in your main realloc. Overall, I would simplify the program by using the C++ string data type instead of char arrays. That eliminates messy pointer and memory management. In fact, once that is done, the whole program can be reduced to two lines. Let me add, though, that this has been good practice for you to try out malloc/realloc, and understand where memory management can go wrong. It would be good to get this working, bug free, and then revamp to use strings instead.
5th Feb 2023, 10:10 PM
Brian
Brian - avatar
+ 2
ArsenicolupinIII there is no need to free the original memory that you pass into realloc. Realloc handles that for you. In fact, realloc might be able to adjust the size in place without changing the location - especially in a case like this where the size is merely being shrunk. After a malloc you may call realloc many times, and realloc will do any necessary freeing. Only worry about freeing the memory once after you are completely done with it.
5th Feb 2023, 9:54 PM
Brian
Brian - avatar
+ 2
Dark in C you can use scanf to do the same thing: while (scanf("%s", word) != EOF)
6th Feb 2023, 4:07 PM
Brian
Brian - avatar
+ 2
Dark if you already have the whole sentence in a string then there are several other ways to break the string into words. - It is possible to redirect cin so it takes the input from the string instead of from the console. - The C string library function strtok() can search the string for spaces and replace them with nulls. Then subsequent calls to strtok in a while loop return a pointer to the next word in the string, then the next one, the next one, and so on. This technique should be done after you make a copy of the original string, because strtok modifies the string that you pass to it. - sscanf is a version of the C scanf function that can read input from a string instead of the console. Each time through the loop you would have to adjust the input string pointer to start past where you left off in the previous read. It is easy to do, but less convenient than simply using scanf on console input. Are you ready to see a two-line solution? Here it is in C: https://code.sololearn.com/cL5y60YJcnu9/?ref=app
6th Feb 2023, 4:45 PM
Brian
Brian - avatar
+ 1
The problem with memory allocation in this code is that the value of "word" is changed by realloc, but the original memory location that was allocated with malloc is not freed. This can lead to memory leaks and make the program less efficient. To fix this, you need to free the memory after you are done using it by calling free(word). To translate a sentence, you need to modify the code to loop through each word in the sentence, and call the ToPigLatin function for each word. You can use the getline function or strtok function to split the sentence into words, then call the ToPigLatin function for each word, and finally print the result.
5th Feb 2023, 7:20 PM
ArsenicolupinIII
+ 1
The code appears to have a problem with multiple runs because the strcat function is concatenating the new string to the original "n_word" array, but it doesn't clear the array before each run. This results in the new string being concatenated to the end of the previous string, leading to incorrect output. To fix this issue, you need to clear the "n_word" array before each run using memset or by manually setting all elements to '\0'.
5th Feb 2023, 7:30 PM
ArsenicolupinIII
+ 1
6th Feb 2023, 6:43 AM
José Ricardo
José Ricardo - avatar
+ 1
Brian Oh , i was struggling with those loop inputs You've just made my day 😅
6th Feb 2023, 4:23 PM
Shimaa
Shimaa - avatar
+ 1
Brian , wow 😂 It blows my mind , like how , where when 😂😂😂 Two lines ! And i have gone through function and main and loops and arrays ! I want to be like this 😂😂😂
6th Feb 2023, 5:34 PM
Shimaa
Shimaa - avatar
+ 1
Dark yah I simply created a wormhole from the input to the required output and bypassed the processing step. 😄
6th Feb 2023, 6:49 PM
Brian
Brian - avatar
5th Feb 2023, 7:07 PM
Shimaa
Shimaa - avatar
0
When i run this code , it works once , then it keeps resulting in different results .
5th Feb 2023, 7:23 PM
Shimaa
Shimaa - avatar
0
ArsenicolupinIII same error when adding free ,.
5th Feb 2023, 7:27 PM
Shimaa
Shimaa - avatar
0
ArsenicolupinIII error with line that has realloc function. , I can not understand it.
5th Feb 2023, 9:27 PM
Shimaa
Shimaa - avatar
0
Brian why it keeps telling that there is error when i do realloc .?
5th Feb 2023, 9:56 PM
Shimaa
Shimaa - avatar
0
Do i have to add the type specifier. Every time . ? Isnt a pointer declaration as a char enough . ? Brian
5th Feb 2023, 9:59 PM
Shimaa
Shimaa - avatar
0
Brian how to avoid that ? Increase length by 1 ?
5th Feb 2023, 10:02 PM
Shimaa
Shimaa - avatar