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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:04:40+00:00 2026-05-14T03:04:40+00:00

I have a struct with a dynamic array inside of it: struct mystruct {

  • 0

I have a struct with a dynamic array inside of it:

struct mystruct {
 int count;
 int *arr;
} mystruct_t;

and I want to pass this struct down a pipe in C and around a ring of processes. When I alter the value of count in each process, it is changed correctly. My problem is with the dynamic array.

I am allocating the array as such:

mystruct_t x;
x.arr = malloc( howManyItemsDoINeedToStore * sizeof( int ) );

Each process should read from the pipe, do something to that array, and then write it to another pipe. The ring is set up correctly; there’s no problem there. My problem is that all of the processes, except the first one, are not getting a correct copy of the array. I initialize all of the values to, say, 10 in the first process; however, they all show up as 0 in the subsequent ones.

for( j = 0; j < howManyItemsDoINeedToStore; j++ ){
    x.arr[j] = 10;
}

Log:

Initally:       10      10      10      10      10
After Proc 1:   9       10      10      10      15
After Proc 2:   0       0       0       0       0
After Proc 3:   0       0       0       0       0
After Proc 4:   0       0       0       0       0
After Proc 5:   0       0       0       0       0
After Proc 1:   9       10      10      10      15
After Proc 2:   0       0       0       0       0
After Proc 3:   0       0       0       0       0
After Proc 4:   0       0       0       0       0
After Proc 5:   0       0       0       0       0

Now, if I alter my code to, say,

struct mystruct {
 int count;
 int arr[10];
} mystruct_t;

everything is passed correctly down the pipe, no problem. I am using read and write in C:

write( STDOUT_FILENO, &x, sizeof( mystruct_t ) );
read( STDIN_FILENO, &x, sizeof( mystruct_t ) );
  • 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-14T03:04:40+00:00Added an answer on May 14, 2026 at 3:04 am

    When you alloc your dynamic array malloc function gives back a pointer to a memory space which is not in your struct. Take a look at the example:

    0x0000000F int count
    0x00000014 >> pointer to your array elsewhere 0x000000F0
    
    
    0x000000F0 your array is here
    

    You could pad your struct with well known datas to demonstrate it.

    struct mystruct{
      int count;
      int *arr;
      char pad [5];
    }mystruct_t;
    mystruct_t x;
    x.pad={0x5b,0x5C,0x5D,0x5E,0x5F};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 529k
  • Answers 529k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First off declare a NSMutableArray in youre viewController's .h file.… May 16, 2026 at 11:12 pm
  • Editorial Team
    Editorial Team added an answer Have your DLL export a function returning meta information about… May 16, 2026 at 11:12 pm
  • Editorial Team
    Editorial Team added an answer One approach would be to inject UriInfo: public final Response… May 16, 2026 at 11:12 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a struct of type Duplicate I have a variable of type int
I have created a dynamic typing system in C in order to create a
Is it valid in XML-RPC to have an unbounded array of elements without having
I am currently writing a program to implement an arraylist (or dynamic array) in
I've read about POD objects in C++. I wanna have a POD struct to
I have a struct: struct s { UINT_PTR B_ID; }; s d; d.B_ID=0x1; That
I have a struct I have written which is supposed to represent an entire
How do I allocate memory for a char variable (not a char pointer) inside
I'm currently working on a C++ project, where dynamic arrays often appear. I was
I'm trying to create an array of structs and also a pointer to that

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.