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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:38:30+00:00 2026-05-26T19:38:30+00:00

struct Test { int var; char *arr; } int main() { Test a; a.arr

  • 0
struct Test
{
    int var;
    char *arr;
}

int main()
{
    Test a;
    a.arr = new char[50];
}

The above code would create a dynamic array in the structure but the dynamic array would not be actually memory allocated within the structure, its memory would be allocated somewhere else. I want this array to be allocated in the structure as with the fixed array but I don’t want to use fixed array. Any ideas?
I’ve tried my best to clarify my question, hope you understand.

I want to send this structure through UDP and UDP takes continues memory buffer to send that’s why I want this structure to have continuous memory.

  • 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-26T19:38:31+00:00Added an answer on May 26, 2026 at 7:38 pm

    You can not do that as the new memory is from heap/ free store and your a will be allocated on stack….

    you can allocate using malloc/new a continous memory block of sizeof Test + your required size and make the pointer arr to point at the end of the Test structure.

    If you need it in function scope on stack you can use alloca.

    Test *a = (Test*)alloca(sizeof(Test)+yoursize);
    a->arr = (char*)a+sizeof(Test)...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

struct { char a; int b; } x; Why would one define a struct
I am curious why this code works: typedef struct test_struct { int id; }
struct elem { int i; char k; }; elem user; // compile error! struct
For the following code: #include<iostream> #include<vector> #include<string> using namespace std; struct Test { string
Here TEST is a struct pointer. Structure contains integer. What can be the right
The code below is passing the struct variable: struct someStruct { unsigned int total;
constexpr int get () { return 5; } template<int N> struct Test {}; int
I have a testing struct definition as follows: struct test{ int a, b, c;
is it possible to do something like the following: struct test { this {
Is it possible to add a list to a struct? public struct test {

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.