C++ in G++ - Segmentation Fault when not using pointers

I'm trying to use G++ to compile some C++ code. It seems to work fine in other compilers, but for whatever reason, G++ won't produce working output.

Disclosure: This is part of a homework assignment, but I feel like it's more of a compiler issue, since it works in other compilers.

Here's the snippet that's wreaking havoc:

set<int> t1, t2;

It's strange because the following code works just fine:

set<int> *t1 = new set<int>(); set<int> *t2 = new set<int>();

Granted, I have to use -> instead of ., but that's expected. The first snippet produces a segmentation fault at runtime. The second does intuitively what I'd expect it to.

Anyhow, behind the scenes, the .cpp for set has this:

#include <cstdlib> #include <iostream> using namespace std; template <class T> set<T>::set() : bag<T>() {} template <class T> set<T>::set(const set<T>& b) : bag<T>(b) {}

The .h looks like this:

#include "bag.h" template <class T> class set : public bag<T> { public: set( ); set(const set &b); // ... }; #include "set.cpp"

And last but not least, the bag.cpp and bag.h files looks like this:

using namespace std; template <class T> bag<T>::bag() { head = NULL; } template <class T> bag<T>::bag(const bag<T>& b) { // ... }

and bag.h:

template <class T> class bag { public: bag( ); bag(const bag &b); // ... }; #include "bag.cpp"

Again, I feel like G++ just hates me, but then again I could be doing something dumb. Just a simple nudge in the right direction would be great.

-------------Problems Reply------------

Here's a general hint that will make your life a million times easier.

Compile this program with the "-g" and "-Wall" flags:

gcc -g -Wall foo.cpp

The "-g" adds debugging information. The "-Wall" spits out additional warnings when compiling. Then use the debugger:

gdb ./a.out

Hit run to start your program. Use bt to dump your call stack once your code crashes. You can then see exactly where the crash is happening in your code.

While you're at it, google "gdb tutorial". Spending an hour or two learning how to use gdb properly will pay itself back, with interest. I promise you.

Category:c# Views:0 Time:2010-10-12

Related post

  • What is segmentation fault? 2010-02-27

    What is segmentation fault? Is it different in C and C++? How are segmentation fault and dangling pointer related? --------------Solutions------------- Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to

  • Why does strcpy trigger a segmentation fault with global variables? 2008-09-23

    So I've got some C code: #include <stdio.h> #include <string.h> /* putting one of the "char*"s here causes a segfault */ void main() { char* path = "/temp"; char* temp; strcpy(temp, path); } This compiles, runs, and behaves as it looks. H

  • Why do I get a segmentation fault when writing to a string? 2008-10-02

    The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; printf("%s", str); While this works perfectly well: char str[] = "string"; str[0] = 'z'; printf("%s", str); Tested with MSVC and GCC. --------------Solutions--------

  • Segmentation Fault in TCP communication 2008-11-21

    I'm getting a segmentation fault in the following C code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include

  • Weird ruby segmentation fault with DBI and MySQL 2009-01-12

    The following code gives a segmentation fault on the last line require 'rubygems' gem 'mysql' gem 'dbi' require 'dbi' require 'mysql' dsn = "DBI:Mysql:DATABASE:www.HOST.net" # redacted dbh = DBI.connect(dsn, "USERNAME", "PASSWORD") # redacted sth = d

  • Apache w/mod_rails segmentation fault 2009-01-19

    I am running Redmine on Apache 2 with mod_rails (passenger) 2.0.3 and Enterprise Ruby 1.8.6. Every so often I get a segfault from Apache when I try to login. Anyone know how I can debug this issue? I see something like this in Apache's error.log: [Mo

  • Line number of segmentation fault 2009-02-02

    Is there any gcc option I can set that will give me the line number of the segmentation fault? I know I can: Debug line by line Put printfs in the code to narrow down. Edits: bt / where on gdb give No stack. Helpful suggestion --------------Solutions

  • Try/Catch a segmentation fault on Linux 2009-02-05

    I have a Linux C++ application and I'd like to test an object pointer for validity before dereferencing it. However try/catch doesn't work for this on Linux because of the segmentation fault. How can this be done? --------------Solutions-------------

  • C program compiled with cygwin in Windows works, segmentation fault under Linux. Is cygwin GCC 'bad'? 2009-02-09

    For my Programming 102 class we are asked to deliver C code that compiles and runs under Linux. I don't have enough spare space on my hard drive to install Linux alongside Windows, and so I use cygwin to compile my programs. The most recent program I

  • long integer as array index in C gives segmentation fault 2009-03-16

    The following C Code gives a segmentation fault: #include <stdio.h> #include <stdint.h> int main(){ uint32_t *a; uint32_t idx=1233245613; a[idx]=1233; return 0; } How can I use uint32_t as index of an array in C? Or how can I use array li

  • How can I avoid a segmentation fault while resize a vector like this 2009-03-18

    I think it is a simple question for you....i am pretty new in c++..... So i have a vector defined like this: vector<vector<float> > big_vector; I read a file and initialized this vector, then the big_wector has about 200,000 elements in i

  • Why is my C++ code causing a segmentation fault well after using the read(-) function? 2009-03-24

    My application is suspending on a line of code that appears to have nothing wrong with it, however my IDE appears to be suspending on that line with the error: gdb/mi (24/03/09 13:36) (Exited. Signal 'SIGSEGV' received. Description: Segmentation faul

  • Why do I get a segmentation fault when calling a virtual method in this code? 2009-04-02

    I'm still learning C++; I was trying out how polymorphism works and I got a segmentation fault when calling a virtual method. (Note: I didn't mark the destructor as virtual, I was just trying out to see what happens.) Here's the code: #include <io

  • Segmentation fault in strcpy 2009-04-05

    consider the program below char str[5]; strcpy(str,"Hello12345678"); printf("%s",str); When run this program gives segmentation fault. But when strcpy is replaced with following, program runs fine. strcpy(str,"Hello1234567"); So question is it should

  • Why am I getting a segmentation fault with this code? 2009-04-16

    I am getting a segmentation fault while running this code. I can't work out why this is happening - can anyone see a possible reason? (I have already got and initialized the semaphore's shared memory.) My code: #include<stdlib.h> #include<sy

  • Ruby monitor segmentation fault 2009-04-18

    I followed the example from http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/index.html and modified the code a bit: require 'monitor.rb' buf = [] buf.extend(MonitorMixin) empty_cond = buf.new_cond producer = Thread.start do # producer line = "prod

  • Segmentation Fault with pointer-to-pointer with C / Linux 2009-04-25

    In the following code I get a segmentation fault: Set *getpar() {...} char function(...) { Set **S; *S = getpar(); /* Segmentation Fault */ ... } But the bizarre thing is that with few changes there is no segmentation fault: Set *getpar() {...} ... c

  • Bus error vs Segmentation fault 2009-05-08

    Difference between a bus error and a segmentation fault? Can it happen that a program gives a seg fault and stops for the first time and for the second time it may give a bus error and exit ? --------------Solutions------------- On most architectures

  • Large 2D array gives segmentation fault 2009-05-12

    I am writing some C++ code in Linux where I have declared a few 2D arrays like so: double x[5000][500], y[5000][500], z[5000][500]; During compilation there is no error. When I execute it says "segmentation fault". Wen I reduce the size of the array

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.158 (s). 11 q(s)