Have different results in different versions of gcc

Compare the two codes as follows:

  1 #include <new>
  2 #include <cstdio>
  3 class CA
  4 {
  5 public:
  6     int i, j, k;
  7 };  
  8 
  9 int main()
 10 {
 11     int aa[4] = { 1, 2, 3, 4 };
 12     CA *i = new(aa) CA();
 13     printf("%d %d %d %d\n", aa[0], aa[1], aa[2], aa[3]);
 14     return 0;
 15 }   

  1 #include <new>
  2 #include <cstdio>
  3 class CA
  4 {
  5 public:
  6     int i, j, k;
  7 };  
  8 
  9 int main()
 10 {
 11     int aa[4] = { 1, 2, 3, 4 };
 12     CA *i = new(aa) CA;
 13     printf("%d %d %d %d\n", aa[0], aa[1], aa[2], aa[3]);
 14     return 0;
 15 }   

The difference is in line 12. In gcc4.1.2, these two codes will get the same result 1 2 3 4 But in gcc4.4 and gcc4.5 the first code will get 0 0 0 4

Why?

+5
source share
3 answers

First of all, different versions of GCC have different levels of standard compliance.

" " - ( , - POD), - CA, . . Michael Burr, .

+2

. CA , i, j k , aa [0], aa [1] amd aa [2] .

+1

, . () , int aa[4], . : CA . int [4] class CA .

: aa[0] .

0

All Articles