[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: c++ question: alloc'ing and free'ing arrays
From:       aleXXX <alexander.neundorf () gmx ! net>
Date:       2002-08-06 21:00:46
[Download RAW message or body]

On Saturday 03 August 2002 20:05, Marcos Dione wrote:
>   a friend of mine is developing a class that has lots of arrays of
> different types (POD and non-POD types), and the class must resize
...
>     delete[] array;
>   array= new int[size]
>
> in the resize method. with non-POD types, she uses

ctor ?

>   npt **npa;
>
>   npa= NULL;
>
>   npa= new npt* [size]
>   for (int i= 0; i<size; i++)
>     npa[i]= new npt;

dtor ?

>   for (int i= 0; i<size; i++)
>     delete npa[i];
>   delete[] npa;

resize ?

>   if (npa!=NULL) {
>     for (int i= 0; i<size; i++)
>       delete npa[i];
>     delete[] npa;
>   }
>   npa= new npt* [size]
>   for (int i= 0; i<size; i++)
>     npa[i]= new npt;
>
>   in the same places respectively.

Lazy workaround: use a container data structure, e.g. QPtrList from Qt or a 
list from the STL (or a vector).

>   now, the problem is that in certain circumstances the resize methods
> segfault in the new (!!!) statement. using gdb, running the test
> program I get this backtrace:
...
> code works on other circumstances, like in the constructor. I hope you
> can give me a hint on what's going on... also, I would like to know
> about any lib that can store and retrieve objects to/from disk. as you

The data contained in the objects has to be serialized/marshalled in order to 
be stored on disk. 
You could use a QDataStream, or create a XML document, or something else :-)

> can see, she's trying to parse a Network from an istream.

What do you mean by "parse a network from an istream" ?
What do you want to do ?

Bye
Alex

 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic