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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:22:54+00:00 2026-05-12T13:22:54+00:00

Ok guys, just a quick question hopefully someone can find my mistake quickly, but

  • 0

Ok guys, just a quick question hopefully someone can find my mistake quickly, but I just can’t see it at the moment

Here is my struct:

typedef struct {
    Car *buffer[CAR_PARK_SIZE];       
    char *arrival_time[CAR_PARK_SIZE]; 
    int  keep_running;               
    int  size;                  
} CarPark;

typedef struct {
    Car *buffer[MAX_QUEUE_SIZE];
    char *arrival_time[MAX_QUEUE_SIZE];
    int keep_running;
    int size;
    int index;
    } CarQueue;

typedef struct {
    char str[3];
    int id;
} Car;

I’ll try to talk this out and hopefully i’ve provided enough information for someone to find out where the problem may lie, the problem is to do with the pointer in the CarQueue buffer, which is meant to be an array of car pointers. When my code runs, it seems to write the same pointer to ever item in the CarQueue buffer, here’s the play by play.

_cq = CarQueue, _cp = CarPark

They are both globals

My thread calls these lines to generate a new car struct.

Car c;
c = new_car();
printf("[A] Car Arrived with ID: %s\n", get_car_id(&c));

pthread_mutex_lock(&mutex);
_cq.size++;
_cq.index++;
_cq.buffer[_cq.index] = &c;

pthread_mutex_unlock(&mutex);

This should put the pointer of the new car structure into the the CarQueue buffer.

The next thread than attempts to take the pointer out of that array, and stick it into its own, and then remove it from the old array. CarPark takes the pointer of the car, puts it into it’s storage array, and then removes it from CarQueue (or in this case, just increases the index and overrides it later).

pthread_mutex_lock(&mutex);
_cp.buffer[_cp.size] = _cq.buffer[_cq.index]; //should take the pointer and put it into its own
printf("[C] Car Parked -> %s\n", get_car_id(_cp.buffer[_cp.size])); //gets the right information
_cp.size++;
pthread_mutex_unlock(&mutex);

I think the problem lies around where the comment in the code is, but I am not certain. Eventually if I print out the array in CarPark, all it’s items seem to be the exact same. Am I passing a wrong pointer to a pointer somewhere accidentally?

if I traverse through the CarPark array and print it out like the following

printf("| element %d is  %s with pointer %d |\n", j, get_car_id(_cp.buffer[j]), _cp.buffer[j]);

They all appear to be the exact same item, same name and pointer.

  • 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-12T13:22:54+00:00Added an answer on May 12, 2026 at 1:22 pm

    problem is here:

    Car c;
    c = new_car();
    get_car_id(&c);
    

    you’re taking pointer at object on stack, so it is deleted/overwritten every time, so the pointers can be the same.

    Car *c;
    c = new_car();
    

    and new_car should return pointer.

    EDIT:

    new_car should malloc() the new car, (and you need to free() it somewhere, for example when removing from queue).

    Consider using array of cars(not array of pointers on cars), and copying the whole item.

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

Sidebar

Related Questions

Just a quick question here guys. I've been searching to no avail so far.
just a quick question guys, how can I simulate a tap on the back
Hey guys just a quick question about the performance of drawRect: as I've noticed
Guys I know this question is silly but just to make sure: Having in
Hey guys just a quick question, I am posting mp3s in dialog boxes each
Hi guys I just have one quick question: what happens when an insert statement
What's up guys, just a quick question. I'm trying to get my GridView to
guys just a quick question. I have a string in a mysql row resembling
Hi guys just a quick question, After having a nightmare trying to get an
Just a quick question, that I cannot fathom out, hope you guys and girls

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.