Array initialization skipping straight to int main?

Im attempting to write a program that converts binary to base10, and vice versa.

But in the function for converting Base10 to Binary, just as it reaches the line of code
int* binary = new int [a];
it skips straight to the int main()

All I'm attempting to do with that line of code is initialize the variable "a" into the elements of the array "binary". Could anyone possibly help decipher why it does this?

Code: http://pastebin.com/LugnYn3j
I think that line is reserving memory on the heap for an array of 'a' objects of type int, the first element of which is at 'binary's' address. 'a' being a constant.
To do what you want you should write something like:
1
2
int a=6;
int binary[50] = {a};

Now all 50 elements in binary[] will hold 6
The first for loop counts the number of binary digits the base 10 number will produce. Then the next for loop is to make each element (Starting from 0, +1 each step) be filled with the correct binary digit. My end result is to display those digits, which I have yet to be successful with. And it happens because it skips over that entire loop that fills the elements with the digits.
Just a note/question, once it hits
int* binary = new int [a];
Why does it skip straight to the int main()?
buffbill wrote:
1
2
int a=6;
int binary[50] = {a};



Now all 50 elements in binary[] will hold 6

This is not correct. Only the first element will be set to 6. The other 49 elements will be set to 0.

GadgetRX wrote:
The first for loop counts the number of binary digits the base 10 number will produce.


The first loop modifies the variable b10 until the condition b10 >=1 is no longer true.

The second loop's control condition is b10>=1. We know, because the first loop terminated, that when the second loops is reached, b10>=1 has to evaluate to false, so the second loop will never be executed.

In the third loop the variable e is initialized to the value of newval, but newval was not initialized to any value and has never been set to any value, so it contains junk. The possibility of it containing junk that means the third loop is never executed is pretty high.

And finally, int*binary = new int[a]; is equivalent to int* binary = new int[0] which makes binary point to an array of 0 elements. Accessing any elements of the binary array will result in undefined behavior.


Last edited on
@cire Thanks for correcting my stupid oops.
binary is initialized to 6 and 49 '0's NOT 50 '6's
Topic archived. No new replies allowed.