0

I am looking for a way to dynamically set the size of an integer array depending on the passed parameter. For example this in pseudocode:

int MyFunction(int number)
{
  int myarr[amount of digits in number];
}

So when the input is 13456 then the int array[] size should be 5. Whats the quickest way to do this in C++, when I don't know the constant for the size?

Dark Side
  • 695
  • 2
  • 8
  • 18
  • 1
    Is there a reason you are not using std::vector? Array sizes must be compile time constants. Do you now the size at compile time or only at runtime? – Benjy Kessler Jun 02 '15 at 19:12
  • `int myarr[runtime_value]` is a VLA extension, prefer to use `std::vector` anyway. – Jarod42 Jun 02 '15 at 19:12
  • I just wanted to know if it is possible to assign the size of the integer somehow dynamically using siezof() or something similar? I know there is std::vector but I want to avoid that. – Dark Side Jun 02 '15 at 19:13
  • `log10` may help, or simple `std::vector::push_back` digit by digit. – Jarod42 Jun 02 '15 at 19:13
  • 1
    If you're thinking along the lines of `sizeof`, something as simple as `int myarr[11];` would be enough in most situations (for 4 byte signed integers). This certainly is the quickest way! – Mr Lister Jun 02 '15 at 19:17

4 Answers4

5

You cannot create an array with run-time size, it must be known at compile time. I would recommend a std::vector instead.

One solution would be to count the characters after converting to a string

#include <string>
int MyFunction(int number)
{
    std::vector<int> myarr(std::to_string(number).size());
}

Mathematically, you can take the log (base 10) to find the number of digits in a number.

#include <cmath>
int MyFunction(int number)
{
    int numDigits = static_cast<int>(std::log10(number)) + 1;
    std::vector<int> myarr(numDigits);
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 2
    Using log(10) to compute the number of digits in a number is dangerous. Consider very large numbers and negative numbers. It is safer to just loop over the digits using div and mod. – Benjy Kessler Jun 02 '15 at 19:14
  • @CoryKramer that not correct in C99 you are allowed to make run-time arrays . See this answer http://stackoverflow.com/questions/737240/c-c-array-size-at-run-time-w-o-dynamic-allocation-is-allowed – jhilmer Jun 02 '15 at 19:52
  • 1
    @jhilmer This question is about C++, not C. Run time sized arrays in C++ are only available via compiler extensions, it is not allowed by the standard. In fact the link that you posted says exactly this. – Cory Kramer Jun 02 '15 at 20:00
  • @CoryKramer Sorry you have right. It just my gcc there have this extension enabled default. – jhilmer Jun 02 '15 at 20:05
1

An additional option you could do is to avoid using an array altogether by accessing the digits of the number directly:

unsigned int getDigit(unsigned int number, unsigned int index) {
  // See http://stackoverflow.com/questions/4410629/finding-a-specific-digit-of-a-number
}

unsigned int setDigit(unsigned int number, unsigned int index, unsigned int newVal) {
  // intPower is from the question linked to above.
  return number - get(number, index)*intPower(10, index) + newVal*intPower(10, index);
}

unsigned int size(unsigned int number) {
  // See http://stackoverflow.com/questions/1306727/way-to-get-number-of-digits-in-an-int
}

unsigned int push_back(unsigned int number, unsigned int newDigit) {
  // Assuming no overflow
  return 10*number + newDigit;
}

unsigned int pop(unsigned int number) {
  // Assume number != 0
  return number / 10;
}

This lets you treat your number as an array without actually initializing the array. You can even turn this into a class and use operator overloading to get actual array semantics.

Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
0

You may use vector instead - actually I think the best option in this case. Create a vector with some initial size. Then you may dynamically increase it -

int initialSize = 5;                    
vector<int> myvector(initialSize, 0);   //hold "initialSize" int's
                                       // and all initialized to zero
myvector[0] = 567;                   // assign values like a c++ array
Razib
  • 10,965
  • 11
  • 53
  • 80
0

With my gcc version 4.6.3 the following is possible:

int MyFunction(int number)
{
    int myarr[int(ceil(log(number)))];

    ....

    return 0;
}

Edit: C99 this is valid see: Array size at run time without dynamic allocation is allowed?

Community
  • 1
  • 1
jhilmer
  • 286
  • 1
  • 8