0

i have a score board that get update all time with new information and the buffer size is = amount of rows my score board can handel, but i'm trying to make it infinity without limits.

what i'm trying to do is as-sign infinity value to a buffer like that:

 const int infinity = std::numeric_limits<const int>::infinity(); 

 char Buffer_format_text [infinity]; 

but it dont work, becouse it says:

error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0

is there a way to do that? or trick? , please help me. Don't ask me why i want to do that, i'm asking how to do that.

Update:

This how i'm doing it with sprintf , how do you that in ostringstream ?

char Buff[100]; 
int length  = 0;
int amount_of_space = 8;
length += sprintf(Buff+length,"%-*s %s\n", amount_of_space, "Test", "Hello");

 

this output: Test     Hello
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
anna Sjolvikaz
  • 141
  • 1
  • 1
  • 8

4 Answers4

6

In C++11, infinity() is constexpr, so in theory you could use it directly this way:

char Buffer_format_text[std::numeric_limits<const int>::infinity()];

However, the problem here is that int cannot represent infinity. If you tried this:

std::cout << std::numeric_limits<const int>::has_infinity;

You would see that 0 (false) is printed to the standard output (live example). The infinity() function for specializations of std::numeric_limits where has_infinity is false will return 0 - in fact, the function is meaningless in those cases - and you cannot create arrays of size 0.

Besides, you cannot expect an array of infinite size to be allocated - how would it fit into memory? The right approach, if you do not know in advance the size of your vector, is to use std::vector or a similar container that does allocate memory upon request.


UPDATE:

It seems what you actually need an infinite array for is to be able to build up a string whose size is not known in advance. To encapsulate such a string that grows dynamically, you can use std::string.

In order to perform type-safe output into an std::string and replace sprintf(), you could use std::ostringstream. That would allow you to insert stuff into a string the same way you would print it to the standard output.

Then, once you are done working with the std::ostringstream, you can get an std::string object from it by calling the str() member function.

This is how you could use it in a simple example:

#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>

int main()
{
    std::ostringstream oss;

    oss << "The answer is: ";
    oss << 42;
    oss << ", and this is a double: ";
    oss << 3.14;
    oss << ". " << std::endl;
    oss << "Oh, btw, you can also output booleans: ";
    oss << std::boolalpha << true;
    oss << ". See? It's easy!" << std::endl;

    std::cout << oss.str();
}

Live demo.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
2

You basically cannot allocate an array with really infinite memory, either on stack or on the heap. You also cannot allocate array with size 0 since it is illegal according to the standard.

You may try to use std::vector which grows when necessary by itself. but you still cannot have infinite memory to allocate since your disk space is limited no matter how large it is.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • Why we can not make an array with size `0`? Ignoring the usage, it's possible. – masoud Apr 20 '13 at 14:23
  • @MM.: No, it's not. The standard forbids this – Andy Prowl Apr 20 '13 at 14:25
  • @MM. I found a post for why array length cannot be 0 in C++. so correct if I am wrong: http://stackoverflow.com/questions/6180012/array-with-size-0 – taocp Apr 20 '13 at 14:25
  • @MM.: Not sure what you mean, but the standard forbids having zero-sized arrays – Andy Prowl Apr 20 '13 at 14:28
  • @MM. the compiler may not give you warnings, but it does not make sense to declare arrays with 0 in length, you simply cannot use it anyway. – taocp Apr 20 '13 at 14:29
  • @AndyProwl: Would you indicate the paragraph of the standard which described it? (If it's possible) – masoud Apr 20 '13 at 14:32
  • @MM.: It's 8.3.4/1: "*[...] If the constant-expression (5.19) is present, it shall be a converted constant expression of type std::size_t and its value shall be greater than zero. [...]*" – Andy Prowl Apr 20 '13 at 14:33
  • @AndyProwl: You're so fast!! My bad English, the word _shall_ is a piece of advice or it means _must_ ? – masoud Apr 20 '13 at 14:35
  • @MM.: No problem, it means "must" - it has to be greater than zero – Andy Prowl Apr 20 '13 at 14:36
2

It doesn't make sense to declare a buffer as big as infinity, both because integral types cannot (normally) represent infinity (the infinity member of numeric_limits is there for FP types), and because no machine could create a buffer infinity bytes big. :)

Instead, use a container that can handle the reallocations needed for new insertions by itself, limited only by the available memory, e.g. std::vector (or std::deque or others, depending from your insertion/removal patterns).


Edit: since the question seem to be about creating an arbitrarily long string, the "C++ answer" is to use std::ostringstream, which allow you to write as many elements as you like (limited only by the available memory) and provides you a std::string as a result.

std::ostringstream os;
for(int i=0; i<1000; i++)
    os<<rand()<<" ";
std::string out=os.str();
// now in out you have a concatenation of 1000 random numbers

Edit/2:

This how i'm doing it with sprintf , how do you that in ostringstream ?

char Buff[100]; 
int length  = 0;
int amount_of_space = 8;
length += sprintf(Buff+length,"%-*s %s\n", amount_of_space, "Test", "Hello");
// needed headers: <sstream> and <iomanip>
std::ostringstream os;
int amount_of_space = 8;
os<<std::left<<std::setw(amount_of_space)<<"Test"<<" "<<"Hello";
std::string out=os.str(); // you can get `length` with `out.size()`.

But if you need to do multiple insertions call os.str() only at the end, when you actually need the string. Again, no need to keep track of length, the stream does it automatically.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • I see, can you sow example using victor with sprintf instead of char buffer[100....] – anna Sjolvikaz Apr 20 '13 at 14:21
  • @annaSjolvikaz: As the comment above says - use ostringstream, or retag your question as C, not C++ – Armen Tsirunyan Apr 20 '13 at 14:22
  • @annaSjolvikaz: use `ostringstream` instead of `sprintf`, it handles the memory reallocations by itself and returns a `std::string`. – Matteo Italia Apr 20 '13 at 14:22
  • Can you give me small examlpe os using ostringstream like sprintf, get the lenght and do such lenght += sprintf(buff+lenght etc?? – anna Sjolvikaz Apr 20 '13 at 14:23
  • @annaSjolvikaz: you don't use `ostringstream` like `sprintf`, you use it like a normal C++ stream; the bookkeeping about the current position in the string, the reallocations & co. is done automatically, see my example. – Matteo Italia Apr 20 '13 at 14:27
  • That example dont help, becouse i want to format the text like sprintf and use the lenght of text to make rows – anna Sjolvikaz Apr 20 '13 at 14:27
  • 1
    @annaSjolvikaz Edit your question to show an example of what you're doing with `sprintf()`. Then we can show you how to do that with `std::ostringstream`. – Angew is no longer proud of SO Apr 20 '13 at 14:28
  • 1
    @annaSjolvikaz: if you actually stated your real, complete question instead of changing the target at every comment it would be actually feasible to provide you a sensible answer. – Matteo Italia Apr 20 '13 at 14:29
  • Just updated my question to show how i'm doing with sprintf , how do you that in ostringstream ? – anna Sjolvikaz Apr 20 '13 at 14:32
0

You should use a container which can grow dynamically, such as std::vector (or, as your case seems to be text, std::string).

You can use std::ostringstream to construct a std::string like this:

#include <iomanip>
#include <sstream>

std::ostringstream ss;
ss << std::setw(amount_of_space) << std::left << "Test" << ' ' << "Hello" << '\n';
std::string result = ss.str();
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455