Firstly, "creating a reference with new" isn't exactly what you're doing. To understand this, the code needs to be broken up...
new float(1.3);
this allocates 4 bytes of memory for a float, constructs the float using a 1.3 double constant, and returns a pointer to the start of the memory - the first of the 4 bytes.
*new float(1.3);
this "dereferences" the pointer returned by the new operator, so instead of reading the pointer value, you're reading the 4-byte float
data it points to.
float &f = *new float(1.3);
constructs a reference f, using the float
provided by new. While (AFAIK) references are implemented as pointers with greater potential for compiler optimizations (?), this is one part people are complaining about. Conceptually, the reference is the float and you shouldn't be assuming you can retrieve the address, free the memory and hence invalidate the reference later.
However, as you have found, it does actually work and you can retrieve the address...
&f
produces a pointer to the memory at the reference's location, which you can then
delete &f;
To answer your actual question...
float f1 = *new float;
can be rewritten as follows
float f1; //a float on the stack!
f1 = *new float; //should probably be: *new float(1.3)
which does not initialize a reference, but instead copies the data allocated by new to the float on the stack. After the copy, the memory address returned by new
is lost forever - you have "leaked" memory.
Now on to why it crashes.
&f1;
creates a pointer to the float f1
, on the stack. This memory has not been created by new
or the memory allocation library new ultimately uses. Attempting to free a memory address that doesn't exist in the memory allocation library...
delete &f1;
...results in your crash.
The difference between float &f
and float f1
: one is a reference, internally implemented as a pointer (which is assumed never to change) and one is an actual float variable declared on the stack which does not reference other memory.
What you should be doing instead is
float *f = new float(1.3f);
// access f as *f = 1.2f or cout << *f
delete f;