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 8987305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:45:28+00:00 2026-06-15T21:45:28+00:00

I have the following struct: struct foo { int a; char b; }; It

  • 0

I have the following struct:

struct foo
{
   int  a;
   char b;
};

It is stored to memory and pointer to it can be unaligned (odd address).

So, is this safe?:

const struct foo a = *((struct foo*)char_ptr); 

I am worrying, because the integer member of the source struct can be in odd address. In some systems reading of (multi byte) integer from odd adderess causes problems.

EDIT:
For avoiding off-topic commenting about usage of const, I did remove a const from the code. (I never cast const pointer to non-const pointer, even in this case where it should not cause any problems)

And more about context of problem:
This kind of structure is part of protocol frame. And it can be in any offset inside the frame. In the real code the struct have __attribute__((packed)) attribute. But that probably not change the answer?

Anyway, I could use memcopy and non-const a for solving the problem. But I would like to use the assingment, because it seems to be more elegant way (if it is safe).

  • 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-15T21:45:29+00:00Added an answer on June 15, 2026 at 9:45 pm

    No, it is not safe, unless you know the pointer has the alignment required for the structure.

    Here are some ways to know the pointer has the necessary alignment:

    • The pointer was originally a pointer to this structure type before it was converted to a pointer to char.
    • The pointer is a result of a memory allocation routine (such as malloc) that guarantees the address returned is suitably allocated for any use (which malloc does guarantee).
    • The pointer has been calculated from one of the above in a way that preserves the desired alignment.
    • You convert the pointer to a uintptr_t and test its alignment, provided this is supported by your C implementation.
    • Your C implementation (particularly the OS and processor) permits unaligned access to objects of the type in the structure.

    If you do not know the pointer has the necessary alignment, then you should not access the structure through a converted pointer. Instead, you can define a new structure and use memcpy to copy from the pointer-to-char into the new structure.

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

Sidebar

Related Questions

I have the following class definitions in c++: struct Foo { int x; char
I have the following code: #include <boost/shared_ptr.hpp> struct Foo { int a; }; static
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
Let's say I have the following object: struct Foo { int size() { return
Suppose I have such a struct: struct Foo { const int bar; const char
So I have following struct public struct Foo { public readonly int FirstLevel; public
In my code, I have the following struct : struct foo { int a;
I have program, that has structure defined like this: struct foo { int magic;
I have the following code: class Foo<T> where T : struct { private T
I have the following struct private struct sData{ public int volume; public System.Timers.Timer aliveTimer;

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.