Hi Folks,
I’m suffered from basic conflicts in std::vector of a structure andsizeof(), which seems to be caused by basic CINT bugs or my miss.
Question1:
The method vector::operator[] and vector::back() of the pointer of a structure return
invalid pointers in the loop and after loop. Sample shows, the methods return target structure not pointer.
Question2:
sizeof( the pointer of structure defined by typedef ) returns the structure size not pointer size. Sample shows, sizeof(Data) return 8 not 4.
Compiled code show no conflict, but CINT/ROOT version 5.16.16 show invalid results.
Compiled code shows:
Question 1: std::vector
Address of name = 804931a
Before: 0 804b050 back 804b050 dir 804b050
InLoop: 1 804b1a8 back 804b1a8 dir 804b1a8
After : 2 804b1b8 back 804b1b8 dir 804b1b8
Question 2: sizeof structure pointer
Sizeof DataRec= 8, Data= 4
Root Interpreter:
Question 1: std::vector
Address of name = 814949c
Before: 0 8b330e8 back 8b330e8 dir 8b330e8
InLoop: 1 8b33298 back 814949c dir 814949c
======= =======
After : 2 8b36250 back 814949c dir 814949c
======= =======
Question 2: sizeof structure pointer
Sizeof DataRec= 8, Data= 8
===
Thanks,
Yukio Okuda
#include <stdio.h>
#include <vector>
typedef struct {
char *name ;
int seq ;
} DataRec ;
typedef DataRec *Data ;
DataRec * CreateData( char *name );
void Check( char *msg, DataRec * org, int idx ) ;
std::vector<DataRec *> dataVec ;
DataRec * CreateData( char *name ) {
static int gSeq = 0 ;
DataRec * data = new DataRec ;
data->name = name ;
data->seq = gSeq ;
++gSeq ;
return data ;
}
void Check( char *msg, DataRec * org, int idx ) {
DataRec * back, *dir ;
back = dataVec.back( ) ;
dir = dataVec[idx] ;
printf( "%s %d %x back %x dir %x \n",
msg, org->seq, org, back, dir ) ;
}
int main( ) {
char * name = "Top" ;
printf( "Question 1: std::vector \n" );
printf( "Address of name = %x \n", name ) ;
DataRec * top = CreateData( name) ;
dataVec.push_back( top ) ;
Check( "Before:", top, 0 ) ;
for ( int i = 1 ; i < 2 ; ++i ) {
DataRec * top = CreateData( name) ;
dataVec.push_back( top ) ;
Check( "InLoop:", top, 1 ) ;
}
top = CreateData( name ) ;
dataVec.push_back( top ) ;
Check( "After :", top, 2 ) ;
printf( "\nQuestion 2: sizeof structure pointer \n" );
printf( "Sizeof DataRec= %d, Data= %d \n", sizeof(DataRec), sizeof(Data) ) ;
}