Difference between declaration and definition

Hi all!

"Defining" a variable means declaring it and allocating memory for it, while only declaring means no memory was allocated. No?
Last edited on
Well for functions when you declare a function you specify its name and type as well as what parameters it accepts at the beginning. When you define a function the function is actually typed out as to what it does.

In a class the variable follows the same rule where in a class definition the declaration of a variable takes no memory until a instance of the class is defined.

I tend to separate pure variables in a function by calling them declared or initialized.
Declared takes memory but the data of the variable is garbage.
Initialized is declared but the data has been defined in some fashion, either by the user or some operation in the program.
Thanks.

Any idea why when I declare a variable, it is given a garbage value, but if I allocate one using new, it is not? For example:

int aa;

gives 'aa' a garbage value. However:

int * aa = new int;

gives '*aa' zero.
Last edited on
NewProgrammer wrote:
However:

int * a = new int;

gives the integer that 'a' points the value zero.

Does it? (spoiler: it doesn't)

Run the following (in both Debug and Release mode) and see:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
    int * a = new int;

    cout << *a << endl;

    delete a;

    cout << "Press ENTER to continue..." << endl;
    cin.get();

    return 0;
}
I ran it on putty, and it printed:
1
2
0
Press ENTER to continue...


What is Debug and release mode?
Putty is not a compiler. Chances are whatever compiler you're using is being nice.

What is Debug and release mode?

These you would set when using an IDE such as Code::Blocks or Visual Studio. In Debug mode, compiler optimizations are turned off, and new memory is set to zero (at least with Visual Studio), among other things. In Relase mode, compiler optimizations are turned on, among other things. Despite the name, it is possible to debug in Release mode. If you're not using an IDE, you can achieve the same Debug/Release effect by having a set of compiler flags for "Debug" and a another set for "Release".

When I run the above code with Visual Studio 2005, I get -842150451 in Debug mode, and 24 in Release mode. These numbers depend on what happened to be in that memory location at the time.
Last edited on
When I run the above code with Visual Studio 2005, I get -842150451 in Debug mode, and 24 in Release mode. These numbers depend on what happened to be in that memory location at the time.
That is what I expected to happen at first. I am not sure how/why putty is initializing the variables.
Or maybe that memory just happened to contain zero at the time.
Prior to starting this thread, I ran a program similar to what you gave above, except I declared an array of variables instead of a pointer to one variable. Be it integers, doubles or chars, the value of the elements in the array were always uniform (zero, zero, empty).

However, the values were not uniform when I declared a pointer to one variable and indexed it:
1
2
3
int* aa;
for (int i = 0; i < 10; ++i) 
     cout << aa[i] << endl;
. This lead me to believe the new operator was initializing the variables somehow.
Nonetheless, I understand all the explanations given in this thread and appreciate all the help.

NP
Essentially, it comes down to undefined behaviour. Reading from an uninitialized automatic variable (such as a local variable on the stack) is undefined (could be garbage, or zero). Same deal for reading from uninitialized memory that's been dynamically created (the value depends on what is in that memory location at the time), i.e. it's indeterminate.
Last edited on
Makes sense.
Just to confuse you:

1
2
int* foo = new int;  // *foo is unassigned/garbage
int* bar = new int();  // *bar is 0 
I'm sure it has something to do with the parentheses :)
And to (hopefully) unconfuse, here's what the standard (2003) says:

(5.3.4.15) If the new-initializer is of the form (), the item is value-initialized (8.5)
(8.5.5) To value-initialize an object of type T means: (in our case) the object is zero-initialized
(8.5.5) To zero-initialize an object of type T means: if T is a scalar type (3.9) (as in our case), the object is set to the value of 0 (zero) converted to T;
Nice. Thanks for the source.
Topic archived. No new replies allowed.