Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector of char arrays

Reply
Thread Tools

vector of char arrays

 
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      01-06-2005
I have a fixed-size string array like this

char m_titles[80000][8];

and can't figure out how to change it to a dynamically-sized vector like
this:

std::vector<char[8]> m_titles;

The declaration compiles but then calling reserve or resize leads to
obscure compiler errors. Can this be done so that it allocates one big
array of chars, not 80000 heap strings?

--
Scott McPhillips [VC++ MVP]

 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      01-06-2005
Scott McPhillips [MVP] wrote:
> I have a fixed-size string array like this
>
> char m_titles[80000][8];
>
> and can't figure out how to change it to a dynamically-sized vector
> like this:
>
> std::vector<char[8]> m_titles;
>
> The declaration compiles but then calling reserve or resize leads to
> obscure compiler errors.


Yeah, this fails both copyconstuctibility and assignability.

> Can this be done so that it allocates one
> big array of chars, not 80000 heap strings?


You can use a struct with a member array, but you can't be sure there won't be
padding between the instances.

Jonathan


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      01-06-2005
Scott McPhillips [MVP] wrote:
> I have a fixed-size string array like this
>
> char m_titles[80000][8];
>
> and can't figure out how to change it to a dynamically-sized vector like
> this:
>
> std::vector<char[8]> m_titles;
>
> The declaration compiles but then calling reserve or resize leads to
> obscure compiler errors. Can this be done so that it allocates one big
> array of chars, not 80000 heap strings?
>

The problem is that the type char[8] isn't assignable so it's not going
to work inside a vector.

You could either do a vector of vector,
or a vector of strings,
or
struct c8 {
char c[8];
};
vector<c8>

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-06-2005

"Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
news:(E-Mail Removed)...
> I have a fixed-size string array like this
>
> char m_titles[80000][8];
>
> and can't figure out how to change it to a dynamically-sized vector like
> this:
>
> std::vector<char[8]> m_titles;
>
> The declaration compiles but then calling reserve or resize leads to
> obscure compiler errors. Can this be done so that it allocates one big
> array of chars, not 80000 heap strings?


Standard containers require that the contained objects
be copyable and assignable. Arrays do not qualify.

Use a vector of vectors.

-Mike


 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      01-06-2005
Scott McPhillips [MVP] wrote:
> I have a fixed-size string array like this
>
> char m_titles[80000][8];
>
> and can't figure out how to change it to a dynamically-sized vector

like
> this:
>
> std::vector<char[8]> m_titles;


Others have commented on this issue already. I just want to point out
that the upcoming library TR has an elegant solution to this which is
not as expensive as a vector of vectors: 'std::tr1::array' is a class
template for fixed size arrays. The replacement would look like this:

| std::vector<std::tr1::array<char, 8> > m_titles;

Unfortunately, TR1 implementations are not yet available, AFAIK. On the
other hand, Boost has an implementation of 'array' which is close if
not
identical to the TR1 version (see <http://www.boost.org/>).
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-06-2005

"Dietmar Kuehl" <(E-Mail Removed)> wrote in message > Others have
commented on this issue already. I just want to point out
> that the upcoming library TR has an elegant solution to this which is
> not as expensive as a vector of vectors: 'std::tr1::array' is a class
> template for fixed size arrays. The replacement would look like this:
>
> | std::vector<std::tr1::array<char, 8> > m_titles;
>
> Unfortunately, TR1 implementations are not yet available, AFAIK. On the
> other hand, Boost has an implementation of 'array' which is close if
> not
> identical to the TR1 version (see <http://www.boost.org/>).
> --


Just curious: what's the "tr1" stand for? Is it really going to be "tr1"?
Having to use std:: isn't bad, but having to do std::tr1: is starting to
look like crap to me. I know it's nice to limit the number of characters in
an identifier (to reduce typing), but at least I try to make my id's
somewhat meaningful. I just hate to see my c++ code cluttered with names
that make no earthly sense!
Thanks,
-Howard



 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      01-06-2005
Howard wrote:

> Just curious: what's the "tr1" stand for? Is it really going to be "tr1"?
> Having to use std:: isn't bad, but having to do std::tr1: is starting to
> look like crap to me. I know it's nice to limit the number of characters in
> an identifier (to reduce typing), but at least I try to make my id's
> somewhat meaningful. I just hate to see my c++ code cluttered with names
> that make no earthly sense!



This is some text from my site:

There will be additional ISO C++ library facilities in a library named
TR1 and in namespace std::tr1, due to 2005-2006 time frame. Proposals
that have been accepted are here:

http://www.open-std.org/jtc1/sc22/wg...al_report.html




--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      01-06-2005
Howard wrote:
> Just curious: what's the "tr1" stand for?


"First (Library) Technical Report". Since "1tr" or "1thtr do not work
and
"firsttr" is much longer, we settled on "tr1". That it is technically
not
really the first technical report issued by the C++ committee (this was
the TR on performance) may be a little strange but it still is the
first
library TR.

> Is it really going to be "tr1"?


See Section 1.3 (tr.intro.namespaces) of the current document
(<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1711.pdf>).

> Having to use std:: isn't bad, but having to do std::tr1: is starting

to
> look like crap to me.


If the components make it into C++0x they will be moved to 'std::'.
However,
the TR does not really modify the standard and it is unclear whether
the
components will really become part of the standard and if so, whether
they
remain unchanged. Putting the names into a clearly distinguished
namespace
which is only used for that purpose allows implementers to retain the
TR
version for compatibility issues and still provide the standard version
in
the appropriate namespace.

It would have been possible to choose '::tr1' (i.e. not nest it into
namespace
'std') but this would possibly be in conflict with user namespaces:
'::tr1' is
definitely not reserved. 'std::tr1' is a safe place where a user shall
not
place his own names (if he does so, he deserves what he gets). Of
course,
'tr1' may be a macro defined by users. In this case, a user cannot use
the
'tr1' components but that's bad luck. Of course, loads of new names
which
cannot be used as macro names are introduced by the TR but since it is
conventional to at least start macro names with a capital letter, the
chances
of conflicts are actually pretty low.

> I know it's nice to limit the number of characters in
> an identifier (to reduce typing), but at least I try to make my id's
> somewhat meaningful.


'std::tr1' *is* meaningful. It is clearly the best name available: even
dropping the '1' to make it shorter is not a reasonable choice as we
will
start working on 'tr2' ("Second (Library) Technical Report") at the
next
meeting in Lillehammer. We might get rid of the 'r', though, but I
prefer
'tr1' over 't1'.

> I just hate to see my c++ code cluttered with names
> that make no earthly sense!


You are free to use using directives or using declarations and only
that
part of your code uses these names. I wouldn't call that cluttering. Of
course, you are also free not to use the TR1 components at all.
Personally, I don't think the latter approach is really such a smart
move
once implementations of TR1 are widely available.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-06-2005

"Dietmar Kuehl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Howard wrote:
>> Just curious: what's the "tr1" stand for?

>
> "First (Library) Technical Report". Since "1tr" or "1thtr do not work
> and
> "firsttr" is much longer, we settled on "tr1". That it is technically
> not
> really the first technical report issued by the C++ committee (this was
> the TR on performance) may be a little strange but it still is the
> first
> library TR.
>
>> Is it really going to be "tr1"?

>
> See Section 1.3 (tr.intro.namespaces) of the current document
> (<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1711.pdf>).
>
>> Having to use std:: isn't bad, but having to do std::tr1: is starting

> to
>> look like crap to me.

>
> If the components make it into C++0x they will be moved to 'std::'.
> However,
> the TR does not really modify the standard and it is unclear whether
> the
> components will really become part of the standard and if so, whether
> they
> remain unchanged. Putting the names into a clearly distinguished
> namespace
> which is only used for that purpose allows implementers to retain the
> TR
> version for compatibility issues and still provide the standard version
> in
> the appropriate namespace.
>
> It would have been possible to choose '::tr1' (i.e. not nest it into
> namespace
> 'std') but this would possibly be in conflict with user namespaces:
> '::tr1' is
> definitely not reserved. 'std::tr1' is a safe place where a user shall
> not
> place his own names (if he does so, he deserves what he gets). Of
> course,
> 'tr1' may be a macro defined by users. In this case, a user cannot use
> the
> 'tr1' components but that's bad luck. Of course, loads of new names
> which
> cannot be used as macro names are introduced by the TR but since it is
> conventional to at least start macro names with a capital letter, the
> chances
> of conflicts are actually pretty low.
>
>> I know it's nice to limit the number of characters in
>> an identifier (to reduce typing), but at least I try to make my id's
>> somewhat meaningful.

>
> 'std::tr1' *is* meaningful.


Well, it's meaningful if you know what it means!

(I was thinking along the lines of using a name like "Length" instead of "l"
in my code. And in that respect "tr1" by itself doesn't tell me much.
Something like "ext05" might tell me that it was the "2005 extension". But
no problem, I'm a big boy, and can deal with it! )

> It is clearly the best name available: even
> dropping the '1' to make it shorter is not a reasonable choice as we
> will
> start working on 'tr2' ("Second (Library) Technical Report") at the
> next
> meeting in Lillehammer. We might get rid of the 'r', though, but I
> prefer
> 'tr1' over 't1'.
>
>> I just hate to see my c++ code cluttered with names
>> that make no earthly sense!

>
> You are free to use using directives or using declarations and only
> that
> part of your code uses these names. I wouldn't call that cluttering. Of
> course, you are also free not to use the TR1 components at all.
> Personally, I don't think the latter approach is really such a smart
> move
> once implementations of TR1 are widely available.
> --
> <(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
> <http://www.contendix.com> - Software Development & Consulting
>


Okee dokee. Thanks for the info, guys!
-H



 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      01-06-2005
Howard wrote:
> Just curious: what's the "tr1" stand for?


"First (Library) Technical Report". Since "1tr" or "1thtr do not work
and
"firsttr" is much longer, we settled on "tr1". That it is technically
not
really the first technical report issued by the C++ committee (this was
the TR on performance) may be a little strange but it still is the
first
library TR.

> Is it really going to be "tr1"?


See Section 1.3 (tr.intro.namespaces) of the current document
(<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1711.pdf>).

> Having to use std:: isn't bad, but having to do std::tr1: is starting

to
> look like crap to me.


If the components make it into C++0x they will be moved to 'std::'.
However,
the TR does not really modify the standard and it is unclear whether
the
components will really become part of the standard and if so, whether
they
remain unchanged. Putting the names into a clearly distinguished
namespace
which is only used for that purpose allows implementers to retain the
TR
version for compatibility issues and still provide the standard version
in
the appropriate namespace.

It would have been possible to choose '::tr1' (i.e. not nest it into
namespace
'std') but this would possibly be in conflict with user namespaces:
'::tr1' is
definitely not reserved. 'std::tr1' is a safe place where a user shall
not
place his own names (if he does so, he deserves what he gets). Of
course,
'tr1' may be a macro defined by users. In this case, a user cannot use
the
'tr1' components but that's bad luck. Of course, loads of new names
which
cannot be used as macro names are introduced by the TR but since it is
conventional to at least start macro names with a capital letter, the
chances
of conflicts are actually pretty low.

> I know it's nice to limit the number of characters in
> an identifier (to reduce typing), but at least I try to make my id's
> somewhat meaningful.


'std::tr1' *is* meaningful. It is clearly the best name available: even
dropping the '1' to make it shorter is not a reasonable choice as we
will
start working on 'tr2' ("Second (Library) Technical Report") at the
next
meeting in Lillehammer. We might get rid of the 'r', though, but I
prefer
'tr1' over 't1'.

> I just hate to see my c++ code cluttered with names
> that make no earthly sense!


You are free to use using directives or using declarations and only
that
part of your code uses these names. I wouldn't call that cluttering. Of
course, you are also free not to use the TR1 components at all.
Personally, I don't think the latter approach is really such a smart
move
once implementations of TR1 are widely available.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM



Advertisments