Programm works fine in Visual Studio but not under g++

Hey guys,

I wrote a Programm with a class "Buchungslist" which basically manages a linked list. The program works fine under Visual Studio and everything seems to work as expected. However when i compile (works) and run the programm in Ubuntu (g++) the program keeps staying in the while loop after the third time the function "addBuchung" is called.

Anyone has an idea why this could happens?

I didnt put the complete source-code below as it would be too much. I hope there is no need for it and I'm just making a noob-mistake.

Thank you for hints!

Cheers, Sam

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "Buchungslist.h"

int main() {
	Buchungslist buchungen;
	
	buchungen.addBuchung(10, "TTTTTTTTTTTTTTTT", 120.3, bew.getHead(), schus,2);
	buchungen.addBuchung(7, "T32434234TTTTTT", 20, bew.getHead(), schus, 2);
	buchungen.addBuchung(12, "T32434234TTTTTT", 20, bew.getHead(), schus, 2); // << In this call the While-Loop never ends
	buchungen.addBuchung(3, "T32434234TTTTTT", 20, bew.getHead(), schus, 2);

	buchungen.addBuchung(5, "T32434234TTTTTT", 20, bew.getHead(), schus, 2);

	return 0;
}


And in Buchungslist.cpp the Method "addBuchung":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Buchungslist::addBuchung(int date, const char* bez, double value, Bewohner * inv, Bewohner * schu, int nschu) {
	n++;
	Buchung * bu = new Buchung(n,date,bez,value,inv,schu,nschu);
	if (head == NULL) {
		head = bu;
		tail = bu;
		bu->next = NULL;
		bu->prev = NULL;
	}
	else {
		Buchung * tmp = head;
		while (tmp->next != NULL) { // <<< HANGING POINT
			tmp = tmp->next;
		}
		tail = bu;
		tmp->next = bu;
		tail->prev = tmp;
	}
}
Last edited on
Hi,

I have some doubts:

In your Buchungslist.cpp in line 2 you have n++; what is the value and scope of n?

Also what happens if you delete line 9 of main.cpp or change the parameters? does it still produces that bug?

PS:needless to say, if it runs perfectly on windows it could be a cross-platform problem
> if it runs perfectly on windows it could be a cross-platform problem
I would guess that their tests were easy and the failure is because it is invoking undefined behaviour.


> Buchung * bu = new Buchung(n,date,bez,value,inv,schu,nschu);
considering that in lines 7,8 you touch the next,prev pointers, I would guess that you don't initalize them in the constructor

so when inserting more elements, tail->next will have garbage.


> I didnt put the complete source-code below as it would be too much.
always post a minimal example that does reproduce your issue.
If your example needs several files or many lines, consider to reduce it. If that's too hard use another paste mechanism, like github.

> bew.getHead(), schus
¿what are those?
considering that in lines 7,8 you touch the next,prev pointers, I would guess that you don't initalize them in the constructor

That.

Is the VS build debug or release?
The VS debug build initializes memory (including Buchung.next) with 0.
The g++ does not.
Topic archived. No new replies allowed.