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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:09:11+00:00 2026-06-12T06:09:11+00:00

I had a short interview where a question is like this: set an integer

  • 0

I had a short interview where a question is like this: set an integer value to be 0xaa55 at address 0x*****9.

The only thing I noticed is that the address given is not aligned on word boundary. So setting an int *p to the address should not work. Then is it just using a unsigned char *p to assign the value byte-wise? Is it the point of this interview question? There is no point of doing this in real life, is there?

  • 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-12T06:09:12+00:00Added an answer on June 12, 2026 at 6:09 am

    You need to get back to the interviewer with a number of subsidiary questions:

    1. What is the size in bytes of an int?
    2. Is the machine little-endian or big-endian?
    3. Does the machine handle non-aligned access automatically?
    4. What is the performance penalty for handling non-aligned access automatically?
    5. What is the point of this?

    The chances are that someone is thinking of marshalling data the quick and dirty way.

    You’re right that one basic process is to write the bytes via a char * or unsigned char * that is initialized to the relevant address. The answers to my subsidiary questions 1 and 2 determine the exact mechanism to use, but for a 2-byte int in little-endian format, you might use:

    unsigned char *p = 0x*****9; // Copied from question!
    unsigned int v = 0xAA55;
    
    *p++ = v & 0xFF;
    v >>= 8;
    *p   = v & 0xFF;
    

    You can generalize to 4-byte or 8-byte integers easily; handling big-endian integers is a bit more fiddly.


    I assembled some timing code to see what the relative costs were. Tested on a MacBook Pro (2.3 GHz Intel Core i7, 16 GiB 1333 MHz DDR3 RAM, Mac OS X 10.7.5, home-built GCC 4.7.1), I got the following times for the non-optimized code:

    Aligned:          0.238420
    Marshalled:       0.931727
    Unaligned:        0.243081
    Memcopy:          1.047383
    Aligned:          0.239070
    Marshalled:       0.931718
    Unaligned:        0.242505
    Memcopy:          1.060336
    Aligned:          0.239915
    Marshalled:       0.934913
    Unaligned:        0.242374
    Memcopy:          1.049218
    

    When compiled with optimization, I got segmentation faults, even without -DUSE_UNALIGNED — which puzzles me a bit. Debugging was not easy; there seemed to be a lot of aggressive inline optimization which meant that variables could not be printed by the debugger.

    The code is below. The Clock type and the time.h header (and timer.c source) are not shown, but can be provided on request (see my profile). They provide high resolution timing across most platforms (Windows is shakiest).

    #include <string.h>
    #include <stdio.h>
    #include "timer.h"
    
    static int array[100000];
    enum { ARRAY_SIZE = sizeof(array) / sizeof(array[0]) };
    static int repcount = 1000;
    
    static void uac_aligned(int value)
    {
        int *base = array;
        for (int i = 0; i < repcount; i++)
        {
            for (int j = 0; j < ARRAY_SIZE - 2; j++)
                base[j] = value;
        }
    }
    
    static void uac_marshalled(int value)
    {
        for (int i = 0; i < repcount; i++)
        {
            char *base = (char *)array + 1;
            for (int j = 0; j < ARRAY_SIZE - 2; j++)
            {
                *base++ = value & 0xFF;
                value >>= 8;
                *base++ = value & 0xFF;
                value >>= 8;
                *base++ = value & 0xFF;
                value >>= 8;
                *base   = value & 0xFF;
                value >>= 8;
            }
        }
    }
    
    #ifdef USE_UNALIGNED
    static void uac_unaligned(int value)
    {
        int *base = (int *)((char *)array + 1);
        for (int i = 0; i < repcount; i++)
        {
            for (int j = 0; j < ARRAY_SIZE - 2; j++)
                base[j] = value;
        }
    }
    #endif /* USE_UNALIGNED */
    
    static void uac_memcpy(int value)
    {
        for (int i = 0; i < repcount; i++)
        {
            char *base = (char *)array + 1;
            for (int j = 0; j < ARRAY_SIZE - 2; j++)
            {
                memcpy(base, &value, sizeof(int));
                base += sizeof(int);
            }
        }
    }
    
    static void time_it(int value, const char *tag, void (*function)(int value))
    {
        Clock c;
        char buffer[32];
        clk_init(&c);
        clk_start(&c);
        (*function)(value);
        clk_stop(&c);
        printf("%-12s  %12s\n", tag, clk_elapsed_us(&c, buffer, sizeof(buffer)));
    }
    
    int main(void)
    {
        int value = 0xAA55;
    
        for (int i = 0; i < 3; i++)
        {
            time_it(value, "Aligned:",   uac_aligned);
            time_it(value, "Marshalled:", uac_marshalled);
    #ifdef USE_UNALIGNED
            time_it(value, "Unaligned:", uac_unaligned);
    #endif /* USE_UNALIGNED */
            time_it(value, "Memcopy:",   uac_memcpy);
        }
        return(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this question isn't clear. The short story is that we had someone
This is one of an interview question which I had recently. I would like
I had an interview question asking this: text file has following lines> 1: A
I had an interview today which involved this very question and in order to
Edit: The short answer to my question is that I had a mistaken view
Short Question Has anyone had an issue when right clicking files (.c/.h/etc..) and the
So I had to create this splash page in a very short amount of
Had the following as an interview question a while ago and choked so bad
Usually <legend> text is pretty short so I had no idea this was a
I'll keep it short and simple. I had a OCZ Vertex 100Gb SSD that

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.