Does C++ new initialise it's value to zero?
Code:int main() { int *i=new int; cout<<*i;//Will it always be 0 getch(); return 0; }
Does C++ new initialise it's value to zero?
Code:int main() { int *i=new int; cout<<*i;//Will it always be 0 getch(); return 0; }
No. Though your compiler may support such a feature, it's not portable.
My best code is written with the delete key.
Do you know any compiler which does not do so???
Variables of built-in type defined inside the body of a function are uninitialized. You must initialize them explicitly. The new expression allows you to do this through direct-initialization.
int *p = new int(0); //initialized to 0;
Only types that are initialized by default inside a function, are classes that provide a default constructor.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
I know of no compiler (or, more accurately, it's library) which offers a guarantee that memory allocated by operator new is implicitly initialised to zero, as providing such a guarantee implies a performance hit (it would be necessary to explicitly initialise the memory). In your example, the value of *i will correspond to whatever happens to be in memory when it is reserved for *i by the use of operator new. And that value is likely to be random junk.
In any event, what you are doing is yielding undefined behaviour (the act of reading an uninitialised value always yields undefined behaviour, and it is necessary to read a value to print it). Your program could just as easily terminate with a system crash as print junk data.
If you want something created with operator new to be initialised, then you have to do it.
If you are working with user defined types (with a constructor), rather than int, then you can do the initialisation in the default constructor.Code:#include <iostream> int main() { int *i=new int(0); std::cout<<*i;// Now it will always be 0 return 0; }
I think there's a function called ZeroMemory or something which sets everything to 0 ... I might be wrong though.
ZeroMemory is an MS-Windows specific function. memset() is more portable.
Code:// allocate 255 integers int* array = new int[255]; // non-portable use of memset. memset(array,0,255 * sizeof(int)); // more portable way to set elements to 0 for(int i = 0; i < 255; i++) array[i] = 0;
>I think there's a function called ZeroMemory or something which sets everything to 0
memset is a standard C++ function that has the same effect as ZeroMemory and friends. However, it's somewhat dangerous in the presence of objects and certain primitive types. Better to use assignment or constructors to initialize your variables and ensure that it's done properly.
My best code is written with the delete key.
Ok,It was just my luck that I got garbage value as 0. I have tried several times but you will not always get it as zero.
ZeroMemory is a macro which uses memset().If you care about portability you're better off defining it yourself or using memset().Code:#define ZeroMemory(a, b) memset((a), 0, (b))
Yeah let's just ignore what Prelude said about it and throw all our two cents in.
Well the OP will never again declare uninitialized variables on purpose thanks to this thread.
Not on 64-bit Windows. There it's a macro that evaluates to RtlZeroMemory.Originally Posted by Desolation
Not that anyone should care about the implementation.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
We can live in hope.Originally Posted by citizen