Why does program crash when an integer variable is declared in main

Pages: 12
I can declare an integer variable in the program anywhere except for the main
function....I just put:

int dummyVariable;

and it crashes everytime. I can declare globally and in classes with no problem
of crashing. The program lets me declare other variables like strings and bools
though?? Has anyone ever run into this and is it maybe a memory issue or
something. Thanks!
Last edited on
Please post the code that is causing your crash.
The problem variable is on line 7. This is just the main function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
int main(int argc, char* args[])
{
    UniversalFunctionsAndStuff universal;
    MainGameInputAndAnimation gameplay;
    StartupInputAndAnimation startup;
    bool nameEntered = false;
    int dummyVariable = 1;//PROBLEM IS HERE

    Timer fps;

    if(InitializeStuff() == false)
        return 70;

    StringInput name;///YEEHAAWW YEAH BUDDY!

    if(LoadFiles() == false)
        return 71;

    //Mix_PlayMusic( mainMusic, -1 );

    for(;;)
    {
        fps.start();
                                            ///---TITLE SCREEN---
        if(titleScreen == true)
        {
            if(startup.HandleInput()==99)
                return 0;
            startup.DoAnimation();
        }

                                            ///---INSTRUCTIONS SCREEN---
        else if(instructionScreen == true)
        {
            startup.HandleInput();

            if(startup.GetInstructionSelectionNumber()==1)
                universal.Apply_Surface(instructions[0]);
            else if(startup.GetInstructionSelectionNumber()==2)
                universal.Apply_Surface(instructions[1]);
        }

                                            ///---CHOOSE PLAYER SCREEN---
        else if(choosePlayerScreen == true)
        {
            startup.HandleInput();
            startup.DoAnimation();
        }

                                            ///---ONE PLAYER MODE---
        else if(playScreenOnePlayer == true)
        {
            gameplay.HandleInput();
            gameplay.DoAnimation();

            if(nameEntered == false)
            {
                //Get user input
                name.handle_input();

                if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )
                    nameEntered = true;
            }

            name.show_centered();
        }

                                            ///---TWO PLAYER MODE---
        else if(playScreenTwoPlayers == true)
        {
            gameplay.HandleInput();
            gameplay.DoAnimation();
        }

        if(SDL_PollEvent(&event))
        {
                if(pauseMenuScreen==true)
                {
                    if(gameplay.HandleInput()==99)
                        return 0;

                    if(gameplay.DoAnimation()==99)
                        return 0;
                }

                if(event.type == SDL_QUIT)
                {
                    return 0;
                }
        }

        SDL_Flip(screen);

        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }

    return 0;
}
Last edited on
The problem is probably in some other parts of the code that we can't see. You should consider using a debugger that tells you where the problem is.
Last edited on
int main(int argc, char* args[])

should be:

int main(int argc, char* argv[])
Alright thanks. One of the programmers here told me to use Valgrind so will check it out. Is it any chance that I can get it working on Windows 7 though because I am told it can only be used on Linux and Mac

@ TheIdeasMan

I've always wondered the difference between the two. Could you possibly shed some
light on it. Thanks
Last edited on
@TheIdeasMan
Are you sure that really matters? I think it doesn't but the standard is not clear.

@dtaqee88
Valgrind is what I use but it will not work on Windows, so I don't have anything to recommend.
Appreciate the help
For a while I had mixed it up: int main(int argv, char* argc[])
So I wouldn't think the name of the variables makes a difference at all.

The problem is probably in some other parts of the code that we can't see. You should consider using a debugger that tells you where the problem is.


Or try this as your whole program:
1
2
3
4
5
6
7
8
#include <iostream>

int main(void)
{
  int x = 5;
  std::cout << x << std::endl;
  return 0;
}


edit: or is this solved?
Last edited on
No the problem is not solved. I tried your program and it compiled but it did not run. I don't
know why but if I run an SDL program then try to run a regular c++ program behind it....
the c++ program never runs. Only when I initially run a regular c++ program does it run
correctly??

Also, I can declare integer variables globally but the moment I declare them in the main
function or return them from a function to main, the program still crashes. I've have just been using bool variables instead of ints to do the job. I don't know if this means anything but I have around 50 ints declared....maybe that's too many???

And I always get a compiler warning from CodeBlocks saying "process terminated with
status 3"
Last edited on
50 ints is not too many. The problem is something else. Do you know the line or at least approximately where the crash happen?
My mistake. Can still return ints to main....just had a little mishap with a function. The integer
declaration problem still persists though. I wish I could tell you where it is happening. All I know is the
exact moment I declare an int...crash with a status 3. I've searched and programmers have said that it
maybe loose variables lying around that haven't been initialized so I am checking my program.
Please show the full error message.
I know it's taboo to drop code off on programmers but you might be able to spot something within
minutes of checking it out...

Here's the code:


http://pastebin.com/uzmUbK5E
Last edited on
@TheIdeasMan
int main(int argc, char* args[])

should be:

int main(int argc, char* argv[])


It is not important what names of the function parameters will be used. For example you can write

int main(int London, char* Paris[])

or even omit names of the parameters if they are not used in the function

int main(int, char*[])
@ vlad from moscow

This is all it says:
Process terminated with status 3 (0 minutes, 3 seconds)
@dtaqee88
There is no need to show all your code. Show the error message. It is obvious that the problem is somewhere else in the code and you could resolve it yourself if would start coding from a simple example.
Last edited on
@dtaqee88
@ vlad from moscow

This is all it says:

Process terminated with status 3 (0 minutes, 3 seconds)



It is a rurime error. The declaration of int has nothing common with your problem. You should look at your classes.



Last edited on
Alright. Thanks will do
Last edited on
Hello again everybody. I know I have beaten this horse long enough but after taking everyone's advice and debugging my program [I did not know CodeBlocks had a debugger], I have found the problem. It is this:

message = TTF_RenderText_Solid( font, z , textColor ); .....phew

z [second parameter] being declared as char* z messed up my program and changed addresses to things it wasn't supposed to?? The second parameter is meant to be a number and it is supposed to be decremented every time a condition occurs [when the user presses enter] I did not know how to decrement a number that isn't an integer so I was told to use itoa and atoi functions and they worked but I was digging myself in a hole.

The second parameter in the above function has to be a pointer to a character but I learned I can declare z as a string and just put "z.c_str()" in the second parameter and everything is alright and c++ recognizes z.c_str() as a pointer to a char. Here is my question...I tried using itoa/atoi functions with a string but I got an error so is there any way to convert a string to a number and decrement the number and change it back to a string?
Pages: 12