Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6473417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:27:50+00:00 2026-05-25T06:27:50+00:00

I have a character array that contains serialized data that I need to interpret

  • 0

I have a character array that contains “serialized” data that I need to interpret as ‘int’s. Previosuly I just cast a pointer to the location to an ‘int*’ and dereferenced that to get at the int data, but although it’s worked well for me it’s breaking the strict alias rules and therefore undefined behavour.

So now I use memcpy to copy the bytes into an int, which I believe is not undefined behavour. However can I use “std::copy”?

For example

char data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;

std::copy(data, data+sizeof(int), reinterpret_cast<char*>(&i));

This in itsself doesn’t break the strict alias rules but any likely implementation will do so… However memcpy has the same issue and that’s “allowed”.

Is this standard compilant code or do I need to stick to using memcpy?

EDIT: I should add that I appeciate the answers on how best to do this, they are interesting, but my question was more about is this legal than how can I do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:27:51+00:00Added an answer on May 25, 2026 at 6:27 am

    This is equivalent to std::memcpy(&i, data, sizeof(int)) and suffers the same problems of relying on endianness and the assumption that sizeof(int) <= sizeof(data), which are platform-dependent. char * is exempt from the strict aliasing rule.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a character array that contains a phone number of the form: (xxx)xxx-xxxx
If I have a byte array that contains UTF8 content, how would I go
I have a rather large list of data that contains 5 properties per element.
I have a UTF-8 formatted data file that contains thousands of floating point numbers.
I have a char array buffer that I am using to store characters that
Is a string actually a character array (is-a), or does it have a character
We have a Silverlight 2 project (game) that will require a lot of character
Scenario: I have a text file that has pipe (as in the | character)
Hello I have a chunk of memory (allocated with malloc()) that contains bits (bit
I have a several DBF files generated by a third party that I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.