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

  • SEARCH
  • Home
  • 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 8988977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:08:24+00:00 2026-06-15T22:08:24+00:00

In my project we have a piece of code like this: // raw data

  • 0

In my project we have a piece of code like this:

// raw data consists of 4 ints
unsigned char data[16];
int i1, i2, i3, i4;
i1 = *((int*)data);
i2 = *((int*)(data + 4));
i3 = *((int*)(data + 8));
i4 = *((int*)(data + 12));

I talked to my tech lead that this code may not be portable since it’s trying to cast a unsigned char* to a int* which usually has a more strict alignment requirement. But tech lead says that’s all right, most compilers remains the same pointer value after casting, and I can just write the code like this.

To be frank, I’m not really convinced. After researching, I find some people against use of pointer castings like above, e.g., here and here.

So here are my questions:

  1. Is it REALLY safe to dereference the pointer after casting in a real project?
  2. Is there any difference between C-style casting and reinterpret_cast?
  3. Is there any difference between C and C++?
  • 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-06-15T22:08:25+00:00Added an answer on June 15, 2026 at 10:08 pm

    1. Is it REALLY safe to dereference the pointer after casting in a real project?

    If the pointer happens to not be aligned properly it really can cause problems. I’ve personally seen and fixed bus errors in real, production code caused by casting a char* to a more strictly aligned type. Even if you don’t get an obvious error you can have less obvious issues like slower performance. Strictly following the standard to avoid UB is a good idea even if you don’t immediately see any problems. (And one rule the code is breaking is the strict aliasing rule, § 3.10/10*)

    A better alternative is to use std::memcpy() or std::memmove if the buffers overlap (or better yet bit_cast<>())

    unsigned char data[16];
    int i1, i2, i3, i4;
    std::memcpy(&i1, data     , sizeof(int));
    std::memcpy(&i2, data +  4, sizeof(int));
    std::memcpy(&i3, data +  8, sizeof(int));
    std::memcpy(&i4, data + 12, sizeof(int));
    

    Some compilers work harder than others to make sure char arrays are aligned more strictly than necessary because programmers so often get this wrong though.

    #include <cstdint>
    #include <typeinfo>
    #include <iostream>
    
    template<typename T> void check_aligned(void *p) {
        std::cout << p << " is " <<
          (0==(reinterpret_cast<std::intptr_t>(p) % alignof(T))?"":"NOT ") <<
          "aligned for the type " << typeid(T).name() << '\n';
    }
    
    void foo1() {
        char a;
        char b[sizeof (int)];
        check_aligned<int>(b); // unaligned in clang
    }
    
    struct S {
        char a;
        char b[sizeof(int)];
    };
    
    void foo2() {
        S s;
        check_aligned<int>(s.b); // unaligned in clang and msvc
    }
    
    S s;
    
    void foo3() {
        check_aligned<int>(s.b); // unaligned in clang, msvc, and gcc
    }
    
    int main() {
        foo1();
        foo2();
        foo3();
    }
    

    http://ideone.com/FFWCjf

    2. Is there any difference between C-style casting and reinterpret_cast?

    It depends. C-style casts do different things depending on the types involved. C-style casting between pointer types will result in the same thing as a reinterpret_cast; See § 5.4 Explicit type conversion (cast notation) and § 5.2.9-11.

    3. Is there any difference between C and C++?

    There shouldn’t be as long as you’re dealing with types that are legal in C.


    * Another issue is that C++ does not specify the result of casting from one pointer type to a type with stricter alignment requirements. This is to support platforms where unaligned pointers cannot even be represented. However typical platforms today can represent unaligned pointers and compilers specify the results of such a cast to be what you would expect. As such, this issue is secondary to the aliasing violation. See [expr.reinterpret.cast]/7.

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

Sidebar

Related Questions

I have problem running and debugging this piece of code: bool readSectionHeaders(char* path, int
I have pieces of code like this in a project and I realize it's
I have started to work on maven project recently. The piece of code I
Hey, I've got this nice little piece of code, much like all the other
For my project i've been working on. I have a piece of Javascript code
This piece of code was split off from a project I am working on.
I have a piece of code from an old project. The logic (in a
I have a piece of code in my iOS project that swaps the texture
I have a C++ project that have like 15+ external libraries installed with a
I have a project where I am required to fix this program that has

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.