[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

dynamically allocate array variable type LPWSTR

Samant.Trupti@gmail.com

10/31/2008 3:04:00 PM

HI,

I want to dynamically allocate array variable of type LPWSTR.
Code looks like this...

main() {
LPWSTR *wstr;
int count = Foo (wstr);
for (int i = 0; i < count; i++)
//print each element;
}

int Foo(LPWSTR *wstr)
{
int count = 0;
while (!done){
//Here I need to allocate "wstr" one element by one element. How
to do that?
// I don't know the count
count ++;
}

Where should I do delete?

Thanks
Trupti
3 Answers

Victor Bazarov

10/31/2008 3:37:00 PM

0

Samant.Trupti@gmail.com wrote:
> HI,
>
> I want to dynamically allocate array variable of type LPWSTR.
> Code looks like this...
>
> main() {

int main() {

> LPWSTR *wstr;

I recommend initialising all pointers to 0.

> int count = Foo (wstr);
> for (int i = 0; i < count; i++)
> //print each element;
> }
>
> int Foo(LPWSTR *wstr)

Whatever you allocate here will probably change 'wstr'. The problem is,
the caller of 'Foo' will not know of those changes. If you expect the
caller to access the array, you need to make sure the changes are also
transferred back to the caller. For that pass 'wstr' either by
reference or by pointer:

int Foo(LPWSTR * &wstr)

> {
> int count = 0;
> while (!done){
> //Here I need to allocate "wstr" one element by one element. How
> to do that?

What does it mean "one element by one element"? Is it supposed to grow
somehow? Is it conditional? Based on what?

> // I don't know the count
> count ++;

Huh? Where is the return statement?

> }
>
> Where should I do delete?

Whoever *owns* the allocated array should dispose of it at the point
they don't need it any longer. I would say, the 'main' seems to get the
ownership once 'Foo' is done allocating, so 'delete[]' should be in the
'main' function (as written, at least).

--------------------------- with all those things in mind, you're
probably much better off using a vector of LPWSTR:

#include <vector>

void Foo(std::vector<LPWSTR>& wstr);

int main()
{
std::vector<LPWSTR> wstr;
Foo(wstr);
... // no need to delete anything, 'std::vector'
// takes care of its own storage. And it has
// '.size()' too, so you don't need to keep
// the count, it does it for you.
}

void Foo(std::vector<LPWSTR>& wstr)
{
while (!done)
{
...
wstr.push_back( /* some new LPWSTR value */ );
}
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Samant.Trupti@gmail.com

10/31/2008 4:33:00 PM

0

On Oct 31, 8:36 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Samant.Tru...@gmail.com wrote:
> > HI,
>
> >   I want to dynamically allocate array variable of type LPWSTR.
> > Code looks like this...
>
> > main() {
>
> int main() {
>
> >    LPWSTR  *wstr;
>
> I recommend initialising all pointers to 0.
>
> >    int count = Foo (wstr);
> >    for (int i = 0; i < count; i++)
> >       //print each element;
> > }
>
> > int Foo(LPWSTR *wstr)
>
> Whatever you allocate here will probably change 'wstr'.  The problem is,
> the caller of 'Foo' will not know of those changes.  If you expect the
> caller to access the array, you need to make sure the changes are also
> transferred back to the caller.  For that pass 'wstr' either by
> reference or by pointer:
>
>     int Foo(LPWSTR * &wstr)
>
> > {
> >    int count = 0;
> >    while (!done){
> >      //Here I need to allocate "wstr" one element by one element.  How
> > to do that?
>
> What does it mean "one element by one element"?  Is it supposed to grow
> somehow?  Is it conditional?  Based on what?
>
> >     // I don't know the count
> >    count ++;
>
> Huh?  Where is the return statement?
>
> >   }
>
> > Where should I do delete?
>
> Whoever *owns* the allocated array should dispose of it at the point
> they don't need it any longer.  I would say, the 'main' seems to get the
> ownership once 'Foo' is done allocating, so 'delete[]' should be in the
> 'main' function (as written, at least).
>
> --------------------------- with all those things in mind, you're
> probably much better off using a vector of LPWSTR:
>
> #include <vector>
>
> void Foo(std::vector<LPWSTR>& wstr);
>
> int main()
> {
>      std::vector<LPWSTR> wstr;
>      Foo(wstr);
>      ...           // no need to delete anything, 'std::vector'
>                    // takes care of its own storage.  And it has
>                    // '.size()' too, so you don't need to keep
>                    // the count, it does it for you.
>
> }
>
> void Foo(std::vector<LPWSTR>& wstr)
> {
>      while (!done)
>      {
>          ...
>          wstr.push_back( /* some new LPWSTR value */ );
>      }
>
> }
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask

Hmmm Ok I will use vectior that seems very simple. Thank you.

But just for my Knowledge
If I do not know the "count" in Foo to allocate the number of elements
in arrary at the beginning
I need to do realloc everytime, right?
Trupti

Victor Bazarov

10/31/2008 5:06:00 PM

0

Samant.Trupti@gmail.com wrote:
> [..]
> If I do not know the "count" in Foo to allocate the number of elements
> in arrary at the beginning
> I need to do realloc everytime, right?

No, not every time. Nobody tells you to allocate exactly the number of
elements you need. You just need to remember how many you allocated and
how many you are currently using. That's what 'std::vector' does behind
the scenes, essentially. It does reallocate when it grows, but not on
every 'push_back'. And you can prevent it from reallocating if you tell
it to 'reserve' as many as you possibly will ever use (although it is
not necessarily known or easy to calculate).

RTFM about 'std::vector', and better if you actually get a decent book
for it, like "The C++ Standard Library: a Tutorial and a Reference" by
Josuttis.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask