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?

5
  • 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? Commented Jun 2, 2015 at 19:12
  • int myarr[runtime_value] is a VLA extension, prefer to use std::vector anyway. Commented Jun 2, 2015 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. Commented Jun 2, 2015 at 19:13
  • log10 may help, or simple std::vector::push_back digit by digit. Commented Jun 2, 2015 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! Commented Jun 2, 2015 at 19:17

4 Answers 4

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

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.
@CoryKramer that not correct in C99 you are allowed to make run-time arrays . See this answer stackoverflow.com/questions/737240/…
@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.
@CoryKramer Sorry you have right. It just my gcc there have this extension enabled default.
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.

Comments

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

1 Comment

This does not solve their primary issue of determining how many digits there are.
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?

1 Comment

This is not allowed in C++ (without a compiler extension)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.