1

I have an integer array: int* b whose values are set for elements 0 through 7. When I print out each element individually, I get the correct elements. However, when I use a for loop, I am getting different results. Any idea why?

Here's the code:

//toBinary(int x) returns an int pointer whose value is an array of size 8: int ret[8]
int* b = toBinary(55);

//Print method 1 (Individual printout)
cout << b[0] << b[1] << b[2] << b[3] << b[4] << b[5] << b[6] << b[7] << endl;

//Print method 2 (for loop)
for (int t = 0; t < 8; t++) 
    cout  << b[t];
cout << endl;

The result of the first print out is the following: 00110111 This is the correct printout.

When I print using the second technique, it says, -858993460-85899346051202679591765470927361022170810222364 This is the wrong printout.

Why am I getting two different printouts?

Here is the toBinary method:

int* toBinary(int i) {

    int byte[8];
    for (int bit = 7; bit >= 0; bit--) {
        if (i - pow(2, bit) >= 0) {
            byte[7-bit] = 1;
            i -= pow(2, bit);
        }
        else
            byte[7-bit] = 0;

    }
    return byte;
}
3
  • Can you construct a minimal test-case? Commented May 28, 2017 at 9:30
  • How is toBinary implemented? Commented May 28, 2017 at 9:30
  • I updated with the toBinary method Commented May 28, 2017 at 9:31

3 Answers 3

7

The toBinary method returns the address of a local variable. byte will be deleted when the function exits.

The fact that your first output works seems to be just luck because the memory segment wasn't used by anything else at that point.

To fix this, you'll either have to allocate the array on the heap manually or you use one of the containers (std::array, std::vector).

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

4 Comments

Thus undefined behaviour!
Why can I print each element individually, though?
@Drone6251 UB means anything is possible; nothing is guaranteed.
Oh, gotcha! Thanks for the help! I'll accept the answer in 8 minutes once the timer expires.
4

Other answers already touched on what the problem is here, I want to emphasize that your code is anti-modern C++, you should really be using std::vector (or std::array if you have C++11) which will solve all of these things instantly:

#include <vector>

std::vector<int> toBinary(int i) {

    std::vector<int> byte(8);
    for (int bit = 7; bit >= 0; bit--) {
        if (i - pow(2, bit) >= 0) {
            byte[7-bit] = 1;
            i -= pow(2, bit);
        }
        else
            byte[7-bit] = 0;

    }
    return byte;
}

And then..

std::vector<int> b = toBinary(55);

//Print method 1 (Individual printout)
cout << b[0] << b[1] << b[2] << b[3] << b[4] << b[5] << b[6] << b[7] << endl;

//Print method 2 (for loop)
for (int t = 0; t < 8; t++) 
    cout  << b[t];
cout << endl;

1 Comment

Thanks for the answer! I am used to Java, so I am still learning these different nuances.
3

Through toBinary you are returning a local variable that gets freed once the function call ends. You must allocate memory for the array using heap. And then return that pointer and collect it in the variable b.

1 Comment

Thanks for the help!

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.