[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

pointers initialization


1.
Int *x is uninitialized

Int *y =new int;

Is y considered initialized to 0 or is it uninitialized ?

0
Gipsy
Asked:
Gipsy
  • 6
  • 2
  • 2
  • +1
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now