Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9
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.
21st May 2022, 3:14 PM
Arsenic
Arsenic - avatar
+ 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
21st May 2022, 2:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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
21st May 2022, 1:21 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Not optimized but working code as below : https://code.sololearn.com/cC2J9cUZT2uI/?ref=app
21st May 2022, 1:37 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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
21st May 2022, 1:53 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
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 ).
23rd May 2022, 12:46 AM
Arsenic
Arsenic - avatar
+ 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.
24th May 2022, 11:13 AM
Bob_Li
Bob_Li - avatar
+ 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
26th May 2022, 4:43 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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
26th May 2022, 9:32 PM
Bob_Li
Bob_Li - avatar
+ 1
Try referring code I just posted in comment section.it is working code
21st May 2022, 1:43 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
22nd May 2022, 12:40 PM
Vinit Sonawane
Vinit Sonawane - avatar
+ 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
22nd May 2022, 7:25 PM
Bob_Li
Bob_Li - avatar
+ 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
22nd May 2022, 8:14 PM
Bob_Li
Bob_Li - avatar
+ 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
23rd May 2022, 9:05 PM
Bob_Li
Bob_Li - avatar
+ 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?
24th May 2022, 7:07 AM
Bob_Li
Bob_Li - avatar
+ 1
Manav Roy well, this is not an ideal method. But using sstream and tokens, perhaps something like this? https://code.sololearn.com/c6g3Y6zhTplr/?ref=app
24th May 2022, 9:23 AM
Bob_Li
Bob_Li - avatar
+ 1
Manav Roy but how can you know the number of blocks if you don't count the words(or the ' ' separator)? maybe by adding a new block when ss returns a ' '? that might work. worth thinking about.
24th May 2022, 10:31 AM
Bob_Li
Bob_Li - avatar
+ 1
Manav Roy realloc sounds promising. But you are trading efficiency for memory space. Lots of memory shuffling to add things. Micro-optimization is more trouble than they're worth, imho. Programs usually have bigger problems than complex optimization for small gains. And code maintainance would be hell😁.
24th May 2022, 10:36 AM
Bob_Li
Bob_Li - avatar
+ 1
Manav Roy here is another similar post by someone using malloc on c++. The advice is overwhelmingly against it. https://stackoverflow.com/questions/51808670/difference-between-malloc-and-calloc-with-stdstring The gist of it is strings are objects in c++, and just simply allocating memory does not guarantee compatibility. strings in c and strings in c++ mixes like oil and water.
27th May 2022, 1:02 PM
Bob_Li
Bob_Li - avatar
0
Manav Roy ss.str() returns a copy of the current contents of the stringstream ss as a string object. as to parentheses vs curly brackets https://stackoverflow.com/questions/35285913/when-should-we-use-parenthesis-vs-initializer-syntax-to-initialize-obje but really, this gets confusing if you think about it too much. That is why I default to Python😁 C/C++ is like doing brain surgery, and javascript is like performing black magic.😅 Python is like playing with Lego blocks.
26th May 2022, 9:18 AM
Bob_Li
Bob_Li - avatar