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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:36:17+00:00 2026-06-13T12:36:17+00:00

I have a structure that contains a pointer to a fixed sized memory. Say,

  • 0

I have a structure that contains a pointer to a fixed sized memory.

Say,

// Structure of a page
struct Page {
public:
    // Number of slots
    unsigned short numSlots;
    void *data = malloc(PF_PAGE_SIZE);
};

I want to put this declaration in a header file. Should I also explicitly put the malloc part in it or it should only contain void *data and no details about how much memory the pointer points to?

In short should the declaration look like above or resemble the following:

// Structure of a page
struct Page {
public:
    // Number of slots
    unsigned short numSlots;
    void *data;
};
  • 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-13T12:36:18+00:00Added an answer on June 13, 2026 at 12:36 pm

    You cannot use the first syntax, it will give you a compiler error.
    As for the correct approach, the answers differ depending on whether you are using C or C++.

    In C++:

    You should only declare the structure member in the header file.
    You initialize it in the Member Initialization List in C++ source file.

    Header file:

    // Structure of a page
    struct Page {
    public:
        // Number of slots
        unsigned short numSlots;
        void *data;
    };
    

    Source File:

    Page::Page():data(malloc(PF_PAGE_SIZE))
    {}
    

    Notes:

    • It is always better to use a smart pointer rather than a raw pointer.
    • Also, In C++ one would usually use new and not malloc but since your pointer is of the type void, malloc might also be fine depending on the usage.
    • The code above just answers your immediate Q, There are still other important things to be considered.You still need to follow the Rule of Three since you have a pointer member with dynamic memory allocation for your structure.

    In C:

    In C, there are no Member Initialization lists, so you have to initialize the member after you create an object of the structure.

    Header file:

    // Structure of a page
    struct Page {
        // Number of slots
        unsigned short numSlots;
        void *data;
    };
    

    Source File:

    struct Page obj;
    obj.data = malloc(PF_PAGE_SIZE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure that contains a pointer to a byte array. To set
I have a unsigned char pointer which contains a structure.Now I want to do
I have an xml file that contains a structure.. In this structure, I have
I have an XML file format that contains a structure of questions: <question id=q101>
I have a table that contains articles posted by users. The structure of the
I have a dictionary structure that maps an id (integer) into a number (double).
I have a structure in my program that contains a particular array. I want
GIVEN that you have a fixed area of memory already allocated that you would
I have a book structure that contains a book title, url, and student name.
seeking some advice here. I have a structure which contains a pointer to another

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.