Alexandru Sofronie wrote:
> 
> Hi all!
> 
> I have this question and i don't know anywhere else to post:
> There is a vector of strings with variant length each one.
> Please cout << me << a solution.
> Or, check the implement below, and PLEASE HELP ME!!!!!!!!!!!
> Or, maybe, please indicate another mailing list with things like that, if
> this is not suitable (?!?). Maybe something like C/C++ mailing list?!?!
> 
> /*********** test.cpp*************/
>     char **p;
> 
> // question is: what is the difference between the two things?
> p=new (char**)[IntElems];
> // or this:
> p=new char*[IntElems];
> 
> /****************end***************/
> 
> If i put there p=new (char*)[IntElem] , the compiler (g++ or Borland C 3.X,
> 5.X) generates an error (cannot convert char* to char**) or something like
> this - LOGIC!

With g++ from egcs-2.91.66 (RedHt 6.2)

p=new (char*)[5];
and
p=new char*[5];
works fine.
p=new char**[5];
gives :assignment to `char **' from `char ***'
which is correct.

'new char*[5]' allocates continuous memory for 5 pointers to
char's and returns a pointer to the first location which is
a char**, pointer to a pointer to a char.

'new char**[5]' allocates continuous memory for 5 pointers to
char* and returns a pointer to the first location which is
a char***, pointer to a pointer to a pointer to a char, and
p is only a char** hence the error.

I'm not sure about the delete.
but p represents a 2 deminsional array.
depending on how it's filled I guess.

delete p would only free the memory holding the char*s I think.
so you would need to free the memory holding the chars firts
before the char*s in p are lost, so I try the delete []p
method (delete [][]p won't compile either)

If you had char*** p, then you'd need a loop to free the
inner char*s first.

Question though. Why are you using char** in C++?
You should probably create a new data type of even
a full class instead. You should seldom need that
high a level af indirection.

example: 

  String* p;
  p=new String[IntElems];

then you are anly dealing with an array of Strings
and don't have to worry about the fact that the
elements are also arrays.

        -Thomas

-- 
To unsubscribe:
mail -s unsubscribe [EMAIL PROTECTED] < /dev/null

Reply via email to