The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Woes with C (incompatible assignment...and threads).

TurnerTurner Registered User regular
edited October 2018 in Help / Advice Forum
deleted.

Turner on

Posts

  • telcustelcus Registered User regular
    edited November 2007
    What's your code for creating ready_trains?
    Dynamic arrays need to be malloc'ed before they can be used as arrays.

    telcus on
    [SIGPIC][/SIGPIC]
  • Bob SappBob Sapp Registered User regular
    edited November 2007
    Looks like you're trying to assign a train pointer when the array holds trains, not pointers. I'm making the assumption that the array is dynamically allocated. Try ready_trains[numreadytrains] = *traintest;

    Bob Sapp on
    fizzatar.jpg
  • falsedeffalsedef Registered User regular
    edited November 2007
    ready_trains[numreadytrains] = traintest

    should be


    ready_trains = traintest


    assuming both are arrays

    falsedef on
  • DocDoc Registered User, ClubPA regular
    edited November 2007
    Bob Sapp wrote: »
    Looks like you're trying to assign a train pointer when the array holds trains, not pointers. I'm making the assumption that the array is dynamically allocated. Try ready_trains[numreadytrains] = *traintest;

    this

    Doc on
  • telcustelcus Registered User regular
    edited November 2007
    I think the problem may be that ready_trains is a dynamic array of train structs, where as traintest is a pointer to a train struct.

    What you want is ready_trains to be a dynamic array of pointers to train structs

    train **ready_trains, and malloc accordingly.

    God C is a confusing language.

    telcus on
    [SIGPIC][/SIGPIC]
  • falsedeffalsedef Registered User regular
    edited November 2007
    Doc wrote: »
    Bob Sapp wrote: »
    Looks like you're trying to assign a train pointer when the array holds trains, not pointers. I'm making the assumption that the array is dynamically allocated. Try ready_trains[numreadytrains] = *traintest;

    this

    That looks like a buffer overflow.

    falsedef on
  • TurnerTurner Registered User regular
    edited October 2018
    deleted.

    Turner on
  • falsedeffalsedef Registered User regular
    edited November 2007
    I looked at all of your code this time. If you're trying to pass by pointer instead of copy:

    This part in your thread creator area:
    train *temp;
            temp = (pthread_t *) malloc(sizeof(pthread_t)*numtrains);
    
    as I see is suppose to look more like:
    train *temp; // temp pointer
    numreadytrains = 0; // the global scope current index
    ready_trains = (train *) malloc(sizeof(train)*numtrains); // allocate space for global scope ready trains
    

    Because temp is being assigned within your loop, you don't need to allocate it individually:
    pthread_create(&control , NULL, Control, NULL);
                    for(j = 0; j < numtrains; j++)  {
                            temp = &(train_array[j]); // pass the address of the train at index j
                            pthread_create(&train_array[j].thread, NULL, Train, (void *)temp);
                    }
    


    Then you can use the fix Bob Sapp gave you. At the moment, if you use Bob's code without the changes, it looks like you're allocating temp instead of allocating readytrains to the appropriate size, which might segfault or give bad data.

    falsedef on
  • TurnerTurner Registered User regular
    edited October 2018
    deleted.

    Turner on
Sign In or Register to comment.