Advertisements

"Ger" <> wrote in message
news:bv3hfo$3se$...
> Hi,
>
> I have been trying to return a data packet (as a char array) at the end of
a
> function but that seems to be impossible...
> I would like to use this char array in a different class..anyone some
ideas?
>
> greetz,
> Ger
>

You can either:
1 return a char* pointer to an array (better not be an automatic one).
2 create a struct with an array in it and return one of those (almost
certainly a bad idea)
3. pass in a reference to a std::vector and fill it (pprobably the best bet
without knowing more)

Advertisements

Ger writes:
> I have been trying to return a data packet (as a char array) at the end of
a
> function but that seems to be impossible...
> I would like to use this char array in a different class..anyone some
ideas?

Allocate space for the char array, using new, in some function *before*
calling the 'data packet' function. Pass a pointer to this array around
until its meaningful lifetime has expired. Then delete it with delete. The
assignment and deletion do not have to be performed in the same function.

It's also a good idea to send a second argument as
well: the size of the array, so you can protect
against an overrun.

An alternative to the above is to wrap the array in a
class or struct, and return that. Or store the array
contents in a standard container (e.g. vector) and return
that, and if the caller needs the 'raw' array form, you
can recreate it from the container data.

osmium wrote:
> Ger writes:
>
>
>>I have been trying to return a data packet (as a char array) at the end of
>
> a
>
>>function but that seems to be impossible...
>>I would like to use this char array in a different class..anyone some
>
> ideas?
>
> Allocate space for the char array, using new, in some function *before*
> calling the 'data packet' function. Pass a pointer to this array around
> until its meaningful lifetime has expired. Then delete it with delete. The
> assignment and deletion do not have to be performed in the same function.
>
>

Better use new[] and delete[], not new and delete.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

try to avoid using byte arrays to represent strings in c++. std::string is
safer and very flexible.
> Hi,
>
> I have been trying to return a data packet (as a char array) at the end of
a
> function but that seems to be impossible...
> I would like to use this char array in a different class..anyone some
ideas?
>
> greetz,
> Ger
>
>
>
>

return a reference to a std::vector<char> instead - in C++, unless you
absolutely have no choice, you should never use arrays. arrays are evil -
use standard containers instead.

"Ger" <> wrote in message
news:bv3hfo$3se$...
> Hi,
>
> I have been trying to return a data packet (as a char array) at the end of
a
> function but that seems to be impossible...
> I would like to use this char array in a different class..anyone some
ideas?
>
> greetz,
> Ger
>
>
>
>

> "Ger" <> wrote in message
> news:bv3hfo$3se$...
>
>>Hi,
>>
>>I have been trying to return a data packet (as a char array) at the end of
>>a function but that seems to be impossible...
>>I would like to use this char array in a different class..anyone some
>
> ideas?
>
>>greetz,
>>Ger

NKOBAYE027 wrote:
> return a reference to a std::vector<char> instead - in C++, unless you
> absolutely have no choice, you should never use arrays. arrays
> are evil - use standard containers instead.

1) That's a load of baloney.

2) Returning a reference to a local vector doesn't
solve the problem. The vector will be destroyed
before the caller gets to use it.

Jeff Schwab wrote:
> NKOBAYE027 wrote:
> > return a reference to a std::vector<char> instead - in C++, unless
> you > absolutely have no choice, you should never use arrays. arrays
> > are evil - use standard containers instead.
>
> 1) That's a load of baloney.

Actually the "avoid arrays" is a good advice for beginners. You can do
everything with a vector, and more safely. Of course this is no rule for
production code.
> 2) Returning a reference to a local vector doesn't
> solve the problem. The vector will be destroyed
> before the caller gets to use it.

It's fun! (My program works in debug mode, but not in release. Please
help!
> 3) Please don't top-post.

Attila Feher wrote:
> Jeff Schwab wrote:
>
>>NKOBAYE027 wrote:
>> > return a reference to a std::vector<char> instead - in C++, unless
>> you > absolutely have no choice, you should never use arrays. arrays
>> > are evil - use standard containers instead.
>>
>>1) That's a load of baloney.
>
>
> Actually the "avoid arrays" is a good advice for beginners. You can do
> everything with a vector, and more safely. Of course this is no rule for
> production code.

You're right: Vectors are, indeed, more appropriate as a learning tool
(and for most production purposes). I'm saddened by the "arrays are the
devil" nonsense, though.

>>2) Returning a reference to a local vector doesn't
>> solve the problem. The vector will be destroyed
>> before the caller gets to use it.
>
>
> It's fun! (My program works in debug mode, but not in release. Please
> help!

ooops forgive the return of reference faux pas...i was thinking the same as
Nick's answer above - hand in a reference to that vector and then modify the
referenced object within the function. shows that we shouldn't type before
we think...

as to the comment arrays are evil, it's a quote from M. Cline, who sits on
the ANSI C++ standards committee, is his C++ FAQ Lite athttp://www.parashift.com/c -faq-lite/index.html it's meant to be tongue in
cheek and taken with a grain of salt. ) Sorry to have offended any former
C programmers.

regards,
Lup
"NKOBAYE027" <> wrote in message
news:5tqRb.21417$...
> return a reference to a std::vector<char> instead - in C++, unless you
> absolutely have no choice, you should never use arrays. arrays are evil -
> use standard containers instead.
>
> "Ger" <> wrote in message
> news:bv3hfo$3se$...
> > Hi,
> >
> > I have been trying to return a data packet (as a char array) at the end
of
> a
> > function but that seems to be impossible...
> > I would like to use this char array in a different class..anyone some
> ideas?
> >
> > greetz,
> > Ger
> >
> >
> >
> >
>
>

Jeff Schwab wrote:
>> Actually the "avoid arrays" is a good advice for beginners. You can
>> do everything with a vector, and more safely. Of course this is no
>> rule for production code.
>
> You're right: Vectors are, indeed, more appropriate as a learning tool
> (and for most production purposes).

Well, in production they might happily hide errors as well (if you really do
have a small upper limit). But I would say that one is still better off
using the boost array wrapper than "naked" arrays.
> I'm saddened by the "arrays are the devil" nonsense, though.

It is "the teaching". It is easier to scare people away from something (cf.
religions) than teach them to be responsible. ;-)
>> It's fun! (My program works in debug mode, but not in release.
>> Please help!
>
> Livin' on the edge, baby! "I like bungee jumping and volcano luging,
> and in my spare time I dereference null..."

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!