?
Solved

new () causing SEGV

Posted on 2003-03-06
8
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 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
Technology Partners: 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!

 

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

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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

800 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