E
E
Egor Mikheev2016-06-29 23:51:10
C++ / C#
Egor Mikheev, 2016-06-29 23:51:10

How to use dynamic memory to store a commensurate number of characters from the input?

A problem from the course on C ++, but I could not solve it myself, maybe someone can help.
Implement a getline function that reads the input stream character by character until it reaches the end of the stream or encounters a line break character ('\n'), and returns a C-style string with the characters read.
Note that since the size of the input is not known in advance, you will need to re-allocate memory during the reading process if there are more characters in the input stream than you expected.
The memory returned from the function will be freed by the delete[] operator. You don't need to add a line break character ('\n') to the string, but remember that there must be a terminating null character at the end of the C-style string.
Implementation Requirements: In this assignment, you can define any helper functions you need. You don't need to define the main function.
The code below, although it works, is not accepted by the grader. I understand that the initialization of a dynamic array must be done inside the loop and the memory must be freed here.

#include <iostream>

char *getline()
{
    char c = '\n';
    int p = 8;
    int i = 0;
    char *str = new char [p];
    
    while(std::cin.get(c) && c != '\n') {
        if (i > p) {
            p = p*2;
             std::cout << "i=" << i << "p=" << p << '\n';
            char *strNew = new char [p];
            for(int z=0; z<i;z++)                
                strNew[z] = str[z];           
            delete [] str;
            char *str = new char [p];
            for(int z=0; z<i;z++)                
                str[z] = strNew[z];           
            delete [] strNew;           
        }
            str[i] = c;
            std::cout << "ix=" << i << "p=" << p << "str = " << str[i] << '\n';
        i++;
    }
    str[i+1] = '\0';
    return str;
}

main() {
   std::cout << getline();
return 0;  
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
abcd0x00, 2016-06-30
@abcd0x00

I understand that the initialization of a dynamic array must be done inside the loop and the memory must be freed here.

You must first allocate a buffer, and then enter the loop and write to it character by character. If it overflows, then, without leaving the loop, you need to increase it (allocate a new one, copy the data, free the old one, put the new one in place of the old one).

A
Adamos, 2016-06-30
@Adamos

Why would you take it?
You have allocated dynamic memory with a size of one byte.
Then, starting from this address, the entire input is pushed in.
Already on the second character the program is simply obliged to fall.
Moreover, even if the input is one character long, this second character will be the terminating zero.
Even in the task it is written about the re-allocation of memory - what, in fact?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question