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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:15:01+00:00 2026-06-14T04:15:01+00:00

I need to create and implement usage of the struct that contains an array

  • 0

I need to create and implement usage of the struct that contains an array inside it. How to do proper initialization and allocation of the struct and array? Here’s my current implementation that seems to work. However, not sure is it proper way and where is the struct allocated (on stack or on heap memory). IMHO a call to malloc makes to store an array on heap, however not sure about remaining struct members. Here are my structs:

typedef struct {

  uint8_t objectSize;
  double objectDepth; 

} ObjectData;

typedef struct {

  ObjectData *objectList;
  double startMark;
  double endMark;

} MyDataPacket; 

Here is the function that fills and returns the struct:

void getMyPacket(MyDataPacket *myDataPacket, uint8_t objectNum)
{
  myDataPacket->startMark = 10.0; 
  myDataPacket->endMark = 60.0; 
  myDataPacket->objectList = malloc(objectNum * sizeof(MyDataPacket));

  uint8_t x;
  for (x = 0; x < objectNum; x++)
  { 
    myDataPacket->objectList[x].objectSize = x; // just test values
    myDataPacket->objectList[x].objectDepth = x; // just test values
  }
}

Here’s function calling part:

uint8_t objectNum = 10;
MyDataPacket myDataPacket;
getMyPacket(&myDataPacket, objectNum);

Does the struct data is mixed and placed both on stack and heap, or everything resides at one place?

  • 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-14T04:15:02+00:00Added an answer on June 14, 2026 at 4:15 am

    If myDataPacket is allocated on the heap, then it will all be on the heap:

    MyDataPacket *myDataPacket = malloc(sizeof(MyDataPacket));
    

    If myDataPacket is an automatic variable, it will all live on the stack, including the objectList pointer itself, the memory it points to however, will be on the heap:

    MyDataPacket myDataPacket; //this is all on the stack
    myDataPacket->objectList = malloc(...) //memory allocated here is on the heap
    

    If it’s a global variable, it depends on whether or not it’s initialized, and whether it’s initialized to zero or not, but generally speaking uninitialized variables go to the .bss section and initialized variables go to the data section, and of course the dynamically allocated memory will be on the heap as usual.

    If you’re worried about efficiency check this SO question:

    Which is faster: Stack allocation or Heap allocation

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

Sidebar

Related Questions

I need to create an SSIS package that will do the following: Connect to
I need to create a non sequential list of numbers that fit within a
I need to create a class that will present a UIVIew and has a
I need to create a random array of int and have it sorted by
So right now i need to create and implement an extension of the Python
I have some problem to implement twig extension. I need to create my own
I need to create a news ticker that is updated via an RSS feed.
I need to create a custom token that can fetch a value from a
I need to create a portable script to give to others to implement on
I have a need to create a couple of classes that will serve as

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.