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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:44:57+00:00 2026-05-30T15:44:57+00:00

In following code sample, how can I make *y to point to offset 4?

  • 0

In following code sample, how can I make *y to point to offset 4?
Right now, only points to offset 0, which is for *x, not *y.

(note that *x & *y will have 4 bytes each)

unsigned char *p = new unsigned char[8];
int *x = &*(int *) p;
*x = 1;
int *y = &*(int *) p;
*y = 2;

Please dont talk about struct or other ways, they won’t help me.

  • 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-30T15:44:58+00:00Added an answer on May 30, 2026 at 3:44 pm

    If you are actually storing multiple types in a contiguous buffer, you might be in for a world of pain when it comes to alignment issues. Not all types can be aligned on the same memory boundaries: some might need to be 32-bit aligned, some 64-bit, some 128-bit, and this is all platform-dependent.

    Some reading to get you started, but you should read a lot more including the alignment of user-defined types, and for the various compilers and operating systems you are targeting if you really want to do this the way you are doing without using a more structured approach (ex: Variant type approaches combining static and dynamic polymorphism).

    In fact, the kind of material you need to understand if you want to do this is quite similar to what you have to learn about data alignment when implementing your own memory allocator (a general memory allocator has to deal with being able to store multiple types in contiguous buffers), so searching for similar topics may give you what you need. I can’t help but say that this low-level problem may give you and any co-workers a lot more grief than anyone should ever deserve.

    You need to know what types need to be aligned to what boundaries (byte, word, doubleword, quadword). It varies as well. You can’t necesarily store an int that isn’t aligned on word boundaries, for example. On some systems this might take a huge performance penalty for unaligned moves or even crash your program. This is also why structs typically have padding inside to make sure all data is aligned correctly.

    However, for your immediate example which isn’t mixing types (just integers):

    unsigned char *p = new unsigned char[sizeof(int) * 2];
    int *x = &*(int *) p;
    *x = 1;
    
    // advance your unsigned char* pointer by the size of x
    p += sizeof x; // or sizeof(int)
    
    int *y = &*(int *) p;
    *y = 2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple 3D cube that I can rotate using the following code:
I have the following code sample that im trying to wrap my head around
I have the following sample code which doesn't seem to want to run. import
How can I make the following code work? I don't think I quite understand
I have the following code sample to illustrate my point. When I load this
The goal of the following code sample is to read the contents of $target
Given the following code sample: uint8_t i, in, ni; i = in = 2;
I have the following sample code in a VB.NET console application. It compiles and
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
I am new to Emacs, and I have the following code as a sample.

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.