Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Having problems defining a variable inside main() function

I am trying to define a variable from an external library in C++, Visual Studio 2010. It only works when I put it outside of the main function.

This code crashes:

#include "StdAfx.h"
#include <ogdf\basic\Graph.h>
#include <ogdf\basic\graph_generators.h>

int main()
{
   ogdf::Graph g;
   ogdf::randomSimpleGraph(g, 10, 20);
   return 0;
}

It gives me an unhandheld exception: Access violation. However, if it is outside main function, it works without any problem:

#include "StdAfx.h"
#include <ogdf\basic\Graph.h>
#include <ogdf\basic\graph_generators.h>

ogdf::Graph g;

int main()
{
   ogdf::randomSimpleGraph(g, 10, 20);
   return 0;
}

Do you have any how do I fix that? I assume, that it is caused by some kind of linking problem.

EDIT: It looks like the problem is not the initialization of the variable. It throws an exception, when the application exits.

int main()
{
ogdf::Graph g; // No problem
ogdf::randomSimpleGraph(g, 10, 20); // No problem
int i; // No problem
std::cin>>i; // No problem
return 0;    // Throws an exception after read i;

}

Call stack: Call STack

The output is: First-chance exception at 0x0126788f in graphs.exe: 0xC0000005: Access violation writing location 0x00000000.

Unhandled exception at 0x0126788f in graphs.exe: 0xC0000005: Access violation writing location 0x00000000.

like image 864
user3382896 Avatar asked Mar 05 '14 10:03

user3382896


People also ask

Can we declare a variable name main inside the main function?

we are able to declare the main keyword as a variable name(without an error); however, the same is not true for other functions(i.e. user defined functions).

Can we declare the global variable inside the main function in C?

No, it's not possible to declare global variables inside the main() , or any other functions' scope. Global variables must be declared outside of any functions scope.

Why can we not just use the variables defined in the main function?

This is because the msg variable that you have declared in the main function is a local variable and can be accessed only within the main function.

Can we declare variable in main?

Instance Variables Can Be Used in Any Method in a ClassIf you declare num inside the main() method body, it can only be used inside of main().


1 Answers

Works on my machine™.

Esoteric errors like that are often a result of binary incompability. Basically, because of different compiler/preprocessor options, effective headers that your code and the library "see" are different.

For instance, if you have a library with following header code:

class Foo
{
#ifdef FOO_DEBUG
    int debug_variable;
#endif
    int variable;
};

Library function:

void bar(Foo& foo)
{
    std::cout << foo.variable;
}

And client code:

Foo foo;
foo.variable = 666;
bar(foo);

If FOO_DEBUG is not in sync amongst client and the library, this will possibly crash and burn -- variable will have different expected offset.

In your case, I suspect one of the following may be true:

  • You have built the ogdf with different compiler than your code
  • If not, you ogdf and your code have different build configurations (Release vs Debug)
  • Both are debug, but you have defined OGDF_DEBUG (as recommended here)
  • You have different "Struct Member Alignment" setting
like image 63
gwiazdorrr Avatar answered Sep 21 '22 21:09

gwiazdorrr