Computer Science Canada Random compiling problem? |
Author: | Panphobia [ 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
|
Author: | jbking [ 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. |
Author: | DemonWasp [ Mon Dec 10, 2012 1:03 pm ] | ||
Post subject: | RE:Random compiling problem? | ||
Have you tried compiling a really simple program, like:
This is usually standard practice when setting up an environment: write the simplest possible code, then make sure that works first. |
Author: | Panphobia [ 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
|
Author: | Insectoid [ 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:
and got this output:
|
Author: | Panphobia [ 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... |
Author: | Panphobia [ 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
|
Author: | Dreadnought [ 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. |
Author: | Panphobia [ 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 |
Author: | DemonWasp [ 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" :
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. |
Author: | Panphobia [ Mon Dec 10, 2012 9:07 pm ] |
Post subject: | Re: Random compiling problem? |
this was the output ![]() |
Author: | Panphobia [ 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 ![]() |
Author: | DemonWasp [ 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". |
Author: | Panphobia [ Mon Dec 10, 2012 11:06 pm ] |
Post subject: | RE:Random compiling problem? |
DemonWasp you just saved my life rofl thank you so much ![]() ![]() |
Author: | [Gandalf] [ 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. |
Author: | TerranceN [ Tue Dec 11, 2012 12:19 am ] |
Post subject: | RE:Random compiling problem? |
To avoid this kind of error, you should check that the file is available (using fin.is_open() or fin.fail()) before you try to access it. |
Author: | Panphobia [ Tue Dec 11, 2012 12:27 am ] |
Post subject: | RE:Random compiling problem? |
Well I didn't think of it maybe? |
Author: | QuantumPhysics [ Tue Dec 25, 2012 5:23 am ] |
Post subject: | RE:Random compiling problem? |
Just a little tip (especially on Ubuntu). You have way too man includes there. Try to limit them (if you are working in C++). It can kill the speed performance of your program. C++ already tends to be slow on its own. If you are using .c then that's a different story -> But you should be using .c on GNU/LINUX, ever heard of emacs? sudo apt-get emacs-1.7.0.1 its great |
Author: | Insectoid [ Tue Dec 25, 2012 6:37 am ] |
Post subject: | RE:Random compiling problem? |
Quote: C++ already tends to be slow on its own.
Wait, what? Quote: If you are using .c then that's a different story
.c is for C source files. .cpp is for C++ source files. GCC won't even compile a c++ source file with a .c extension on my computer (though it will compile C files with a .cpp extension). Quote: ever heard of emacs? sudo apt-get emacs-1.7.0.1 its great
emacs is just a text editor. Not really important, and does not solve OP's issue. |
Author: | QuantumPhysics [ Tue Dec 25, 2012 6:08 pm ] |
Post subject: | RE:Random compiling problem? |
I'm just saying that Cpp is slow over C. C Preprocessor is much faster. I don't understand why you cant compile a .c project as .cpp (my computer compiles it flawlessly(all my computers do)) |
Author: | Insectoid [ Wed Dec 26, 2012 11:11 am ] |
Post subject: | RE:Random compiling problem? |
The preprocessor has nothing to do with runtime execution. That's only compile time. And both C and C++ use the same one (if you're using gcc, anyway). Also, I can compile C source saved as a .cpp file. I cannot compile C++ source saved as a .c file. GCC cares about your extension. |