I try to create BFS like of tree. I made the tree and queue the child.
I’ve wrote the code in here https://github.com/cengek/CKDatastruct.
But strangely, when my friends who use Windows and MinGW to compile it it return error. It doesn’t happen in OSX and and Linux.
My friend said it become error when I used malloc.
I don’t know where the specific code that does the error but I think it’s in here
this is the part where I put the child of tree into the queue
while (isEmpty(antrianNodes) != 1) {
//tampilkan isinya
printf("%c,", antrianNodes.first->paket.s->c);
simpul * now = antrianNodes.first->paket.s;
simpul * nodePertama = now;
//jika punya anak
//masukkan child dari node alamat di queue ke dalam queue sekarang
if(now->child != NULL){
simpul * nowchild = now->child;
//jika punya saudara
if(nowchild->sibling != NULL){
//looping memasukkan anak-anak
while (nowchild->sibling != now->child) {
add(&antrianNodes, nowchild);
nowchild = nowchild->sibling;
}
//masukkan yang terakhir
add(&antrianNodes, nowchild);
}else{
//tak punya saudara masukkan saja satu
add(&antrianNodes, nowchild);
}
}
del(&antrianNodes);
}
and in here for adding the childs into the queue so I can process it. It does the print of the child.
void add(queue *Q, simpul *s){
elemen * baru = (elemen *) malloc(sizeof(elemen));
baru->paket.s = (simpul *) malloc(sizeof(simpul));
baru->paket.s = s;
baru->next = NULL;
if(isEmpty(*Q) == 1){
(*Q).first = baru;
(*Q).last = baru;
}else{
(*Q).last->next = baru;
(*Q).last = baru;
}
(*Q).jumlahElemen++;
}
I think it is the ordinary code of queue and tree.
Honestly I don’t know where the exact part because it does strangely different in every operating system, i’ve try to compile it in ideone and give the correct result as this http://ideone.com/vVNOe
My friend said the error from windows is like this
Problem signature:
Problem Event Name: APPCRASH
Application Name: main.exe
Application Version: 0.0.0.0
Application Timestamp: 4fa665b6
Fault Module Name: main.exe
Fault Module Version: 0.0.0.0
Fault Module Timestamp: 4fa665b6
Exception Code: c0000005
Exception Offset: 000015e0
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409
If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt
Does every operating system do the different malloc? Or is it just my code that do the error?
Best Regards
giripp
I’m not sure if this is the cause of your problem or not, but it might be. In
add()you allocate a newsimpuland place that pointer into apaket, but then you immediately overwrite that pointer with the one passed as an argument toadd():But I’m not sure.
Also, your
makeTree()function needs to initialize thechildmember of the root node: