0

C++ newbie here and I have run into an issue which I can't figure out.

When I run the code below I get the correct results I expected:

#include <iostream>
#include <math.h>

void factorize(int *factors, int number)
{

    // declare vars
    int index = 0;

    // find factors of number
    for (int x = 1; x <= number; ++x) {
        // if x is a factor of number, save it to array
        if (number % x == 0) {
            std::cout << "   Index: " << index << ",  F: " << x << "\n";
            index++;
        }
    }

}

int main()
{

    //declare vars
    int triangle = 0;
    int factors[] = {};

    //loop through all numbers
    for (int x = 1; x <= 5; ++x) {

        // calculate triangle value
        std::cout << "initial triangle value: " << triangle << ", ";
        triangle = triangle + x;
        std::cout << "x value: " << x << " , new triangle value: " << triangle << "\n\n";

        // find factors of triangle number
        factorize(factors, triangle);
        std::cout<<"\n";

    }

    return 0;

}

Correct results:

initial triangle value: 0, x value: 1 , new triangle value: 1

   Index: 0,  F: 1

initial triangle value: 1, x value: 2 , new triangle value: 3

   Index: 0,  F: 1
   Index: 1,  F: 3

initial triangle value: 3, x value: 3 , new triangle value: 6

   Index: 0,  F: 1
   Index: 1,  F: 2
   Index: 2,  F: 3
   Index: 3,  F: 6

initial triangle value: 6, x value: 4 , new triangle value: 10

   Index: 0,  F: 1
   Index: 1,  F: 2
   Index: 2,  F: 5
   Index: 3,  F: 10

initial triangle value: 10, x value: 5 , new triangle value: 15

   Index: 0,  F: 1
   Index: 1,  F: 3
   Index: 2,  F: 5
   Index: 3,  F: 15

But when I add the line factors[index] = x; to the factorize function, I get strange results. For some reason, the value of triangle in main is modified.

#include <iostream>
#include <math.h>

void factorize(int *factors, int number)
{

    // declare vars
    int index = 0;

    // find factors of number
    for (int x = 1; x <= number; ++x) {
        // if x is a factor of number, save it to array
        if (number % x == 0) {
            std::cout << "   Index: " << index << ",  F: " << x << "\n";
            factors[index] = x;
            index++;
        }
    }

}

int main()
{

    //declare vars
    int triangle = 0;
    int factors[] = {};

    //loop through all numbers
    for (int x = 1; x <= 5; ++x) {

        // calculate triangle value
        std::cout << "initial triangle value: " << triangle << ", ";
        triangle = triangle + x;
        std::cout << "x value: " << x << " , new triangle value: " << triangle << "\n\n";

        // find factors of triangle number
        factorize(factors, triangle);
        std::cout<<"\n";

    }

    return 0;

}

Unexpected results (notice how the triangle value goes: 0, 1, 3, but then for some reason jumps back to 2):

initial triangle value: 0, x value: 1 , new triangle value: 1

   Index: 0,  F: 1

initial triangle value: 1, x value: 2 , new triangle value: 3

   Index: 0,  F: 1
   Index: 1,  F: 3

initial triangle value: 3, x value: 3 , new triangle value: 6

   Index: 0,  F: 1
   Index: 1,  F: 2
   Index: 2,  F: 3
   Index: 3,  F: 6

initial triangle value: 2, x value: 4 , new triangle value: 6

   Index: 0,  F: 1
   Index: 1,  F: 2
   Index: 2,  F: 3
   Index: 3,  F: 6

initial triangle value: 2, x value: 5 , new triangle value: 7

   Index: 0,  F: 1
   Index: 1,  F: 7

What am I missing?

1 Answer 1

4

The definition

int factors[] = {};

defines factors to be an empty array of int. Every indexing of it will be out of bounds and lead to undefined behavior. That the first program works is because you don't actually use factors.

If you want a dynamic "array" in C++ you should use std::vector.

If you know the size beforehand, either use that size, or use std::array.

Sign up to request clarification or add additional context in comments.

2 Comments

0 sized arrays are not supported in c++. Many compilers offer it as an extension though.
@FrançoisAndrieux True, but the compiler could allow it as an extension? Without knowing the compiler or what warnings it gives it's impossible to say more specifically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.