Re: strcpy and the copy constructor
From: RonHiler (rhiler_at_rjcyberware.com)
Date: 10/19/04
- Next message: Maurizio Loreti: "Re: [URGENT] fgets reading last line in file twice"
- Previous message: Jackie: "Re: count of pointer to array question"
- In reply to: Adrian: "Re: strcpy and the copy constructor"
- Next in thread: John Harrison: "Re: strcpy and the copy constructor"
- Reply: John Harrison: "Re: strcpy and the copy constructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 18 Oct 2004 23:11:37 -0700
"Adrian" <nntp@bluedreamer.com> wrote in message news:<cl14kp$ofv$1@titan.btinternet.com>...
>
> Since you delcare Name as a char array the default copy constructor will
> work fine, it you change name to pointer then all bets are off.
It does not. The defualt constructor gives the same crash. And I'm
not using the default constructor for three reasons
1) I will be changing the char arrays to pointers and using 'new' to
allocate memory for them in the copy constructor (otherwise I'll be
wasting a lot of space, as I will have about 800 techs by the end).
For now they are arrays just to keep things simple until I get this
part debugged :)
2) The class in question contains vectors (see the header for
BreakthroughClass), and it's been my experience that default copy
constructors do not handle vectors well.
3) I consider it bad form to use default copy constructors in ANY
case, even when it seems perfectly safe. I will either prototype them
as private (and not define the function) if the class is meant to be
singly instanced, or, when I HAVE to have a copy constructor (such as
this case where the class is used as a vector member), I create my
own.
> Also as
> mentioned elsewhere use std::string
>
for purposes of performance, I dislike std::string.
I've discovered an odd thing. Having decided the source data was fine
(based on the ODS output), I turned my attention to where the data was
going. And, as it turns out, the code seems to work just fine when I
add a single line to the constructor for TechManagerClass
TechManagerClass::TechManagerClass()
{
Breakthroughs.clear();
Breakthroughs.reserve(100);
}
The addition of the reserve command seems to do the trick, though I'm
not sure why that should be. It appears as though the memory for the
vectors is not being allocated before the strcpy command in the CC.
Which seems very strange to me, as what I know about vectors suggests
that ought not be the case (I thought the first thing push_back() did
was allocate memory if needed).
Does someone know the reason for this? Is it because of the large
memory footprint of the class (1296 bytes per member if I calculate
that right)?
I've also considered the possibility that I'm just hiding the problem,
that in fact strcpy is still leaking (somehow) but now it has memory
space to leak into (because of the reserved space), and I'll run right
back into the same issue once I get more techs input into the resource
file. Can someone comment? :)
Thanks guys for the responses. I know it's hard to debug someone
else's code, and I appreciate the efforts.
Ron
- Next message: Maurizio Loreti: "Re: [URGENT] fgets reading last line in file twice"
- Previous message: Jackie: "Re: count of pointer to array question"
- In reply to: Adrian: "Re: strcpy and the copy constructor"
- Next in thread: John Harrison: "Re: strcpy and the copy constructor"
- Reply: John Harrison: "Re: strcpy and the copy constructor"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|