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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:35:37+00:00 2026-06-15T07:35:37+00:00

int i = 42; int *p1 = &i; int long *p2 = (long*)p1; Is

  • 0
int i = 42;
int *p1 = &i;
int long *p2 = (long*)p1;

Is this undefined behavior? In C++, I think it is implementation defined behavior for some reason.

I looked in C Standard:

C99 6.3.2.3/7 A pointer to an object or incomplete type may be
converted to a pointer to a different object or incomplete type. If
the resulting pointer is not correctly aligned 57) for the pointed-to
type, the behavior is undefined. Otherwise, when converted back again,
the result shall compare equal to the original pointer.

57) In general, the concept “correctly aligned” is transitive: if a
pointer to type A is correctly aligned for a pointer to type B, which
in turn is correctly aligned for a pointer to type C, then a pointer
to type A is correctly aligned for a pointer to type C.

What does the term correctly aligned mean here in practice? How do you know if you’re doing it correctly without stepping into Undefined behavior?

  • 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-15T07:35:39+00:00Added an answer on June 15, 2026 at 7:35 am

    It basically means that if, say, int is aligned to 4 bytes, and int long is aligned to 8 bytes, the behavior is undefined. Say you have something like:

     0x04     0x08    0x0C    0x10
    +------+-------+-------+-------+
    |      |       |   i   |       |
    +------+-------+-------+-------+
    

    In this case, &i == 0x0C (which is valid because int is aligned to 4 bytes). When you cast to int long*, the pointer is converted to an aligned one: p2 == 0x08, because our theoretical system aligns int long to 8 bytes, so you’d be basically reading off an address you don’t own if you dereference p1, ergo the undefined behavior.

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

Sidebar

Related Questions

I tried this: request.DeviceID = (long long int *)&(DeviceID); request.ProdReqID = (long long int
for some reason this code throws an error // map<int, AnItem> roomlist; // map<string,
I have been stuck on this for a long time and for some reason
int a = 10; int* pA = &a; long long b = 200; long
#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf(%ld,&n); n=100000;
In a C++ function like this: int& getNumber(); what does the & mean? Is
Does this code violate strict aliasing? struct {int x;} a; *(int*)&a = 3 More
This is unfortunately defined in some external library: cannot touch! // library.h typedef struct
void DoWork(int n); void DoWork(const int &n); What's the difference?
I have a function with the following declaration: void cleanValid(int valid[][4], int &size, int

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.