0

Suppose I have a vector:

std::vector<char>& over;

and somewhere in my code I have an input that allows me to assign data to that character vector. I understand that an unsigned char has a bit size of 255 such that if I input 256 characters into that char array at once the program will overflow. But what if I need to push more than 255 characters into this variable? It must stay a vector of type char for other reasons so what other solutions are there to deal with this overflow issue?

Dave
  • 23
  • 4
  • 2
    `an unsigned char has a bit size of 255` No, a `char` usually has 8 bits which can store numbers in the range 0, 1, ..., 255. Each element of a `vector` is such a `char` and can have values between 0 and 255. This is not related to the size of the vector itself, which can grow as large as needed. – dxiv Aug 31 '20 at 03:13
  • 1
    `vector` could be as long as you want until it reaches `vetor.max_size`. – Louis Go Aug 31 '20 at 03:13
  • Whoops you are right that is a fundamental misunderstanding. When using io_service.run() I end up getting a std::bad alloc error when i put more than 255 characters into the input. I definitely mixed up my understand of bits. – Dave Aug 31 '20 at 03:20

3 Answers3

1

I think you're confusing concepts here. The container in which you store whatever datatype objects (in this case chars) has a capacity that is independent of the type it is storing. The container will consume memory from the heap (if allocated dynamically, like a vector does). So, the max size of the container isn't limited to the datatype max size, but your available memory (RAM), basically.

Note: A char object occupies 1 byte (i.e. 8 bits). With 8 bits, the max signed value we can get is 127 and the max unsigned value is 255.

emegona
  • 168
  • 1
  • 12
0

You mixed type overflow with vector size.

Overflow of a type means: the calculation result is out of the type's defined range.

Example: https://godbolt.org/z/3o7jqq

#include <iostream>
#include <limits>

int main(int, char**)
{
       unsigned char a{std::numeric_limits<unsigned char>::max()};
       std::cout<< +a << std::endl;
       a+=1; // max value +1, it must be overflowed!
       std::cout<< +a << std::endl;
}

This prints

255 <== It's max value
0   <== the result of overflow.

For vector size, it has a pretty large number than 255.

You may test it. https://godbolt.org/z/1fqdPa

std::vector<unsigned char> v;
std::cout<< v.max_size() << std::endl;

It prints 9223372036854775807 on online compiler.

Louis Go
  • 2,213
  • 2
  • 16
  • 29
  • Sorry i'm probably fundamentally asking the wrong question then. Thank you for clearing up the misconception. – Dave Aug 31 '20 at 03:24
  • @Dave My suggestion is get a entry level book in https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. It would save you a lot of time than searching on the internet. – Louis Go Aug 31 '20 at 03:26
0

I understand that an unsigned char has a bit size of 255 such that if I input 256 characters into that char array at once the program will overflow.

You misunderstand. The size of an array is largely independent of the type of the elements that the array contains. The array could contain 0, 255, 256 or perhaps 424242 elements.

But what if I need to push more than 255 characters into this variable?

No problem. You can push as many elements as you want into the vector as long as there is memory available for the process.

eerorika
  • 232,697
  • 12
  • 197
  • 326