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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:59:10+00:00 2026-05-23T09:59:10+00:00

Assume I have a sample source file, test.c, which I am compiling like so:

  • 0

Assume I have a sample source file, test.c, which I am compiling like so:

$ gcc -03 -Wall

test.c looks something like this ..

/// CMP128(x, y)
//
// arguments
//  x - any pointer to an 128-bit int
//  y - any pointer to an 128-bit int
//
// returns -1, 0, or 1 if x is less than, equal to, or greater than y
//
#define CMP128(x, y) // magic goes here

// example usages

uint8_t  A[16];
uint16_t B[8];
uint32_t C[4];
uint64_t D[2];
struct in6_addr E;
uint8_t* F;

// use CMP128 on any combination of pointers to 128-bit ints, i.e.

CMP128(A, B);
CMP128(&C[0], &D[0]);
CMP128(&E, F);

// and so on

let’s also say I accept the restriction that if you pass in two overlapping pointers, you get undefined results.

I’ve tried something like this (imagine these macros are properly formatted with backslash-escaped newlines at the end of each line)

#define CMP128(x, y) ({
  uint64_t* a = (void*)x;
    uint64_t* b = (void*)y;

  // compare a[0] with b[0], a[1] with b[1]
})

but when I dereference a in the macro (a[0] < b[0]) I get “dereferencing breaks strict-aliasing rules” errors from gcc

I had thought that you were supposed to use unions to properly refer to a single place in memory in two different ways, so next I tried something like

#define CMP128(x, y) ({
    union {
        typeof(x) a;
        typeof(y) b;
        uint64_t* c;
    }   d = { .a = (x) }
        , e = { .b = (y) };

    // compare d.c[0] with e.c[0], etc
})

Except that I get the exact same errors from the compiler about strict-aliasing rules.

So: is there some way to do this without breaking strict-aliasing, short of actually COPYING the memory?

(may_alias doesnt count, it just allows you to bypass the strict-aliasing rules)

EDIT: use memcmp to do this. I got caught up on the aliasing rules and didn’t think of it.

  • 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-23T09:59:11+00:00Added an answer on May 23, 2026 at 9:59 am

    The compiler is correct as the aliasing rules are determined by the so-called
    ‘effective type’ of the object (ie memory location) you’re accessing, regardless of any pointer magic. In this case, type-punning the pointers with a union is no different than an explicit cast – using the cast is actually preferable as the standard does not guarantee that arbitary pointer types have compatible representations, ie you’re unnecessarily depending on implementation-defined behaviour.

    If you want to conform to the standard, you need to copy the data to new variables or use a union during the declaration of the original variables.

    If your 128-bit integers are either big-endian or little-endian (ie not mixed-endian), you could also use memcmp() (either directly or after negating the return value) or do a byte-wise comparison yourself: access through pointers of character type is an exception to the aliasing rule.

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

Sidebar

Related Questions

Assume I have a class that looks like this: class Sample { public string
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
Assume you have some objects which have several fields they can be compared by:
Assume that I have a list of 100 products, each of which has a
Firstly, let me set out what I'd like to do. Assume I have three
Once again, I have a problem for which I would like to shave off
Good day! Let us have a source file main.cpp and a CMakeLists.txt file containing
Assume I have a webpage where people submit java source code (a simple class).
Let's assume we have a simple internet socket, and it's going to send 10

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.