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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:03:48+00:00 2026-06-01T23:03:48+00:00

I have a struct of the form: typedef struct node { unsigned int *

  • 0

I have a struct of the form:

typedef struct node {
  unsigned int * keys;
  unsigned int * branches;
} NODE;

The number of keys and branches is determined at runtime, but is known. It is derived from another struct:

typedef struct tree {
  unsigned int num_keys_per_node;
} TREE;

In order to allocate a NODE for this TREE, the manual steps would be:

NODE node;
unsigned int keys[tree->num_keys_per_node];
unsigned int branches[tree->num_keys_per_node + 1];

node.keys     = keys;
node.branches = branches;

I need to allocate a lot of these nodes inside tight loops, only temporarily as I traverse a data structure, discarding them quickly as the node traversal continues. I could write a function that returns a pointer and malloc() the keys and branches on the heap and free() them manually, but I’d prefer to use the stack if possible.

Since this initialization logic is going to be repeated in a number of places, how can I define a macro, so that I can effectively do something like:

NODE node = CREATE_NODE_FOR_TREE(tree);

I’m having difficultly seeing a way to do this which will result in the preprocessor giving a valid syntax.

Happy to hear other approaches to dynamic struct allocation on stack memory too.

EDIT | I should never need more than one node in memory at the same time, so I can re-use the one struct repeatedly too.

  • 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-01T23:03:49+00:00Added an answer on June 1, 2026 at 11:03 pm

    Try to pass node as argument to the macro like so:

    #define CREATE_NODE_FOR_TREE( \
      node, \
      tree) \
      \
      unsigned int keys[tree->num_keys_per_node]; \
      unsigned int branches[tree->num_keys_per_node + 1]; \
      \
      node.keys     = keys; \
      node.branches = branches; 
    
    ...
    NODE node = {0};
    CREATE_NODE_FOR_TREE(node, tree);
    ...
    

    This solution assumes at least c99.

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

Sidebar

Related Questions

I have a structured defined as typedef struct{ char string1 char string2 int number1
I have two window form applications written in C, one holds a struct consisting
Ok so I have struct like this typedef struct { float x; float y;
using C++, I have struct Base { virtual void stuff(/*base stuff*/); }; struct Derived
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
I have a struct struct Packet { int senderId; int sequenceNumber; char data[MaxDataSize]; char*
I have 2 matrix structs means equal data but have different form like these:
I have this struct: Example.Form = Ext.extend(Ext.form.FormPanel, { // other element , onSuccess:function(form, action)
I have a templated struct of the following form: struct Command(T) { alias T
I have a C structure. struct data{ double value1[50]; double value2[50]; int count; };

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.