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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:08:43+00:00 2026-05-17T23:08:43+00:00

If I have a typedef of a struct typedef struct { char SmType; char

  • 0

If I have a typedef of a struct

typedef struct 
{
char    SmType;
char    SRes;
float   SParm;
float   EParm;
WORD    Count;
char    Flags;
char    unused;
GPOINT2 Nodes[];
} GPATH2;

and it contains an uninitialized array, how can I create an instance of this type so that is will hold, say, 4 values in Nodes[]?

Edit: This belongs to an API for a program written in Assembler. I guess as long as the underlying data in memory is the same, an answer changing the struct definition would work, but not if the underlying memory is different. The Assembly Language application is not using this definition …. but …. a C program using it can create GPATH2 elements that the Assembly Language application can “read”.

Can I ever resize Nodes[] once I have created an instance of GPATH2?

Note: I would have placed this with a straight C tag, but there is only a C++ tag.

  • 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-17T23:08:43+00:00Added an answer on May 17, 2026 at 11:08 pm

    You could use a bastard mix of C and C++ if you really want to:

    #include <new>
    #include <cstdlib>
    
    #include "definition_of_GPATH2.h"
    
    using namespace std;
    
    int main(void)
    {
        int i;
        /* Allocate raw memory buffer */
        void * raw_buffer = calloc(1, sizeof(GPATH2) + 4 * sizeof(GPOINT2));
        /* Initialize struct with placement-new */
        GPATH2 * path = new (raw_buffer) GPATH2;
    
        path->Count = 4;
        for ( i = 0 ; i < 4 ; i++ )
        {
            path->Nodes[i].x = rand();
            path->Nodes[i].y = rand();
        }
    
        /* Resize raw buffer */
        raw_buffer = realloc(raw_buffer, sizeof(GPATH2) + 8 * sizeof(GPOINT2));
    
        /* 'path' still points to the old buffer that might have been free'd 
         * by realloc, so it has to be re-initialized
         * realloc copies old memory contents, so I am not certain this would
         * work with a proper object that actaully does something in the 
         * constructor
         */
        path = new (raw_buffer) GPATH2;
    
        /* now we can write more elements of array */
        path->Count = 5;
        path->Nodes[4].x = rand();
        path->Nodes[4].y = rand();
    
        /* Because this is allocated with malloc/realloc, free it with free
         * rather than delete.
         * If 'path' was a proper object rather than a struct, you should
         * call the destructor manually first.
         */
        free(raw_buffer);
    
        return 0;
    }
    

    Granted, it’s not idiomatic C++ as others have observed, but if the struct is part of legacy code it might be the most straightforward option.

    Correctness of the above sample program has only been checked with valgrind using dummy definitions of the structs, your mileage may vary.

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

Sidebar

Related Questions

I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following construction: typedef struct bucket { char *key; ENTRY *data; struct
I have seen many programs consisting of structures like the one below typedef struct
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a
I have my code organized as following class MyClass { public: typedef void (MyClass::Ptr2func)();
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I

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.