Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::array is packed, unpadded and can be casted?

51 views
Skip to first unread message

Frederick Gotham

unread,
Mar 19, 2020, 7:47:14 AM3/19/20
to

In C++11, is std::array packed, unpadded, and castable? Can I do the following?

void Func(array<char,4> &a, array<char,4> &b, array<char,4> &c)
{

}

auto main(void) -> int
{
array<char,12> abc;

Func(
*reinterpret_cast<array<char,4> *>(&abc[0]),
*reinterpret_cast<array<char,4> *>(&abc[4]),
*reinterpret_cast<array<char,4> *>(&abc[8])
);
}


I might go back to normal arrays if there's no nice way of doing this in C++11.

cda...@gmail.com

unread,
Mar 19, 2020, 7:54:46 AM3/19/20
to
Remind me to NEVER grant you the first interview for any kind of programming job at my firm.

Bonita Montero

unread,
Mar 19, 2020, 7:59:29 AM3/19/20
to
Am 19.03.2020 um 12:46 schrieb Frederick Gotham:
> In C++11, is std::array packed, unpadded, and castable? Can I do the following?
> void Func(array<char,4> &a, array<char,4> &b, array<char,4> &c)
> {
>
> }
> auto main(void) -> int
> {
> array<char,12> abc;
>
> Func(
> *reinterpret_cast<array<char,4> *>(&abc[0]),
> *reinterpret_cast<array<char,4> *>(&abc[4]),
> *reinterpret_cast<array<char,4> *>(&abc[8])
> );
> }

Why should someone do this ?
You could stick with pointers, iterators or a span.

Jorgen Grahn

unread,
Mar 19, 2020, 8:07:42 AM3/19/20
to
If you need an object which can be seen as a sequence of 12 chars /or/
as three sequences of four chars, write your own class for it, one
that's tailored to your special problem and your needs.

void Func(Foo& abc);

int main()
{
Foo abc;
Func(abc);
}

Internally, it might be better to use C arrays than to do strange
things to std::arrays, even if it /would/ turn out to be possible.
But in any case, whatever you do would be isolated in that class.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Frederick Gotham

unread,
Mar 19, 2020, 8:13:53 AM3/19/20
to
On Thursday, March 19, 2020 at 11:59:29 AM UTC, Bonita Montero wrote:

> Why should someone do this ?
> You could stick with pointers, iterators or a span.


typedef boost::lockfree::spsc_queue< std::array< std::array<uint8_t,16u>, 4u >, boost::lockfree::capacity<g_buffer_size_for_cryptor_thread> > SPSC_for_CryptorThread;

SPSC_for_CryptorThread g_spsc_t1, g_spsc_t2, g_spsc_t3;

auto main(void) -> int
{
std::array<uint8_t,192u> segment;

...

g_spsc_t1.push( a subset of segment );
}

If you have a better idea, I'm all ears. I think I wanna keep to C++11 though because the more recent standards and going a bit mad.

cda...@gmail.com

unread,
Mar 19, 2020, 8:18:17 AM3/19/20
to
Let me give you some more advice so that you aren't destined to be in IT support until the day that you die. Besides reading the book "Advanced Programming in the Unix Environment" by the late Dr. Stevens, you should also give boost a rest. Kind of like how girls tell you to give your right hand a rest if ya know what I mean Vern.

Frederick Gotham

unread,
Mar 19, 2020, 8:22:11 AM3/19/20
to
On Thursday, March 19, 2020 at 12:18:17 PM UTC, cda...@gmail.com wrote:

> Let me give you some more advice so that you aren't destined to be in IT support until the day that you die.


Do you know what my day job is?

cda...@gmail.com

unread,
Mar 19, 2020, 8:25:02 AM3/19/20
to
Well given the fact that you don't appear to be that well read, and like, pretty some pretty poor C++, I'm suspecting that you aren't a Software Engineer for a place like Google, Facebook, or Uber.

Mr Flibble

unread,
Mar 19, 2020, 3:33:37 PM3/19/20
to
using gtfo = std::array<std::array<char, 4> ,3>;
void Func(gtfo& stopTrolling);

:p

/Flibble

--
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."

Sam

unread,
Mar 19, 2020, 7:26:15 PM3/19/20
to
Frederick Gotham writes:

>
> In C++11, is std::array packed, unpadded, and castable? Can I do the
> following?
>
> void Func(array<char,4> &a, array<char,4> &b, array<char,4> &c)
> {
>
> }
>
> auto main(void) -> int
> {
> array<char,12> abc;
>
> Func(
> *reinterpret_cast<array<char,4> *>(&abc[0]),
> *reinterpret_cast<array<char,4> *>(&abc[4]),
> *reinterpret_cast<array<char,4> *>(&abc[8])
> );
> }

Well, you can do that only in a sense that a C++ compiler might produce some
code that does something. What it actually does, who knows.

So the real answer is: no, you can't.

This might actually produce the expected results – and is likely to do so
with most C++ implementations, but you can't really do that.

> I might go back to normal arrays if there's no nice way of doing this in
> C++11.

You probably should. Containers are not a solution to every problem.

Melzzzzz

unread,
Mar 19, 2020, 11:44:16 PM3/19/20
to
reinterpret cast is bad and ugly, don't use it if not necessary.
Also you have to cast back before use ;)



--
press any key to continue or any other to quit...
U ničemu ja ne uživam kao u svom statusu INVALIDA -- Zli Zec
Svi smo svedoci - oko 3 godine intenzivne propagande je dovoljno da jedan narod poludi -- Zli Zec
Na divljem zapadu i nije bilo tako puno nasilja, upravo zato jer su svi
bili naoruzani. -- Mladen Gogala

Juha Nieminen

unread,
Mar 24, 2020, 6:29:04 AM3/24/20
to
Frederick Gotham <cauldwel...@gmail.com> wrote:
>
> In C++11, is std::array packed, unpadded, and castable? Can I do the following?
>
> void Func(array<char,4> &a, array<char,4> &b, array<char,4> &c)
> {
>
> }
>
> auto main(void) -> int
> {
> array<char,12> abc;
>
> Func(
> *reinterpret_cast<array<char,4> *>(&abc[0]),
> *reinterpret_cast<array<char,4> *>(&abc[4]),
> *reinterpret_cast<array<char,4> *>(&abc[8])
> );
> }

If you absolutely cannot change the signature of Func() above (eg. because
it's not code you are developing nor can affect), and you absolutely must
be able to "split" an existing std::array into several, then it's probably
not very kosher to do that according to the standard (although this is just
my hunch. I haven't read that part of the standard.)

If all that code is developed by you and you have a choice, then you can
simply create your own "array" class that explicitly offers that kind
of functionality. In modern C++ terminology it would be an an "array view".
Which, incidentally, is supported in C++20 directly IIRC, so if you can
afford using C++20 you can use the standard library implementation.
But creating your own should be quite easy.
0 new messages