0

I'm trying to dynamically allocate a character array in c++ using smart pointers based on the user input like

std::cout<<"Input a word: ";
std::cin>>std::unique_ptr<char[]>ch_array(new char[]);

So when the user inputs the string it only consumes the necessary amount of bytes. Since it keeps giving me an error is there any other workaround to achieve such results ?

  • What is the use-case for something like that? What is the actual problem you need to solve? Why can't you use a `std::string`? – Some programmer dude Oct 10 '18 at 16:44
  • @Someprogrammerdude I have to create an array using smart pointer take an input and check if it's a palindrome or not (It's an assignment). My mentor said I can use static array but I just don't want to do it because user can give any amount of characters. – Muhammad Ali Khan Oct 10 '18 at 16:49
  • 1
    std::string does all you need. If your instructor doesn't allow you to use std::string, you need to implement what std::string does yourself. There is nothing magical in it: it reads characters one by one until a delimiter is found, and reallocates storage when it becomes too small to store the next character. So you need to know how to do both of these things. – n. m. could be an AI Oct 10 '18 at 16:57
  • @n.m. But can't we just create a array using smart pointers based on string ? Like when we initialize something like--> char array[]="Hello world!" and it automatically initializes array based on the given string. – Muhammad Ali Khan Oct 10 '18 at 17:03
  • You can of course. It would be totally pointless though. If you are allowed to use std::string, just use it throughout. – n. m. could be an AI Oct 10 '18 at 17:12
  • `char array[] = "some string"` initializes the array to a fixed specific size, the length of the string plus the terminator. It's the compiler that handles that at the time of compilation, not at run-time. Like I said in my answer, arrays must have a known size, no matter if it's allocated dynamically with `new[]` or statically by the compiler. – Some programmer dude Oct 10 '18 at 18:03

2 Answers2

0

A few of the problems:

  • You can not have an array of unknown size, you must always create one with a size.
  • You can not declare or define variables in all contexts, like part of a generic expression.
  • A std::unique_ptr object is still a std::unique_ptr object, there's no built-in conversion operator to convert to the type being pointed to, which means it can't be directly used as a destination for the formatted input operator.
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

If you do not want to use std::string or any other standard containers, as if was suggested earlier, you have basically two choices:

Straight forward one - allocate buffer in advance, big enough to store the input

std::cout << "Input a word: ";
char ch_array[100];
std::cin >> ch_array;

Much complicated one - read the input by symbol and reallocate the memory

Of Cause the reallocation casts a lot. And I do not suggest to use it, until the memory is extremely limited resource and you cannot spend even an extra byte of data. Just to show how complicated it can be:

char* ch_array;
static int count = 0;
char c = getchar();

while(c != '\n')
{
    if( count == 0)
        ch_array = static_cast<char*>( malloc( sizeof(char)+1 ) );
    else
        ch_array = static_cast<char*>( realloc(ch_array, sizeof(char)*(count+2) ) );

    ch_array[count] = c;

    count++;
    c = getchar();
}
ch_array[count] = '\0';

In this code error checks are also skipped which are absolutely necessary for the reallocation. With them, it will be even more complicated

As for the smart pointer, which is in the title, but seems unrelated to the question:

With the first option you can allocate the buffer on heap and use it:

std::unique_ptr<char[]> ch_array(new char[100]);

With the second you need a custom deleter, as described here Is it possible to use a C++ smart pointers together with C's malloc?