Hi i have the user that get randomly a number, make a structure called transaction and add it to a list. The problem is that give me back segmentation fault but i don't know where is the problem. This is a big problem cause this program is launched by another like his son, if the segmentation will not be solved the father cannot do his duty.
main:
int main(int argc, char const *argv[])
{
id = atoi(argv[1]);
int shmid = shmget(SH_KEY, SO_NODES_NUM * sizeof(pid_t), IPC_EXCL | 0666);
int *nodeList = (int *)shmat(shmid, NULL, 0);
initTransactionPool();
struct Transaction prova1;
while (1)
{
int r1 = rand() % totalBudget + 2;
/*getBudget();
setBudget();*/
if (totalBudget<2)
{
break;
}
prova1.money = r1;
prova1.receiver = 12;
prova1.sender = 1;
prova1.reward = 1;
prova1.timestamp = "ciao";
addTransaction(prova1);
totalBudget-=r1;
printf("\n il budget e' %d",totalBudget);
}
freeTransactionPool();
return 0;
}
other function:
void initTransactionPool()
{
transactionPool = (struct Transaction *)malloc(SO_TP_SIZE * sizeof(struct Transaction *));
}
void freeTransactionPool()
{
free(transactionPool);
}
int addTransaction(struct Transaction transaction)
{
if (sizePool >= SO_TP_SIZE - 1)
return -1;
++sizePool;
transactionPool[sizePool] = transaction;
return 0;
}
the data: transactionPool
is saved in a ".h" like this : extern struct Transaction* transactionPool;
and in a ":c" like this: struct Transaction* transactionPool;