I need your help. | Sololearn: Learn to code for FREE!

+3

I need your help.

https://code.sololearn.com/cmMHEst295UB/?ref=app My intention with this code is basically storing words of sentences. For example - I've sentence "Hello what's up", So,I want to store Hello, What's,up in respective index in an array. I'm trying to do this by stringstream and dynamic memory allocation. But, It's not working, Could anyone please fix it?

c++

5/21/2022 9:06:24 AM

Manav Roy

38 Answers

New Answer

+10

Do you really need to bother with dynamic memory allocation? Like is this for a school assignment or something? I m asking because I dont see why you would need to reinvent the wheel. The STL library has something called "vector" which is a dynamic array. https://code.sololearn.com/cs7Eh8TBevUS/?ref=app

+8

Although Ketan Lalcheta and ChillPill 🌢 have already suggested you some nice alternatives. But If you are looking for reasons your particular code is not working then here's why : 1) Line 9: stringstream s; The object is created with default constructor ( initialised with empty underlying basic_stringbuf object ) 2) Line 12: while(s>>sentence) Now you are attempting to read from an empty buffer ( which is similar to reading from empty stdin via cin ) and your program will never enter the while loop. 3) Line 19: cout<<words[0]; Now as program never entered the while loop, here, you are attempting to read an uninitialised pointer "words", causing undefined behaviour.

+4

You are allocating memory in loop.... So for second time in loop, you are allocating memory which is not freed yet... Calloc is used to allocate memory fresh... Memory which is not freed and need to allocate again need to be handled by realloc As you are messing up with memory allocation in loop , you are not getting output properly

+2

Couple of points wrong here at first glance... 1. Initially I is zero on line 14 and you are trying to calloc with 0 2. == for comparision , not assignment Why going with calloc and all for this ? Can easily loop through string stored in sentence and as soon as space is found, add it to string array

+2

Not optimized but working code as below : https://code.sololearn.com/cC2J9cUZT2uI/?ref=app

+2

What you expected from your code ? It is not printing anything so you are not able to see any output And you can check what is missing in your code by comparing what I have done additionally ... If you don't get purpose of additional statements in my code , I would be happy to clarify that

+2

Arsenic I've added bit modification,But it's throwing segmentation fault πŸ₯Ί

+2

Bob_Li Hello, In your code , You're counting number of words first and then specifying blocks in calloc. But this could also be easily done with static memory allocation. My intention of using dma is nothing but creating the size of the "string" array according to user input. For Example - If user enters Hello What's up So, There're 3 words(Within space basically). So ,I want that a string array of size 3 automatically creates and that three words store in it. I'm basically targeting to conserve memory and making my code more efficient. I would like to use string stream and dma in my code.

+2

Manav Roy I don't know. But people who write compilers probably would try to keep memory usage and performance as balanced as possible. Like people and cars. Some like to tinker with the engine to supercharge it, some just drive and trust the manufacturer. I guess I'm the trusting type. manual memory allocation is a delicate thing. It's easy to mess things up.

+2

Manav Roy wanted to share realloc as you were looking for : https://code.sololearn.com/cnArHgNHewEO/?ref=app @All : please share if something is there to improve on existing code

+2

Manav Roy Ketan Lalcheta 's code is really what you are thinking about. 😎 But it have warnings about calloc, realloc and suggestion on using new and delete. Here is Ketan Lalcheta's code modified using new and delete. No need for cstdlib, calloc, realloc and temp, too. Which is a good thing. Just remember to clean up the new string object. But this code cannot handle long strings and too many words. maybe it is really necessary to use vectors. https://code.sololearn.com/cHZ3TefWQJdB/?ref=app

+1

Try referring code I just posted in comment section.it is working code

+1

Ketan Lalcheta Though the code is working fine ,But what's the issue with my code?I want to do that with my own code πŸ™ƒ

+1

https://code.sololearn.com/cu2u8RnniVPL/?ref=app Check if this helps

+1

Manav Roy with C++ you don't really have to use C-style memory allocations. Use new and delete instead. Much easier. https://code.sololearn.com/cA8d5FJOnjU8/?ref=app

+1

Manav Roy or just use vector. The need for manual memory management is really niche. Good to know but rarely needed. https://code.sololearn.com/cyzJPng4Ec0H/?ref=app

+1

Manav Roy Although the updated version will allow the the programm to enter the while loop, it is opening to a new lot of problems. Unfortunately, using malloc/calloc will only give you raw memory to operate on and doesn't actually "construct" the object properly there, hence as already suggested by others, a better option would be to use new/delete instead of malloc/calloc here ( or use a stl container which does it for you ).

+1

Manav Roy Here is an example closer to your intention. No vector, just a string array with size based on the number of newlines in the sstream object. There is also no need for pointer, new and delete, too. https://code.sololearn.com/caylVJ7iC6Z0/?ref=app

+1

Manav Roy with C++ you don't really have to use C-style memory allocations. Use new and delete instead. Much easier. https://code.sololearn.com/cA8d5FJOnjU8/?ref=app Bob_Li Actually, I want to store all words (Within a space)of a sentence in an array, Would this code do that?

+1

Manav Roy have you tried the last code I posted? It works pretty much the same way as the vector one, but using an array instead. The C++Stream one stored all lines as a single string. The C++Vector and C++SstreamDynamicArray both stored each line separately in a vector<string> and string[ ] respectively. Unfortunately, I can"t get your code to work. And I am unclear on what you mean by: "store all words(within a space) of a sentence in an array". Does that mean each word is stored in a different index, instead of each line?