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

  • Home
  • SEARCH
  • 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 4594270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:48:15+00:00 2026-05-21T22:48:15+00:00

I have a struct that I’d like to export to a file, and then

  • 0

I have a struct that I’d like to export to a file, and then mmap() that file. One issue is that the struct has a member variable that is a string, and I’m not sure how mmap would handle that. In this case all of these strings are of identical size, 8 characters. I’m working on Windows, although I’m using an mmap() function I found online which is supposed to replicate the Linux mmap() function.

The struct itself is defined as:

struct testStruct
{
    string testString;
    unsigned int testInt;
    unsigned int tsetArr[9];
};

Is it possible to define the return value of sizeof() for an object?

Would mmapping a file which contains struct data be possible?

What code would I have to use to export the struct to a file, and then mmap 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-21T22:48:16+00:00Added an answer on May 21, 2026 at 10:48 pm

    The representation of std::string is not guaranteed by the C++ standard, so this won’t work. std::string may (and commonly will) allocate its contents anywhere on the heap, so you’ll be storing a pointer and a size member, rather than the string itself.

    A char array with compile-time constant size, such as tsetArr, should work, though.

    Is it possible to define the return value of sizeof() for an object?

    No. sizeof is not a function, so you can’t overload it (and strictly, it has a value, but not a return value since it doesn’t return from anywhere; it’s expanded to a constant by the compiler).

    Would mmapping a file which contains struct data be possible?

    Possible, yes, but I advise against it; your code will not be portable, perhaps not even to different compilers on the same platform, and your struct is cast in stone. If you want to do so anyway, only mmap POD (plain old data) with no pointer members and put an unsigned version member in your struct that you increment every time its definition is changed.

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

Sidebar

Related Questions

I have class with a member function that takes a default argument. struct Class
I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
I have a struct that I initialize like this: typedef struct { word w;
I have a struct that I'd like to output using either 'std::cout' or some
Say I have a struct that looks like this (a POD): struct Foo {
Summary: I have a struct that is read/written to file. This struct changes frequently,
I have a C (not C++) struct that goes like this typedef struct mystruct{
In my C# application, I have a large struct (176 bytes) that is passed
I have a function that takes a struct, and I'm trying to store its
I have this routine that calculates the seconds-to-date for a struct tm . On

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.