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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:27:48+00:00 2026-05-12T09:27:48+00:00

I’m writing an embedded control system in C that consists of multiple tasks that

  • 0

I’m writing an embedded control system in C that consists of multiple tasks that send messages to each other (a fairly common idiom, I believe!), but I’m having a hard time designing a mechanism which:

  • is neat
  • is generic
  • is relatively efficient
  • most importantly: is platform independent (specifically, doesn’t violate strict-aliasing or alignment issues)

Conceptually, I’d like to represent each message type as a separate struct definition, and I’d like a system with the following functions (simplified):

void sendMsg(queue_t *pQueue, void *pMsg, size_t size);
void *dequeueMsg(queue_t *pQueue);

where a queue_t comprises a linked list of nodes, each with a char buf[MAX_SIZE] field. The system I’m on doesn’t have a malloc() implementation, so there’ll need to be a global pool of free nodes, and then one of the following (perceived issues in bold):

  1. sendMsg() does a memcpy of the incoming message into the buffer of a free node.
    My understanding is that this will have alignment issues unless the caller of dequeueMsg() does a further memcpy on the return value.
  2. or there’ll be a void *getFreeBuffer() function which returns the buf[] of the next free node, which the caller (the sender) will cast to the appropriate pointer-to-type.
    My understanding is that this will now have alignment issues on the way in, and still requires a memcpy after dequeueMsg() to avoid alignment issues on the way out.
  3. or redefine the buffer in queue_t nodes as (e.g.) uint32_t buf[MAX_SIZE].
    My understanding is that this violates strict aliasing, and isn’t platform-independent.

The only other option I can see is to create a union of all the message types along with char buf[MAX_SIZE], but I don’t count this as “neat”!

So my question is, how does one do this properly?

  • 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-12T09:27:48+00:00Added an answer on May 12, 2026 at 9:27 am

    The way we deal with this is to have our free list consist entirely of aligned nodes. In fact we have multiple free lists for different sizes of node, so we have lists that are aligned on 2 byte, 4 byte, and 16 byte boundaries (our platform doesn’t care about alignment larger than one SIMD vector) . Any allocation gets rounded up to one of those values and put in a properly aligned node. So, sendMsg always copies its data into an aligned node. Since you’re writing the free list yourself, you can easily enforce alignment.

    We would also use a #pragma or declspec to force that char buf[MAX_SIZE] array to be aligned to at least a word boundary inside the queue_t node struct.

    This assumes of course that the input data is aligned, but if for some reason you’re passing in a message that expects to be (let’s say) 3 bytes off from alignment, you can always detect that with a modulus and return an offset into the free node.

    With that underlying design we have interfaces that support both option 1 and 2 above. Again, we precondition that input data is always natively aligned, so our dequeue of course returns an aligned pointer; but if you need oddly aligned data in, again, just offset into the free node and return the offset pointer.

    This keeps you dealing in void *s thus avoiding your strict aliasing problems. (In general though I think you may need to relax your strict aliasing requirements when writing your own memory allocators, since by nature they blur types internally.)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests

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.