What are the differences in a linux g++ compiler and a windows MINGW g++ compiler?

There are programs which use STL C++ and run perfectly on Windows MINGW g++ compiler but show Segmentation fault sometimes(and sometimes not (strange) ) in linux.
as for example?
Last edited on
Sounds like undefined behaviour. Your code probably does something wrong and you were just lucky that it gave you an error message on Linux. Fix your code, and it should work everywhere.
Last edited on
Line number 62-68. The problem is when I give a Unicode encoded txt file, it shows a strange behaviour
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <cmath>
#include <locale>
using namespace std;
#if defined(_WIN32)
	char os='W';
#else
	char os='L';
#endif
int m=127;
int h(string );
bool compare(pair<string,int> s1, pair<string,int> s2);
int main()
{
	ios::sync_with_stdio(false);
	locale loc;
	if(os=='W')
		loc.global(locale(""));
	else
		loc.global(locale("en_US.UTF-8"));
	wcout.imbue(loc);
	if(os=='W')
		system("dir /b *.txt > files.dat");
	else
		system("ls *.txt > files.dat");
	vector<string> FileList;
	string file;
	ifstream fp("files.dat");
	fp>>file;
	while(!fp.eof())
	{
		FileList.push_back(file);
		fp>>file;
	}
	fp.close();
	int NofFile=FileList.size();
	int choice, slot, i, j, c;
	pair<string,int> SortedFiles[NofFile];
	wstring str;
	string s;
	cout<<"\n\nChoose a method : \n";
	cout<<"1. Hash tables\n";
	cout<<"2. Tries\n";
	cout<<"3. Boyer Moore Algorithm\n";
	cout<<"Enter choice : ";
	cin>>choice;
	list<string> H[m][NofFile];
	list<string> B[NofFile];
	wifstream FP;
	switch(choice)
	{
		case 1:
			
			for(int f=0;f<NofFile;f++)
			{
				char* CurrFile=&FileList[f][0];
				FP.open(CurrFile);
				FP.imbue(loc);
				FP>>str;
				while(!FP.eof())
				{
					string s( str.begin(), str.end() );
					i=0;
					j=s.length();
					if(!s.empty())
					{
						while(!isalnum(s[i])) i++;
						while(!isalnum(s[j])) j--;
					}
					if(j>=i)
					{
						s=s.substr(i,j-i+1);
						transform(s.begin(), s.end(), s.begin(), ::tolower);
						slot=h(s);
						H[slot][f].push_front(s);
					}
					FP>>str;
				}
				FP.close();
			}
			cout<<"Enter a word to search : ";
			cin>>s;
			transform(s.begin(), s.end(), s.begin(), ::tolower);
			slot=h(s);
			for(int f=0;f<NofFile;f++)
			{
				SortedFiles[f].first=FileList[f];
				SortedFiles[f].second=count(H[slot][f].begin(),H[slot][f].end(),s);
			}
			break;
		case 2:
			cout<<"Tries\n";
			break;
		case 3:
			
			for(int f=0;f<NofFile;f++)
			{
				char* CurrFile=&FileList[f][0];
				FP.open(CurrFile);
				FP.imbue(loc);
				FP>>str;
				while(!FP.eof())
				{
					string s( str.begin(), str.end() );
					i=0;
					j=s.length();
					if(!s.empty())
					{
						while(!isalnum(s[i])) i++;
						while(!isalnum(s[j])) j--;
					}
					if(j>=i)
					{
						s=s.substr(i,j-i+1);
						transform(s.begin(), s.end(), s.begin(), ::tolower);
						B[f].push_back(s);
					}
					FP>>str;
				}
				FP.close();
			}
			cout<<"Enter a word to search : ";
			cin>>s;
			transform(s.begin(), s.end(), s.begin(), ::tolower);
			for(int f=0;f<NofFile;f++)
			{
				SortedFiles[f].first=FileList[f];
				SortedFiles[f].second=count(B[f].begin(),B[f].end(),s);
			}
			break;
		default:
			cout<<"End\n";
			break;
	}
	sort(SortedFiles,SortedFiles+NofFile,compare);
	for(int f=0;f<NofFile;f++)
		cout<<SortedFiles[f].first<<" "<<SortedFiles[f].second<<endl;
	return 0;
}
bool compare(pair<string,int> s1, pair<string,int> s2) { return s1.second>s2.second; }
int h(string s)
{
	unsigned int sum=0,len=s.length();
	unsigned int po=1;                                         
	for(int i=0;i<len;i++)
	{
		if(isalpha(s[i]))
			sum=( sum + (s[i]-97)*po )%m;
		else
			sum=( sum + 26*po )%m;
		po = (po*27)%m;
	}
	return sum;
}
Last edited on
The problem is when I give a Unicode encoded txt file, it shows a strange behaviour

Yeah, if you have a unicode string you should encode it to an ascii string.
I wonder why that works in windows?
The problem here is not the STL container, I think the problem is somewhere on file-level
UTF-8 uses more than one byte for each character. The tolower function works on one byte at a time so it can't handle multi-byte characters. The reason it works for you on Windows is probably because it uses some other character encoding that only use one byte for the characters you have tested. Test with some more exotic character like ♖ and it will probably not work.

I have not read your code very carefully but the way you convert from wide string on line 68 will not work.
Topic archived. No new replies allowed.