473,480 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

seg fault problem


Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?
the example below given as a representation of the part of a larger
code which exebits the problem.

many thanks

class A
{
std::map<std::string, std::ofstream*m_os;
std::map<std::string, myTypet4s;
std::vector<stringfile_names;
public:
A(){}
void A_Mthd();
};

A::A()
{
void populate();
}

void A::populate()
{
/* some code to fill file_names vector here */

/* Populate the ostream pointers map. */
for( vector<string>::iterator i = file_names.begin(); i != file_names.end(); i++ )
{
string f = "mydirectory/" + (*i);
ofstream* p_of = new ofstream( f.c_str(), ios::app);
m_os[*i] = p_of;
}
void A::A_Mthd(){
for( vector<string>::iterator i = file_names.begin(); i != file_names.end(); i++ )
(*m_os[*i]) << t4s[*i].B_Mthd() << " " << endl;
}
Feb 3 '07 #1
3 1409
Gary Wessle wrote:
Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?
the example below given as a representation of the part of a larger
code which exebits the problem.

many thanks

class A
{
std::map<std::string, std::ofstream*m_os;
std::map<std::string, myTypet4s;
std::vector<stringfile_names;
public:
A(){}
void A_Mthd();
};

A::A()
{
void populate();
Instead of declaring a 'populate' function here you might want to
actually call it:

populate(); // drop the 'void'

And next time put a trace statements everywhere you need to verify
passing control through, or just use a debugger like everybody else
to confirm that the functions you need called actually get called.
}

void A::populate()
{
/* some code to fill file_names vector here */

/* Populate the ostream pointers map. */
for( vector<string>::iterator i = file_names.begin(); i !=
file_names.end(); i++ ) {
string f = "mydirectory/" + (*i);
ofstream* p_of = new ofstream( f.c_str(), ios::app);
m_os[*i] = p_of;
}
void A::A_Mthd(){
for( vector<string>::iterator i = file_names.begin(); i !=
file_names.end(); i++ ) (*m_os[*i]) << t4s[*i].B_Mthd() << " " <<
endl; }
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 3 '07 #2
"Victor Bazarov" <v.********@comAcast.netwrites:
Gary Wessle wrote:
Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?
the example below given as a representation of the part of a larger
code which exebits the problem.

many thanks

class A
{
std::map<std::string, std::ofstream*m_os;
std::map<std::string, myTypet4s;
std::vector<stringfile_names;
public:
A(){}
void A_Mthd();
};

A::A()
{
void populate();

Instead of declaring a 'populate' function here you might want to
actually call it:

populate(); // drop the 'void'

And next time put a trace statements everywhere you need to verify
passing control through, or just use a debugger like everybody else
to confirm that the functions you need called actually get called.
actually, the function populate is being called from within the
ctor. I miss typed this part of the example.

trace statements? what is that, I put "cout <<" every where and that
is way I was able to narrow it down to the area mentioned at the start
of my original post.

I am not able to use the gdb debugger because I am not able to get it
working with this multi-threaded code.

I which I can do more, but I don't know what or how.

thanks
Feb 3 '07 #3
Gary Wessle wrote:
"Victor Bazarov" <v.********@comAcast.netwrites:
>Gary Wessle wrote:
Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?
the example below given as a representation of the part of a larger
code which exebits the problem.

many thanks

class A
{
std::map<std::string, std::ofstream*m_os;
std::map<std::string, myTypet4s;
std::vector<stringfile_names;
public:
A(){}
void A_Mthd();
};

A::A()
{
void populate();

Instead of declaring a 'populate' function here you might want to
actually call it:

populate(); // drop the 'void'

And next time put a trace statements everywhere you need to verify
passing control through, or just use a debugger like everybody else
to confirm that the functions you need called actually get called.

actually, the function populate is being called from within the
ctor. I miss typed this part of the example.
[snip]
>
I which I can do more, but I don't know what or how.
One thing you could do is to provide a minimal complete piece of code that
demonstrates _the_ problem (not some other problem as you OP). And make it
so that one can compile and run it without adding stuff like main().
Best

Kai-Uwe Bux

Feb 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
8953
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
11378
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
2971
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
3
4219
by: Moshe Kravchik | last post by:
Hi! We have a Web Service written in ATL Server and a client written in Java using Axis. When something goes wrong on the server side, it returns an HRESULT of the error which is translated into...
0
1283
by: relaxedrob | last post by:
Hi All, I have a portType such as this: <portType name="CMLeJobSoapGetEmpBrand"> <operation name="EJobGetEmpBrand"> <input message="tns:EJobEmpBrdReq" name="EJobEmpBrdReq"/> <output...
27
3313
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
3
5127
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
14
2143
by: Matt | last post by:
Hello. I'm after some general advice. At the moment i have some code that should run a FOR loop a set number of times. Each time the structure stamps is read in as follows: for( i = 0 ; i <...
3
7546
by: =?Utf-8?B?TWFucHJlZXQgU3VzaGls?= | last post by:
I am having a Webservice within which i am throwing SOAP Exceptions and therefore whenever something wrong happens a SOAP fault comes up in the response - see below: <?xml version="1.0"...
8
14621
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6912
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7092
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.