[cfe-dev] (no subject)

mats petersson mats at planetcatfish.com
Wed Aug 5 00:01:57 PDT 2015


It is a case of "undefined value" and will depend on what happens to be on
the stack where the struct S is located. This may be the same or different
value, depending on a number of factors:
1. Is the stack at the same place every time.
2. What (if anything) has this been used for before you got here...
3. If the code is optimised or not.
4. Processor architecture.

For example, if you have address space randomization turned on, the return
address and local variable addresses that may be stored on the stack will
vary, where if you haven't got that, it will be constant. If that part of
the stack has NEVER been used, then zero is a likely value, but some
runtime environment will fill uninitialized stack locations with other
patterns.

And of course, if the code is optimised, it will depend on what registers
the value for x ends up in, and what that was holding before - and this of
course also depends on the actual processor architecture.

--
Mats

On 5 August 2015 at 06:29, Nikola Smiljanic <popizdeh at gmail.com> wrote:

> What version of clang, I've just tried this with recent revision and it's
> printing random values.
>
> On Wed, Aug 5, 2015 at 3:36 AM, Anders Granlund <
> anders.granlund.0 at gmail.com> wrote:
>
>> It seems like this program always prints 0. I used the following command
>> line:
>>
>> clang++ prog.cc -std=c++14 -pedantic-errors
>>
>>  It looks like clang does an unnecessary zero-initialization of x here.
>> Why is that?
>>
>> #include <iostream>
>> struct S { int x; S() { std::cout << x << std::endl; } };
>> int main() { S s; }
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>
>>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150805/e837c71f/attachment.html>


More information about the cfe-dev mailing list