Hi, that's my first bigger work in C++. Programm works good but I don't know where I have to alocate memory. And for which structures or something else I have to do alocation? Can anybody help me? Thx.

There is my code:

#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <pthread.h>
#include <queue>
#include <map>

using namespace std;

useconds_t WAITING_TIME_UNIT = 100000;

map<pthread_t, int> userMap;
map<pthread_t, int>::iterator it;
pair<map<pthread_t, int>::iterator, bool> ret;

queue<pthread_t> queueA[5][2];

pthread_t threadPerson, threadElevator;
pthread_mutex_t mutexMove, mutexMove2, mutexCout;
pthread_cond_t condMove, condMove2;

struct Person {
    int id, fromLevel, toLevel, direction; // direction: 1 = up, 0 = down
};

struct Elevator {
    int actualLevel, numberOfPeople, direction;
};

Elevator elevator;

void* doElevator(void *null) {
    // there is code for elevator thread
    // there is used variable: elevator 
}

void* doPerson(void *person) {
    Person man;
    man = *((Clovek*) person);

    // there is code for people thread
    // there is used variable: man, elevator
}

int createPerson(string s) {
    string idS, fromLevelS, toLevelS;
    int check = 1, id, fromLevel, toLevel;

    Person user;

    // I check input if it's all right

    user.id = id;
    user.fromLevel = fromLevel;
    user.toLevel = toLevel;

    if (fromLevel < toLevel) {
        user.direction = 1;                  // up
    } else if (fromLevel > toLevel) {
        user.direction = 0;                  // down
    } else {                            // fromLevel == toLevel ... error
        return 0;
    }

    int ok;
    ok = pthread_create(&threadPerson, NULL, doPerson, (void *) & user);
    if (ok != 0) {
        return 0;   // thread not made
    }
    return 1;
}

int main(int argc, char** argv) {
    string input;

    elevator.actualLevel = 1;             // default set up of elevator
    elevator.numberOfPeople = 0;    // default set up of elevator
    elevator.direction = 1;                 // default set up of elevator
    
    pthread_mutex_init(&mutexMove, NULL);
    pthread_mutex_init(&mutexMove2, NULL);
    pthread_mutex_init(&mutexCout, NULL);
    pthread_cond_init(&condMove, NULL);
    pthread_cond_init(&condMove2, NULL);

    if (pthread_create(&threadElevator, NULL, doElevator, NULL) != 0) {
        destroy();                  // destroys mutexes and conditions variables
        exit(EXIT_FAILURE);
    }
    while (!feof(stdin)) {
        cin >> input;
        if(createPerson(input) == 0) {
            destroy();                  // destroys mutexes and conditions variables
            exit(EXIT_FAILURE);
        }
    }

    pthread_join(threadElevator, NULL);    // wait on threadElevator exit
    if(isAllEmpty() == 1) {    // check if all queues and map are empty
        destroy();                  // destroys mutexes and conditions variables
        return(EXIT_SUCCESS);
    } else {
        destroy();                  // destroys mutexes and conditions variables
        return(EXIT_FAILURE);
    }
}

You need to allocate memory - using the 'new' operator - for anything you want to reside on the heap (as opposed to the stack) and the rule is that if you 'new' it, then when you are finished with it you must 'delete' it. It is also good practise to set the pointer to 0 when you have called 'delete' on it (that is, de-allocated the memory it points to) so that you won't accidently try and use it again or delete it again, which could cause memory corruption.

You only need memory allocation when you need memory for n-objects...

Not only that, stack space is limited so creation of large data structures is generally more suited to the heap.

So map and queue are OK, but I have to allocate memory for structures Elevator and Person? But I haven't got them as pointer and "void * malloc ( size_t size );" I don't know how use it in my programm.

This article has been dead for over six months. Start a new discussion instead.