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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:29:05+00:00 2026-05-15T08:29:05+00:00

Aren’t misaligned pointers (in the BEST possible case) supposed to slow down performance and

  • 0

Aren’t misaligned pointers (in the BEST possible case) supposed to slow down performance and in the worst case crash your program (assuming the compiler was nice enough to compile your invalid c program).

Well, the following code doesn’t seem to have any performance differences between the aligned and misaligned versions. Why is that?

/* brutality.c */

#ifdef BRUTALITY
    xs = (unsigned long *) ((unsigned char *) xs + 1);
#endif

…

/* main.c */

#include <stdio.h>
#include <stdlib.h>

#define size_t_max ((size_t)-1)
#define max_count(var) (size_t_max / (sizeof var))

int main(int argc, char *argv[]) {

    unsigned long sum, *xs, *itr, *xs_end;
    size_t element_count = max_count(*xs) >> 4;

    xs = malloc(element_count * (sizeof *xs));
    if(!xs) exit(1);

    xs_end = xs + element_count - 1; sum = 0;

    for(itr = xs; itr < xs_end; itr++)
        *itr = 0;

#include "brutality.c"

    itr = xs;
    while(itr < xs_end)
        sum += *itr++;

    printf("%lu\n", sum);

    /* we could free the malloc-ed memory here */
    /* but we are almost done                  */
    exit(0);
}

Compiled and tested on two separate machines using

gcc -pedantic -Wall -O0 -std=c99 main.c
for i in {0..9}; do time ./a.out; done
  • 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-15T08:29:05+00:00Added an answer on May 15, 2026 at 8:29 am

    I tested this some time in the past on Win32 machines and did not notice much of a penalty on 32-bit machines. On 64-bit, though, it was significantly slower. For example, I ran the following bit of code. On a 32-bit machine, the times printed were hardly changed. But on a 64-bit machine, the times for the misaligned accesses were nearly twice as long. The times follow the code.

    #define UINT unsigned __int64
    #define ENDPART QuadPart
    #else
    #define UINT unsigned int
    #define ENDPART LowPart
    #endif
    
    
    int main(int argc, char *argv[])
    {
       LARGE_INTEGER startCount, endCount, freq;
       int i;
       int offset;
       int iters = atoi(argv[1]);
       char *p = (char*)malloc(16);
       double *d;
    
       for ( offset = 0; offset < 9; offset++ )
          {
          d = (double*)( p + offset );
          printf( "Address alignment = %u\n", (unsigned int)d % 8 );
          *d = 0;
          QueryPerformanceFrequency(&freq);
          QueryPerformanceCounter(&startCount);
          for(i = 0; i < iters; ++i)
             *d = *d + 1.234;
          QueryPerformanceCounter(&endCount);
    
          printf( "Time:  %lf\n",
                 (double)(endCount.ENDPART-startCount.ENDPART)/freq.ENDPART );
          }
    }
    

    Here are the results on a 64-bit machine. I compiled the code as a 32-bit application.

    [P:\t]pointeralignment.exe 100000000
    Address alignment = 0
    Time:  0.484156
    Address alignment = 1
    Time:  0.861444
    Address alignment = 2
    Time:  0.859656
    Address alignment = 3
    Time:  0.861639
    Address alignment = 4
    Time:  0.860234
    Address alignment = 5
    Time:  0.861539
    Address alignment = 6
    Time:  0.860555
    Address alignment = 7
    Time:  0.859800
    Address alignment = 0
    Time:  0.484898
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 417k
  • Answers 417k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are taking a copy of the QString in your… May 15, 2026 at 9:44 am
  • Editorial Team
    Editorial Team added an answer if (!extension_loaded('php_gd2')) { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {… May 15, 2026 at 9:44 am
  • Editorial Team
    Editorial Team added an answer double? is just shorthand for Nullable<double>; basically, a double that… May 15, 2026 at 9:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.