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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:07:33+00:00 2026-05-27T10:07:33+00:00

In c, say you have the following structure and an instance of that: #include

  • 0

In c, say you have the following structure and an instance of that:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    typedef struct _a {
        int *a1;
        float *a2;
        char *a3;
    }a;

    a b;
    b.a1 = (int *) malloc(sizeof(int) * 10);
    b.a2 = (float *) malloc(sizeof(float) * 10);
    b.a3 = (char *) malloc(sizeof(char) * 10);

    return 0;
}

Now, how to find the total memory that is allocated/associated with the variable b? sizeof(b) will just return the combined size of the pointers in the structure but will not calculate the sum of the memories that has been allocated using malloc for different types/sizes. How to find the total size of this structure in the memory (including padding if applicable)?

  • 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-27T10:07:33+00:00Added an answer on May 27, 2026 at 10:07 am

    You’ll have to keep track of the aggregate size of your structure yourself.

    The C infrastructure doesn’t know what memory is allocated to which pointers.
    Even creating a strong, generalized concept of “ownership” of memory by a pointer is exceedingly difficult.

    To illustrate, realize that the elements of your structure could contain other pointers that reference other memory. Some of those references could be in loops (circularly linked list, undirected graphs).

    In that case, counting the memory could become impossibly difficult. If two different elements point to the same memory, should it be counted twice, or once? What system would keep track of which memory is counted or not counted? How would that system fit into C’s minimalist paradigm?

    To do what you’re asking, I think you need a language with reflection/introspection, such as Java or .Net (C#).

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

Sidebar

Related Questions

Say we have the following structure: struct Tree { string id; int numof_children; Tree
Say I have the following C structure definition: struct stringStructure { char *stringVariable; };
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
Let's say I have the following structure declaration (simple struct with no constructor). struct
Lets say, I have the following text file: Listing 1: Endianess=little AddressModel=32 typedef struct{
Say I have this following XML structure: <?xml version=1.0 encoding=UTF-8?> <main> <parent> <child1>some value</child1>
Let's say I have the following filesystem structure: /app/ Main application folder /app.js Server
Let's say you have the following structure in C#: struct Piece : IEquatable<Piece> {
Say I have the following structure for 3 different apps that live on 3
Say I have the following structure for a list: <ul> <li></li><li></li> <li></li><li></li> </ul> and

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.