Posted: Sun Dec 09, 2012 10:26 pm Post subject: Random compiling problem?
Well I have Ubuntu OS and I installed the gcc compiler but, whenever i try to compile and run my code in c++ that should work, it either does not output or gives a
code:
Segmentation fault (core dumped)
error, and it doesnt show anything, yes it runs for the odd program, but most of them dont work, even ones that work on other peoples computers, any ideas?
Sponsor Sponsor
jbking
Posted: Mon Dec 10, 2012 11:16 am Post subject: Re: Random compiling problem?
Have you tried adding some logging to the program to see that parts of it are executing? Have you examined if you have any dangling pointers being used that may cause the segmentation fault error? Those would be my suggestions for where to look.
DemonWasp
Posted: Mon Dec 10, 2012 1:03 pm Post subject: RE:Random compiling problem?
Have you tried compiling a really simple program, like:
code:
#include <iostream>
int main ( int argc, char ** argv ) {
std::cout << "Hello World" << std::endl;
}
This is usually standard practice when setting up an environment: write the simplest possible code, then make sure that works first.
Panphobia
Posted: Mon Dec 10, 2012 3:52 pm Post subject: Re: Random compiling problem?
See all the coding that is done throughout the program is ok, but when I start reading from a data file things go wrong, like this code, should provide at least some output, but it produces ZERO, it is code for the 4th questions in the latest dwite round, round 2, then it says that segment thing
Posted: Mon Dec 10, 2012 4:48 pm Post subject: RE:Random compiling problem?
I don't get a segfault when I run this, however if I run it without an existing input file, I just get 5 empty newlines. I'm assuming you're still using this problem, so I tried it with this input file:
code:
3
1 1 0
4
2 2 2 2
3
1 1 0
4
2 2 2 2
4
2 2 2 2
and got this output:
code:
?))
)
?))
?))
?))
Panphobia
Posted: Mon Dec 10, 2012 4:55 pm Post subject: RE:Random compiling problem?
Well I just converted my java code that works 100% perfectley, into c++ and then I ran it and it gets the seg fault, but I also copied another c++ answer from dwite.org that is 5/5 and it did the same thing soo...
Panphobia
Posted: Mon Dec 10, 2012 7:00 pm Post subject: Re: Random compiling problem?
see this code will print the seg thingy and it got 5/5 on the contest
code:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream fin ("DATA4.txt");
ofstream fout ("OUT4.txt");
int n, stack[101], list[101], c[101];
for (int i = 0; i < 5; i++){
fin >> n;
for (int i = 0; i < 101; i++){
stack[i] = -1;
list[i] = i;
}
for (int j = 1; j <= n; j++)
fin >> c[j];
for (int j = 1; j <= n; j++){
if (c[j] > n-j){
fout << "IMPOSSIBLE";
break;
}
stack[j] = list[n-j+1-c[j]];
for (int k = n-j+1-c[j]; k < 100; k++){
list[k] = list[k+1];
}
Posted: Mon Dec 10, 2012 7:26 pm Post subject: Re: Random compiling problem?
Panphobia wrote:
see this code will print the seg thingy and it got 5/5 on the contest
This code runs perfectly fine for me.
Sponsor Sponsor
Panphobia
Posted: Mon Dec 10, 2012 7:36 pm Post subject: RE:Random compiling problem?
Yes but for me it does not, and I do not know why, when I installed everything correctly, the c++ compiler, EVERYTHING
DemonWasp
Posted: Mon Dec 10, 2012 8:52 pm Post subject: RE:Random compiling problem?
Clearly you haven't done everything correctly, or it would work. The problem doesn't have to do with the code, so it must be your environment. Try this:
1. Put this into a file called "hello.cc" :
code:
#include <iostream>
int main ( int argc, char ** argv ) {
std::cout << "Hello World" << std::endl;
}
2. In a terminal, run g++ hello.cc -o hello If that produces any output, there's something wrong. Read the message and correct it, or copy-paste it verbatim here.
3. ./hello
This should output "Hello World". If it outputs "segmentation violation" then you goofed. Read the instructions again and try carefully.
Panphobia
Posted: Mon Dec 10, 2012 9:07 pm Post subject: Re: Random compiling problem?
this was the output [/img]
Panphobia
Posted: Mon Dec 10, 2012 9:16 pm Post subject: Re: Random compiling problem?
and this is the output for what you guys said was ok for you
DemonWasp
Posted: Mon Dec 10, 2012 10:33 pm Post subject: RE:Random compiling problem?
Looks to me like your data file is "DATA4" and your program is looking for "DATA4.txt".
Panphobia
Posted: Mon Dec 10, 2012 11:06 pm Post subject: RE:Random compiling problem?
DemonWasp you just saved my life rofl thank you so much 30 bits
[Gandalf]
Posted: Tue Dec 11, 2012 12:14 am Post subject: RE:Random compiling problem?
Now that is why:
1. You should always check the return code from calls to open().
2. Learning a programming language from contest code is a really bad idea, since the code is rough and unsafe. If you learn from such code, you're basically focusing only on little implementation details for a subset of the language.