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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:39:33+00:00 2026-06-05T04:39:33+00:00

I am trying to figure out why I am getting an error when I

  • 0

I am trying to figure out why I am getting an error when I assign a value to a structure in an array of structures in heap-allocated memory.

I have the follow C struct:

typedef struct _Point{
int x;
int y;
 }Point;

and an array of Point:

typedef Point Sample[];

Now I want allocate an array of Sample in witch any Sample consists of 15 Points and I using the follow code:

Sample *new_positions;
data.old_positions =(Sample*) malloc(sizeof(Point) * 15 * global.nsamples);

When I try to use it with the follow code in order to assign a value, I have an error

data.old_positions[0][0].x=5;

Where is the problem? 🙁

  • 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-05T04:39:35+00:00Added an answer on June 5, 2026 at 4:39 am

    data.old_positions is only single dimension –

     data.old_positions[0].x=5;
    

    Assigns the Zeroith item. You’re better off simulating two dimensions than constructing a 2D array.

     data.old_positions[0 * 15 + 0].x = 5;
    

    (Sample *)malloc(... effectively constructs a single dimensional array with no contents. You’re going to need to malloc the second dimension…

     data.old_positions = (Sample *)malloc(sizeof(Sample) * global.nsamples)
     for (int i = 0; i < global.nsamples; i++) 
         data.old_positions[i] = (Point*)malloc(sizeof(Point) * 15);
    

    That should do it.

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

Sidebar

Related Questions

I'm trying to figure out why I'm getting this error: switch (token.type.GetName()) { case
I'm trying to figure out why I am getting this error when my Magento
I am trying to figure out why I am getting an error when using
I am trying to figure out the best way of getting the record count
I am trying to figure out how to declare my delegates without getting any
I'm trying to figure out IO using StaX, but I keep getting malformed and
Ok, so I've been trying to figure out why I keep getting this specific
I'm just trying to figure some things out in my head about getting information
Trying to figure out a stack corruption error in a function when I noticed
I have this query that i've been trying to figure out how to convert

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.