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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:22:15+00:00 2026-06-04T14:22:15+00:00

I want to get the size (in bytes) of struct testB here: struct testA

  • 0

I want to get the size (in bytes) of struct testB here:

struct testA
{
    int x;
};
struct testB
{
    std::string guid;
    testA userData;
};

I can get the size like this but it’s stupid:

int testA::size()
{
    return sizeof(testA);
}
int testB::size()
{
    return guid.size()*sizeof(TCHAR) + userData.size();
}

Are there simpler solutions?

Ps: the problem at hand is like this:

I want to story an object to database, so when my application crashes, I can get my data back. So I used sqlite, and save all the members of the struct in seperate rows. Everything goes fine, except that I have a member who hold the binary string of a Protobuf message, which can’t be inserted into the database because it will make the SQL sentence invalid.

So I come to 2 solutions:

1 serialize the protobuf message into a json/XML text, which seems to be tedious to me

2 dump all the content into the db using sqlite3_bind_blob whose third param is the size of the blob data in bytes, so the question is : how to get the size of the struct (which is quite complicated)?

  • 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-04T14:22:16+00:00Added an answer on June 4, 2026 at 2:22 pm
    sizeof (testB) // how many bytes are allocated for testB object itself
    

    is possibly what you want, or

    sizeof (testB) + guid.capacity() // also include heap allocation for string
    

    Without some kind of description of the problem at hand, there’s no way to come to a specific conclusion.

    The most obvious use for this information would be to evaluate program memory usage. This strategy isn’t appropriate for that. Use a profiler or whatever debugging tools are available on your platform to report the number and total size of allocations while running real-world test cases. For example, Valgrind should do this.

    EDIT: You want to pass the size to sqlite_bind_blob, which takes a void * pointer to unformatted memory and a number of bytes to read from that pointer.

    An interface accepting a void * will not be aware of any data structures such as implemented by the STL. The void * needs to come from a flat structure with no pointers and a very stable binary format. The C++ terminology is “standard layout” and the simplest thing is not to use any C++ features at all.

    In this case, you could write something like

    struct testB
    {
        char guid[ 36 ]; // ASCII representation of hex of GUID
        testA userData;
    };
    

    But really the best thing would be to put the GUID in another database column outside this blob. The database might optimize storage of GUIDs, and you’re likely to want to look up records by GUID as well.

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

Sidebar

Related Questions

Let's say that I want to get the size in bytes or in chars
In mssql2005 when I want to get size of table in MBs, I use
I want to get the size of the current terminal, i.e. the terminal my
i want to create functions what get the folder size. I write small function:
I want to know how to get the total size of the modified files
I want get the time used for a case so I can create an
I want to get the top #nr_of_rows - (int)(#nr_of_rows / 10 - 1) of
I want to get the file size of any given file using the C#,
How can I get the length in bytes of a stringstream. stringstream.str().length(); would copy
I want to get the size of any record type in following function. But

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.