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
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?

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:

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.

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
code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <cstring>
using namespace std;
int main()
{
     freopen("/home/daniel/DATA4.txt", "r", stdin);
     for(int x = 0; x < 5; ++x)
     {
      int n;
      cin >> n;
          vector <int> cards(n);
           int cardsLeft[n];
        for(int i = 0; i < n;++i)
        {
                cin >> cardsLeft[i];
                cards[i] = i + 1;
        }
        string phrase = "";
        for(int i = 0; i < n;++i)
        {
            if(cards.size() - 1 - cardsLeft[i] < 0)
            {
                        cout << "IMPOSSIBLE" << endl;
                }else{
                        phrase += cards[cards.size() - 1 - cardsLeft[i]]+" ";
                        cards.erase(cards.begin()+(cards.size() - 1 - cardsLeft[i]));
                }
        }
        cout << phrase  << endl;
  }
    return 0;
 }

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:
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:

?))
  )
?))
?))
?))

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
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];
            }

        }
        if (stack[n] != -1){
            fout << stack[1];
            for (int j = 2; j <= n; j++){
                fout << " " << stack[j];
            }
        }
        fout << endl;
    }
    return 0;
}

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" :
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.

Author:  Panphobia [ Mon Dec 10, 2012 9:07 pm ]
Post subject:  Re: Random compiling problem?

this was the output Posted Image, might have been reduced in size. Click Image to view fullscreen.[/img]

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 Posted Image, might have been reduced in size. Click Image to view fullscreen.

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 Very Happy 30 bits Very Happy

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.


: