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
  • 1 View
  • 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

Related Questions

I have created a simple dynamic array in C. typedef struct varray_t { void
I have dynamic array in struct and a method that uses the dynamic array.
I am writing a dynamic array in C. typedef struct __c_array { void**_elem; int
I have a struct with many char array like this (and it works) :
I have the following struct: typedef struct _chess { int **array; int size; struct
Ok so I have struct like this typedef struct { float x; float y;
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
I have a struct, MyStruct , that has a private member private bool[] boolArray;
I have a struct PACKET which contains two int fields. How would I declare
struct Test { int var; char *arr; } int main() { Test a; a.arr

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.