Solved

pointers initialization

Posted on 2003-10-22
11
197 Views
Last Modified: 2010-04-02

1.
Int *x is uninitialized

Int *y =new int;

Is y considered initialized to 0 or is it uninitialized ?

0
Comment
Question by:Gipsy
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 9600438
y is uninitialized, but allocated.  It will not point to 0, and the data at y will not necessarily be 0.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9601064
However, with parentheses...

    int *y = new int();

....is initialised *and* allocated 0.

This is a feature that was introduced for the sake of templates.
0
 
LVL 2

Expert Comment

by:Elias Saliba
ID: 9601942
you can use :

int *y=NULL;  //in some compilers int *y=0; works too, but i prefer using NULL

and "y" will be initialized to nothing. ( not zero, but nothing )

but using this > int *y = new int();
will not intialise it to zero..

just try cout y and see what results in both commands..

sorry for bad english if any..
trinety
0
 
LVL 2

Expert Comment

by:Elias Saliba
ID: 9601971
it is dangerous to initialise a pointer to some adress and change its contents.. it may cause you system to malfunction...
my opinion is to just keep it uninitialised > int *y; < and it will take a free adress

trinety
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9602950
It *will* initialise the value pointed to to zero, trinety. It won't make the pointer 0, but it will make the value that the pointer points to zero.

Try this:
--------8<--------
#include <iostream>

int main()
{
        // Put some nonsense into the free store and free it up
        int *tmp = new int[1024];
        memset(tmp,123,sizeof(tmp));
        delete tmp;

        // Allocate and assign the default value to an int in the free store
        int *y = new int();
        std::cout << "Address of pointer is: " << reinterpret_cast<void*>(y) << '\n';
        std::cout << "Value of int pointed to is: " << *y << '\n';
}
--------8<--------

You'll see that y points to a memory location assigned the default value zero.

Default values are something that STL depends upon.

Compare what you see with this, which doesn't assign a default value (i.e. doesn't use parentheses):
--------8<--------
#include <iostream>

int main()
{
        // Put some nonsense into the free store and free it up
        int *tmp = new int[1024];
        memset(tmp,123,sizeof(tmp));
        delete tmp;

        // Allocate but don't assign the default value to an int in the free store
        int *y = new int;
        std::cout << "Address of pointer is: " << reinterpret_cast<void*>(y) << '\n';
        std::cout << "Value of int pointed to is: " << *y << '\n';
}
--------8<--------

This time the int pointer probably pointes to a value assigned to the gibber previously created by our memset.

Interesting stuff, eh?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 17

Expert Comment

by:rstaveley
ID: 9603035
If I'd been more sober I'd have deleted tmp with [].... but I'm not. Sorry.
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9604410
HI rstaveley..

    I don't know which compiler you are using.... I am using VC6 and its the value of pointer is always uninitialized whether you do "int *ptr = new int;" or "int *ptr = new int();" .... So it is wrong to say that when you put bracket() it will be initialized......

And trinity its always right to initialize a pointer during its definition; If you are not using that pointer imediately then initialize it with NULL. But never leave a pointer uninitialized.... because dangling pointer is a serious problem......

int *ptr = NULL ;

or

int num = 20 ;
int *ptr = &num ;

But not

int *ptr ;

Dennis
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9604429
The point that
int *ptr = new int() ;

 will initialize the value pointed by ptr to 0 is not correct since it is not working in my copy of VC6 and also in Turbo C++ environment.....

But i don't know the difference between using a bracket and not using them....

int *ptr = new int ;                      and               int * ptr = new int() ;

I see no difference in their working...... So y do we need two style.....


Dennis
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9604873
I see that...

   int *ptr = new int() ;

... also isn't supported by VC7, which I find surpising. It is, however, a new language feature that "if you use the syntax of an explicit constructor call without any arguments, fundamental types are initialised with zero" (ref: http://www.josuttis.com/libbook/ section 2.2.2). I believe that it is in the ISO/IEC 14882:1998 standard, but I couldn't quote chapter and verse from the standard.

I notice that this language feature is supported by GCC 3.2 and is not supported by GCC 2.96.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9605246
I'd better repost the test code with a corrected size in the memset and the proper delete for the record:

--------8<--------
#include <iostream>
#include <memory>

int main()
{
        // Put some nonsense into the free store and free it up
        int *tmp = new int[1024];
        std::memset(tmp,123,sizeof(int)*1024);
        delete []tmp;

        // Allocate and assign the default value to an int in the free store
        // new language feature supported by GCC 3.2, but not 2.98
        // not supported by VC7 or VC6
        int *y = new int();

        // Allocate, but don't assign
        int *z = new int;

        std::cout << "Address of new int() is: " << reinterpret_cast<void*>(y) << '\n';
        std::cout << "Value pointed to is: " << *y << '\n';

        std::cout << "\nAddress of new int is: " << reinterpret_cast<void*>(z) << '\n';
        std::cout << "Value pointed to is: " << *z << '\n';
}
--------8<--------
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 20 total points
ID: 9625908
For the record VC7.0 is non-compliant in this regard, but VC7.1 gets it right. Thanks to cliff_m at http:/Programming/Programming_Languages/Cplusplus/Q_20776663.html#9625611 for putting me straight.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now