0

EDIT

I am currently studying the comments written down for me below

A function that get a structure that contains a pointer to a array of sentences, and the amount of sentences. and returns a structure that contains a pointer to an array of words, and the amount of words (the sentences are split into words).

In the current function, a primary loop that through a sentence, and a secondary loop that through the one sentence.

During the loop, each character of the sentence is copied to a new word, until space. and move to the next word after the space.

The problem is, writing to the pointer of the current word overwrites the previous word, rather than copied to its right place. You can notice that in later words, there is a basis of the previous words. (The program "on the fly" after the fourth word)

Expression:

Sentences spilt(Sentences sentence) {
    Sentences words = {nullptr, 0};
    int basic_size = 5;
    int arr_size = 5;

    words._data = new(nothrow) char *[basic_size];

    for(int i = 0; i < sentence._num_of_sentences; i++) { // iterate though serntes
        for (int sent_inter = 0, words_inter = 0; sent_inter < strlen(sentence._data[i]); sent_inter++) { // iterate though words
           //alloc amount of words
            if(words._num_of_sentences == basic_size) 
                //func of alloc_sen
            
            //alloc len of word
            if(strlen(words._data[words._num_of_sentences]) == arr_size) 
                //func of alloc_word
            
            if(sentence._data[i][sent_inter] == '\n')
                break;

            //next new word
            if (sentence._data[i][sent_inter] == ' ') {
                words._data[words._num_of_sentences][words_inter] = '\0';//endline
                ++words._num_of_sentences;
                words_inter = 0;
                continue;
            }

            //copy correct character to correct word
            words._data[words._num_of_sentences][words_inter++] = sentence._data[i][sent_inter];
            
            //cout << words._data[words._num_of_sentences] << endl; 
        }
        ++words._num_of_sentences;
    }
 return words;
}

Temp result:

Input:

qwe rty uio
asd fgh jkl
zxc vbn

output:

q$g☻
qwg☻
->qwe☻
r↓♦️
rt♦️
->rty☺️
uwe
uie
->uio
aty
asy
->asd
    
now break
slomo
  • 107
  • 10
  • [Undefined Behaviour](https://en.cppreference.com/w/cpp/language/ub) Since the behaviour of undefined behaviour is undefined, you might get ascii crap, you might get a segfault, you might get attacked by cyberninjas. The possibilities are limitless, but ascii crap and crashes are more common than cyberninjas. – user4581301 Apr 28 '21 at 00:28
  • pls provide minimal code required. –  Apr 28 '21 at 00:28
  • I tried to narrow it down as much as possible. If I narrow it down further, the logic of the code will become incomprehensible – slomo Apr 28 '21 at 00:34
  • 2
    Narrowing down is nice, but if we cannot compile code, run the resulting program, and see the same results (within reason in the case of Undefined Behaviour because that smurf can look like anything), you've written a weak question. As an added bonus, if you give the problem the full [mre] treatment, you'll probably find the answer yourself. – user4581301 Apr 28 '21 at 00:36
  • Question: Why torture yourself with `words._data = new(nothrow) char *[basic_size];`? With `nothrow` you have to test the returned pointer for validity. Rare are the times you want to do that. – user4581301 Apr 28 '21 at 00:38
  • 2
    `words._data` is apparently an array of pointers, but I don't see anywhere that you make those pointers point to valid memory before dereferencing them (e.g. by passing to `strlen`). – Nate Eldredge Apr 28 '21 at 00:38
  • user4581301 I use the kidneys I've learned, I'm still a student, and I study all the time... – slomo Apr 28 '21 at 00:41
  • 1
    More interesting, why is something outside of `Sentences` messing with `Sentences`'s data? This is just asking for hard-to-find bugs. `Sentences` should control that allocation end-to-end. No one outside of `Sentences` should even know it exists. Reread the section in your text on Encapsulation. – user4581301 Apr 28 '21 at 00:41
  • 4
    I'm worried that someone is teaching you C++ ass-backwards. `new` isn't something that should be taught to beginners, It's intermediate material at the earliest, and throwing `nothrow` in there is even worse. Pay attention to the teacher. Give them what they want and pass the class, but I recommend supplementing what they cover [with a good book](https://stackoverflow.com/questions/388242). Otherwise you'll find yourself out in the job market underprepared, thinking you wasted your time and money, and probably scared of C++. It is a hard language, but it doesn't need to be that hard. – user4581301 Apr 28 '21 at 00:50
  • 1
    By the way, between all of my ranting, I think Nate nailed the first bug you need to deal with. – user4581301 Apr 28 '21 at 00:56
  • 3
    @slomo -- To be honest with you, and to make the point others were saying, this could have been a 5 to 10 line function, with no calls to `new` whatsoever **if** you were being taught C++. Using `std::string`, `std::vector`, `std::istringstream` to split the words, etc. All of that could have been used here. – PaulMcKenzie Apr 28 '21 at 04:23
  • "A function that get a structure that contains a pointer to a array of sentences, and the amount of sentences. and returns a structure that contains a pointer to an array of words, and the amount of words" shouldn't those both be containers that know their length? E.g. `std::vector` . See [Standard Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#abstract) for general good practices that this code does not resemble in the slightest. – JDługosz Apr 28 '21 at 15:16

0 Answers0