This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C and C++ : Partial initialization of automatic structure
I'm looking for a fast way to init local array with zeros. (By "Fast", I mean "Fast to type.") When I do the following:
HANDLE hHandles[32] = {0};
does it zero out the first element or all 32?
It initializes all the 32 elements to zero.
See this surprisingly popular answer for details/alternatives. The difference between C and C++ seems to be that in C++ {} will do zero-initialization as well.
Related
This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
How do compilers treat variable length arrays
(4 answers)
Closed 2 years ago.
In the below program, when does the array c gets initialize ?
If is it at compile time then how I got the output as 20 which is the product of n i.e. 5 and size of integer in C++ i.e. 4 ,after I pass the value of n at runtime. And if it got allocated at runtime then, how is it possible ,as for runtime allocation we have to use new operator which is not use in this program.
This question already has answers here:
How are variable names stored in memory in C?
(5 answers)
Closed 5 years ago.
It is always advised to use bigger and clearer variable names in programming suppose I define :
int captain=10 ;
I know that 10 is stored but where is the "captain" or the variable name stored and who allocates memory for it?
In the end they aren't stored anywhere unless you have linked in additional debug information.
They're translated into memory (be it RAM or ROM) addesses and address offsets by your toolchains linker.
This question already has answers here:
When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?
(9 answers)
Closed 7 years ago.
So after allocating char array in debugging windows I can see my array is filled with 0xCC. What does it mean? (-52 = 0xCC)
Uninitialized built-in types have an in-determined value, trying to read it is undefined behavior.
The actual values you can see depend on the compiler: You might for example see garbage, zeroes or (what seems to be the case in your example) some special value indicating "data uninitialized".
It's there as a sentinel value so that you know that the memory is uninitialized.
See the /GZ compiler switch.
This question already has answers here:
C: How to wrap a float to the interval [-pi, pi)
(15 answers)
Closed 9 years ago.
I'm wondering if it's possible to define a custom data type which can only take a value between -3.1415926535897932 and 3.1415926535897931.
The idea is that a value below or above the range would automatically "wrap-around", eliminating the need to write code to do the conversion and also eliminating the possibility of error somewhere.
Yes it is possible. In the method that sets the value check to see if it outside the limits and if so do whatever operation you want to do to force it to be inside the limits. fmod is one good choice for operation.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the most random function in C++?
In C++, is this safe:
int main() {
srand(time(0));
unsigned char arr[10];
for(int i=0; i <sizeof(arr); ++i)
arr[i] = (unsigned char)rand();
}
Is there a better way to randomly fill a byte array in a platform independent way? failing that, is there a better way to do this on windows? (I know rand() isn't a very good PRNG, I'm just using it as an example).
Thank you!
What about using boost.random? The generators it uses can be passed to std::generate to fill your array, it's platform independent and headers-only. I would give a code sample but have no boost install available at the moment.
Edit: as mentioned: in C++0x (the upcoming standard), you can use tr1::random, which is essentially just the boost library becoming part of the standard C++ library.