| re: stl vector: what is wrong with this code?
<kfallis@kc.rr.com> wrote in message
news:s%Ngb.16140$%C5.4635@twister.rdc-kc.rr.com...[color=blue]
> luigi,
>
> try dereferencing TClass::a and TClass::b in the vectors when you set the
> member variables
>
> int* a;
> a = some memory location
> *a = (dereferencing) i.e. the contents of that memory location
>
> vec[i].a[j]=j or in basic form vec[i].a[j]= 0 (first time through) is[/color]
saying[color=blue]
> that a = 0 and when you try and delete memory you dont own...KABOOM
> it would be better to say *(vec[i].a[j]) = 0 or j giving you modification[/color]
of[color=blue]
> the contents of the memory location
>
> Hope this helps....
>
> Kevin
>[/color]
Kevin,
[Please post your answers after the text you're referring to (or
interspersed with it at least).]
Your answer is not correct. The OP allocated two arrays of int, not two
arrays of pointers. Your method will try to dereference the int at
vec[i].a[j], but that's not allowed, since they're not pointers. The line
"a = new int[npts]" allocates an array of ints of size npts, and the
notation a[j] refers to the int at index j in that array. Also, setting
vec.[i].a[j] = 0 will set a[j] to 0, not a. The pointer 'a 'points to
memory somewhere else, where the int's are actually stored. The int's are
*not* stored at the address of 'a', but at the address 'a' *points to*.
The OP's problem has to do with a lack of a proper copy-constructor, I
believe. (See Attila's response.)
-Howard |