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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:21:16+00:00 2026-06-14T03:21:16+00:00

I was recently reading this article on structs and classes in D, and at

  • 0

I was recently reading this article on structs and classes in D, and at one point the author comments that

…this is a perfect candidate for a struct. The reason is that it contains only one member, a pointer to an ALLEGRO_CONFIG. This means I can pass it around by value without care, as it’s only the size of a pointer.

This got me thinking; is that really the case? I can think of a few situations in which believing you’re passing a struct around “for free” could have some hidden gotchas.

Consider the following code:

struct S
{
    int* pointer;
}

void doStuff(S ptrStruct)
{
    // Some code here
}

int n = 123;
auto s = S(&n);
doStuff(s);

When s is passed to doStuff(), is a single pointer (wrapped in a struct) really all that’s being passed to the function? Off the top of my head, it seems that any pointers to member functions would also be passed, as well as the struct’s type information.

This wouldn’t be an issue with classes, of course, since they’re always reference types, but a struct’s pass by value semantics suggests to me that any extra “hidden” data such as described above would be passed to the function along with the struct’s pointer to int. This could lead to a programmer thinking that they’re passing around an (assuming a 64-bit machine) 8-byte pointer, when they’re actually passing around an 8-byte pointer, plus several other 8-byte pointers to functions, plus however many bytes an object’s typeinfo is. The unwary programmer is then allocating far more data on the stack than was intended.

Am I chasing shadows here, or is this a valid concern when passing a struct with a single reference, and thinking that you’re getting a struct that is a pseudo reference type? Is there some mechanism in D that prevents this from being the case?

  • 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-14T03:21:18+00:00Added an answer on June 14, 2026 at 3:21 am

    I think this question can be generalized to wrapping native types. E.g. you could make a SafeInt type which wraps and acts like an int, but throws on any integer overflow conditions.

    There are two issues here:

    1. Compilers may not optimize your code as well as with a native type.

      For example, if you’re wrapping an int, you’ll likely implement overloaded arithmetic operators. A sufficiently-smart compiler will inline those methods, and the resulting code will be no different than that as with an int. In your example, a dumb compiler might be compiling a dereference in some clumsy way (e.g. get the address of the struct’s start, add the offset of the pointer field (which is 0), then dereference that).

      Additionally, when calling a function, the compiler may decide to pass the struct in some other way (due to e.g. poor optimization, or an ABI restriction). This could happen e.g. if the compiler doesn’t pay attention to the struct’s size, and treats all structs in the same way.

    2. struct types in D may indeed have a hidden member, if you declare it in a function.

      For example, the following code works:

      import std.stdio;
      
      void main()
      {
          string str = "I am on the stack of main()";
      
          struct S
          {
              string toString() const { return str; }
          }
      
          S s;
          writeln(s);
      }
      

      It works because S saves a hidden pointer to main()’s stack frame. You can force a struct to not have any hidden pointers by prefixing static to the declaration (e.g. static struct S).

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

Sidebar

Related Questions

I was recently reading this discussion at SO where somebody commented that not all
I was recently reading this article by Dave Detlefs in which he presents a
I've recently started reading up on OOP javascript and one thing that authors seem
I was recently reading this document which lists a number of strategies that could
I was reading a forum recently, and saw this comment: So, you see you've
I was recently reading an article on the Windows Metafile vulnerability (http://en.wikipedia.org/wiki/Windows_Metafile_vulnerability#Third-party_patch) and I
I was recently reading an article in the bitsquid blog about how to manage
I recently ran across this great article by Chad Parry entitled DIY-DI or Do-It-Yourself
I'm reading an article about the recently released Gizzard sharding framework by twitter (
When reading about JQuery best practices, I read this recently: Never include Javascript events

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.