Advanced Uninstaller PRO 9.0 with Serial key
Advanced Uninstaller PRO with serial key
Advanced Uninstaller PRO 9.0 free download
uninstaller pro review
Advanced Uninstaller PRO 9.0 with Serial keyQ:
Why is this struct initialisation by address less safe than value initialisation?
To my understanding, structs, with public fields, can be initialized either by value or by address (using new or & operator).
(Please correct me if I'm wrong).
So the question is:
Why does the compiler flag the following as a possible bug:
struct s {
int n1 = 9;
int n2 = 0;
};
s my_s;
But does not flag the following:
struct s {
int n1 = 9;
int n2 = 0;
};
s* my_s = new s();
?
A:
Your code is not safe because you're relying on access restrictions, which do not always exist.
struct s {
int n1 = 9;
int n2 = 0;
};
s* my_s = new s();
// my_s is not guaranteed to be what it looks like.
Here, because of access control, it may be that the new-operator's memory is allocated at a higher base (on a thread where a previous operation allocated memory to a higher base).
So you can rely on memory management mechanisms to do their job, but struct-initialization is not guaranteed to be safe.
A:
There are two issues here:
1) The address member of the struct is not initialized.
This is not guaranteed to work, since the address of a local variable is not guaranteed to be constant.
Consider the following code:
struct s {
int n1 = 9;
int n2 = 0;
};
struct s *s1 = new s;
struct s *s2 = new s;
delete s2;
This can lead to undefined behavior.
This can be fixed with memset:
struct s *s1 = be359ba680
Related links:
Comments