declaration vs definition in relation to Memory

84 Views Asked by At

I'm trying to understand the distinction between declarations and definitions in C++. I've read various answers here and consulted the C++ Standard, but I'm still confused about a few points:

  1. Some explanations suggest that definitions allocate memory to the variable, implying that if memory isn't allocated, it's considered a declaration. Is this always the case?

  2. Which phase of the compilation process—Compiler or Linker—is primarily associated with declarations and definitions?

Here's a summary of my understanding so far:

Declaration: This informs the compiler about the existence of a variable or function, including its name and type, but doesn't specify memory allocation.

Definition: This not only declares the existence of a variable or function but also provides the implementation and how much memory a variable needs but it doesn't allocate memory unless it is used.

Given the following code snippet similar to what we've discussed in lectures:

class Point { // declaration
public:
    int x; // declaration
    int y; // declaration
    void setPoint(int x, int y); // declaration
};

int main() {
    Point p1; // definition
};

My understanding is that everything in this code snippet should be considered a definition except for void setPoint(int x, int y);. However, I'm unsure about int x; because it seems that memory isn't allocated until an object is created. For instance, my IDE (Clion) considers int x; a declaration, which adds to my confusion.

Could someone please clarify how definitions are precisely associated with memory allocation? Additionally, I'd appreciate insights into the specific cases where memory is or isn't allocated for variables declared in classes or function bodies that haven't been invoked.

1

There are 1 best solutions below

0
Pete Becker On BEST ANSWER

Declarations and definitions are about names, not necessarily about objects.

A somewhat simplified (hence, wrong) description of those terms is that a declaration tells the compiler that some name will be used in a particular way; a definition gives the compiler all the details of what that name means.

This is a declaration of the class Point:

class Point;

After that declaration the name Point can be used in ways that don't depend on its definition. Point* and Point& (pointer to Point and reference to Point, respectively) are probably the most common examples.

This is the definition of the class Point:

class Point {
public:
    int x;
    int y;
    void setPoint(int x, int y);
};

Its two data members, x and y, are part of the definition. Those are neither declarations nor definitions. void setPoint(int x, int y); is a declaration of the member function Point::setPoint. Presumably, there's a definition somewhere that hasn't been shown.

This is a declaration of the object p1:

extern Point p1;

After that declaration the compiler knows that p1 names an object of type Point, and the code can do any operations on p1 that are valid for an object of type Point.

This is the definition of the object p1:

Point p1;

This tells the compiler "here it is".

Note that nothing in these descriptions mentions "memory". The definition of a class doesn't use memory; the definition of an object might mean "grab some memory for this thing", or it might mean "when the program gets here grab some memory for this thing".