initializing very large number

Jun 12, 2012 at 2:33am
I am trying to initialize a very large number and then pass it to a function:

1
2
3
int iteration = 2000000000;

void function(iteration);


My program throws an exception when the function is called. Is this because I need to use a different integer type since the number is so big? I have tried using long, long long, double and long double to initialize 'iteration' and I keep getting exceptions. Any ideas???? Thanks in advance...
Jun 12, 2012 at 2:40am
What exception is it?
Jun 12, 2012 at 2:50am
closed account (1yR4jE8b)
Show code for the actual function body, not the function call.
Jun 12, 2012 at 4:04am
Nothing to do with the number range I guess. Better to show the code.
Last edited on Jun 12, 2012 at 4:05am
Jun 12, 2012 at 4:35am
¿what function call?
Jun 12, 2012 at 4:44am
¿what function call?


This. I'm surprised that compiles...
Jun 13, 2012 at 10:42pm
Ok I think I found the problem. In my function where I am getting an exception thrown I am using vector::reserve and allocating memory for the vector 'stack'. The amount of bins I am allocating is based on 'iteration':

1
2
3
4
5
vector <secondary> stack;

int expected_iterations = ceil( iteration * 0.25 );

stack.reserve(expected_number_of_secondaries);


'seconday' is bunch of variables defined in the 'Secondary' class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Secondary
{
public:
	char m_plus[10];
	char m_particle_type[10];
	int m_int_particle_recognition;
	float m_energy;
	float m_direction_x;
	float m_direction_y;
	float m_direction_z;
	float m_position_x;
	float m_position_y;
	float m_position_z;
}


It seems whenever I am using a large enough number to allocate memory for the stack vector I get an exception. I think this because the problem goes away when I use a smaller number for 'iteration' like for example 2000000. How can I make vector::reserve work when allocating large numbers????????
Last edited on Jun 13, 2012 at 10:50pm
Jun 13, 2012 at 10:59pm
Before using reserve you can check how many elements of the vector you can allocate by calling vector member function max_size()
Last edited on Jun 13, 2012 at 11:00pm
Jun 13, 2012 at 11:03pm
How in the hell does your code compile? Your secondary class is missing the terminating semicolon, and also why is it a class instead of a struct (either works, but common practice, or something, dictates the use of a struct for variables and no functions)
But as for your problem, depending on the IDE you're using, as long as the number doesn't overreach the limitations of an int (2.1 billion for signed, 4.2 billion for unsigned) you shouldn't be having an issue. And actually, I can't recall the IDE giving a damn either. what's it throwing?
Topic archived. No new replies allowed.