pointers initialization


1.
Int *x is uninitialized

Int *y =new int;

Is y considered initialized to 0 or is it uninitialized ?

GipsyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mnashadkaCommented:
y is uninitialized, but allocated.  It will not point to 0, and the data at y will not necessarily be 0.
0
rstaveleyCommented:
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
Elias SalibaCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Elias SalibaCommented:
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
rstaveleyCommented:
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
rstaveleyCommented:
If I'd been more sober I'd have deleted tmp with [].... but I'm not. Sorry.
0
dennis_georgeCommented:
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
dennis_georgeCommented:
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
rstaveleyCommented:
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
rstaveleyCommented:
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
rstaveleyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.