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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:08:13+00:00 2026-05-11T05:08:13+00:00

I have the STRUCT1 Structure declared as below typedef struct struct1 { short int

  • 0

I have the STRUCT1 Structure declared as below

typedef struct struct1 {     short int nbr_fe;     [size_is(nbr_fe)] STRUCT2  ptr_fe[*]; } STRUCT1; 

STRUCT2 is also another structure inside STRUCT1

and then I have a pointer declared to it as below

typedef [ptr] STRUCT1 * ptr; 

And I have to allocate a memory to an array of STRUCT1 base on the nbrRequested And so far I have

STRUCT1 obj1; memset((void*)&obj1, '\0' , sizeof(STRUCT1));  for(int i1=0;i1<(int)nbrRequested;i1++) {    STRUCT2 obj2;    memset((void*)&obj2, '\0' , sizeof(STRUCT2));    obj1.ptr_fe[i1] = obj2; }  ptr ptr2; ptr2 = &obj1; 

but if the nbrRequested is greater than 500, the loop goes in infinite and the application hangs.

Is there any better way to allocate a memory without using for loop

  • 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. 2026-05-11T05:08:14+00:00Added an answer on May 11, 2026 at 5:08 am

    You’re not allocating anything, you’re overwriting the same data on the stack (which then goes out of scope when the loop exits.

    In C++, you allocate memory with the new operator. In C you’d use malloc. So a minimal rewrite of your code would be something like this (in C, since that seems to be what you’re writing)

    // Allocate enough space for the array of `WF_LIST_WORKUNIT_P_FE`s WF_LIST_WORKUNIT_P_FE listWU = malloc(sizeof(WF_STRUCT_WORKUNIT_FE) * nbrRequested); memset(listWU, 0, sizeof(WF_STRUCT_WORKUNIT_FE) * nbrRequested)); 

    Of course, this just sets every struct in the array to 0, rather than a more meaningful initialization, if that is what you want.

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

Sidebar

Related Questions

I have a structure typedef struct store { char name[11]; int age; } store;
I have declared a structure that look like typedef struct { char* key; char*
I have a structure: struct mystruct { int* pointer; }; structure mystruct* struct_inst; Now
How can we access variables of a structure? I have a struct: typedef struct
I have a structure: typedef struct stock { const char* key1p2; // stock code
I have a structure I would like to optimize the footprint of. typedef struct
I have a structure like struct category { int id,newID; std::string name; }; and
I have a data structure like this: struct foo { int id; int route;
In rpc.h, the GUID structure is declared as follows: typedef struct _GUID { DWORD
I have structure declaration in a header globalStruct.h #ifndef _GLOBALSTRUCT_H #define _GLOBALSTRUCT_H typedef struct

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.