Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

new () causing SEGV

Posted on 2003-03-06
8
Medium Priority
?
268 Views
Last Modified: 2010-04-01
I am trying to run this program. I am getting SEGV when trying to initialize constructs via new(). What is wrong here?

#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <string.h>
#include "RWCustomer.h"
#include "RWParts.h"
#include "RWService.h"
#include "RWVehicle.h"
#include "searched.h"
#include "error.h"

class interface {
     public:
          fstream transF;
          masterFile *cust;
          masterFile *veh;
          masterFile *srv;
          masterFile *prt;
          searched *where;
          error *report;
          char functionCode[3]; // storage for functionCode's
          char fileCode[3]; // storage for fileCode's
          char keys[20];
          char operation[256];
          interface() {
               fstream transF("xactions1", ios::in|ios::out); // open IO stream
               functionCode[2]=0;
               fileCode[2]=0;
               operation[255]=0;
               keys[19]=0;
               cust=new customer();
               veh=new vehicle();
               srv=new service();
               prt=new parts();
               where=new searched();
               report=new error();
          }
          int validated();
          int validFunctionCode();
          int validFileCode();
          void deleteRecord(masterFile *mst, int r);
          void changeRecord(masterFile *mst, int r);
          void printIt(masterFile *mst, int r);
          void SPACE() {
               transF.seekg(1,ios::cur); // Move the GET pointer one position
          }
};
0
Comment
Question by:oscar_wilson
7 Comments
 
LVL 1

Expert Comment

by:helmet275
ID: 8083233
It could possibly be the that cust,veh,srv,prt are set up as masterFile pointers.  when you use the new operator for these variables they need to be new masterFile()
0
 

Expert Comment

by:evidence0001
ID: 8083477
yes, however, masterFile is an abstract class and creating oderived objects 'defined' as a masterFile is still ok in C++.
0
 

Expert Comment

by:evidence0001
ID: 8083515
helmet,
I have used gdb to try to see some of the problems.

The stack has two things on it just below,
but right after new parts() is called, the stack goes crazy and tries to access weird memory locations.

(gdb) where
#0  service::service (this=0x2be50) at service.cpp:51
#1  0x000158dc in main () at main.cpp:21
(gdb) s
main () at main.cpp:22
22              prog->prt=new parts();
(gdb) where
#0  main () at main.cpp:22
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
0xff241f74 in ?? ()
(gdb) where
#0  0xff241f74 in ?? ()
#1  0xff241b10 in ?? ()
#2  0xff2418a8 in ?? ()
#3  0xff353f08 in ?? ()
#4  0x00015924 in main () at main.cpp:22
(gdb) Quit
(gdb)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:evidence0001
ID: 8083537
The stack trace (gdb) that i performed showed a seg fault when calling new parts() but not new cust,veh,srv().

ALso to note, if newparts() is commented out then the seg fault occurs at new searched(), the problem might not be specific..

any help is appreciated.
0
 
LVL 5

Accepted Solution

by:
Kocil earned 100 total points
ID: 8084065
How about this ?
interface():
   transF("xactions1", ios::in|ios::out); // open here
{
   // not here      
   fstream transF("xactions1", ios::in|ios::out);

   ....

}
             
0
 
LVL 11

Assisted Solution

by:cup
cup earned 100 total points
ID: 8084538
Probably need to look at the constructor of masterFile and customer to determine what has gone wrong.

Does masterFile need transF to be passed in?  Doesn't make much sense opening a file and not using it.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9889654
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points split between Kocil and cup (a local in the constructor masks the member variable with the same name)

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question