3

I am always confused with the concept "define variable." What does define mean?

For example:

void main {
map<int,int> *infoMap;

if() {
//some check here,if it passes this check, then new infoMap
}

infoMap = new infoMap; 

}

So does

map<int,int> *infoMap;

or

map<int,int> *infoMap = new inforMap;

define a variable?

Chuck
  • 998
  • 8
  • 17
  • 30
  • 3
    I am afraid none of the above is called define. map *infoMap; is a declaration of a pointer to a map while infoMap = new infoMap; is the assignment to the previously declared variable. – FailedDev Oct 12 '11 at 13:38
  • @ratzip - Apart from the declaration/definition, it's not a good idea to have a pointer to map and then explicitly new it. On every insertion of key association with values using operator `[]`, map grows automatically. `std::map` should suffice in most of the cases. If you thinking in terms of raw pointers, please think over. – Mahesh Oct 12 '11 at 13:46
  • 2
    @FailedDev: you are mistaken. It's true that `map *infoMap;` is a declaration, but in this case it is also a definition. – Steve Jessop Oct 12 '11 at 13:47
  • @SteveJessop - Could you please post it as an answer and explanation giving us an insight? – Mahesh Oct 12 '11 at 13:48
  • @SteveJessop hm I guess you are right but I usually see the term declaration used for such statements. – FailedDev Oct 12 '11 at 13:48
  • @FailedDev: I'm not surprised, but only because most people don't care about the distinction. It's not possible to declare an automatic variable without defining it, so the declaration always is the definition. It's only for objects with wider scope (and especially external linkage), functions and classes, that the difference matters. – Steve Jessop Oct 12 '11 at 13:50

6 Answers6

6

The top one is the declaration, or if you like, definition. Here, the compiler allocates space for the variable.

The bottom one is an assignment. Here, the compiler fills the space it allocated at definition time. You can have more than one assignment, if you want to change the value of the variable to something else.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • You might also mention that (As SteveJessop says) there is a difference between declaration and definition, but for local variables they're the same place. Also, ratzip seems to think his variable is the map, whereas in reality the variable `infoMap` is a _pointer_. – Mooing Duck Oct 12 '11 at 16:48
5

Here's a random, incomplete illustration:

class Foo;   // (incomplete forward) class declaration

class Foo {  //  class definition
  int a;           //  member declaration + definition
  int b(int, int); //  member function declaration
  static int c;    //  static member declaration
};

int Foo::b(int a, int b) { return a+b; }  // member function definition
int Foo::c;                               // static member defintion

int bar(int);   // free function declaration

int main() {    // free function declaration + definition
  int q;        // declaration + definition
  q = bar(0);
  return q;
}

int bar(int a) { return 2 * a; }  // free function definition

Perhaps the "static member definition" is unique in the sense that it provided an actual object instance (i.e. allocation plus construction) for an object that has been declared elsewhere. This is only comparable to a pure-extern declaration:

extern int N;  // declaration only, no definition

Not to be confused with a declaration+definition with external visibility:

extern const int M = 11;  // declaration and definition
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2
map<int,int> *infoMap;

infoMap is the declaration. Usually when there is initialization along with declaration then it's called defining the variable.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
2

To define something in C++ is to bind an identifier (in this case a pointer to a map<int, int>) to some storage, as opposed to a declaration that only binds an identifier to a type and does not allocate storage. If the compiler does not need any information about the definition of a type (i.e. it only needs the type), then you can get away with just a declaration. Otherwise you need a definition.

gred
  • 612
  • 1
  • 8
  • 15
1

With variables define and declare tend to be used interchangeably. However there is a subtle difference.

In most cases you are actually defining the variable.

map<int,int> *infoMap;

The term "define" declares a symbol and gives it substance, storage space for variable, structure/class body, function implementation.

In some instances you can "declare" the variable using the extern keyword. This basically informs the compiler of the existence of the symbol name and its type but does not allocate space for it. The space is allocated elsewhere where the variable is actually defined.

// foo.c
extern int bar;

// bar.c
int bar;
Dave Rager
  • 8,002
  • 3
  • 33
  • 52
  • You have "declaration" and "definition" precisely backwards. – Steve Jessop Oct 12 '11 at 13:48
  • 1
    Every definition is a declaration, and the list of declarations which are not definitions is in 3.1/2 in the FDIS. So they're right as far as calling a declaration goes, it is a declaration. But most of the answers here are wrong in some way or another. – Steve Jessop Oct 12 '11 at 13:56
  • @Steve, Yep you are right. It's been a while since I really cared about the distinction. I updated my answer. – Dave Rager Oct 12 '11 at 14:08
0

When you declare a variable in c++ you reserve space in memory for it, but the memory is not written to. This declaration happens in

map<int,int> *infoMap;

It could contain anything. When you define the variable you actually set it to some value, in this case a new infoMap.

Sinthia V
  • 2,103
  • 2
  • 18
  • 36