Re: strcpy and the copy constructor

From: RonHiler (rhiler_at_rjcyberware.com)
Date: 10/19/04


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



Relevant Pages

  • Re: when can pass by value be dangerous?
    ... the object's constructor function is not called. ... > used as an argument allocates memory and frees that memory when it is ... Suppose the object has a pointer inside it to another, ... pointers to the SAME sub-object instead of to separate copies of that ...
    (comp.lang.cpp)
  • Re: Readings on clr optimization?
    ... The compiler cannot inline a call to a constructor if the class has a static ... Weak references are special objects that allow you to track references that ... There aren't many implication with memory ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Readings on clr optimization?
    ... Funny...Those are my 3 favorite advanced .Net books! ... The compiler cannot inline a call to a constructor if the class has a static ... Weak references are special objects that allow you to track references that ... There aren't many implication with memory ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Controlled types and exception safety
    ... >> constructor throws/raises, the object is considered to be never created ... you'll have memory leaks and other such badness. ... > actually wish is to have access to the left side of assignment. ... Ada doesn't really have user-defined assignment; ...
    (comp.lang.ada)
  • Re: Thread safety of readonly members
    ... > Since all you have is the constructor and getVal, ... On SMP machines, if no memory ... >> the lock. ... C++ doesn't make any promises as to memory consistency in the ...
    (microsoft.public.dotnet.languages.csharp)