[prog] This segmetation fault makes no sence to me.
Jimen Ching
jching at flex.com
Fri May 30 09:34:27 EST 2003
On Fri, 30 May 2003, Sue Stones wrote:
>anual salary is 350000
Is this a software engineer's salary? ;-)
> Firm andCo = Firm();
> Manager smith = Manager("Smith", 1, 350000);
Using an assignment operator with an explicit call to the constructor only
adds additional temporaries. I.e. in the case of smith, it is equivalent
to:
Manager smith;
smith = Manager("Smith", 1, 350000);
You can avoid the extra work with just:
Manager smith("Smith", 1, 350000);
The same goes for andCo. Note also, I didn't see an assignment operator
defined below, so one is generated for you. And it just does a byte by
byte copy. I'll mention why this is significant later.
>Manager::Manager(char *e_name, int e_num, int annual_salary)
>{
> cout << "conctructing Manager\n";
> Employee(e_name, e_num);
> setSalary(annual_salary);
>}//constructor
Does Manager inheret from Employee? If so, you need a mem-initializer
like so:
Manager::Manager(char *e_name, int e_num, int annual_salary)
: Employee(e_name, e_num)
{
// ...
}
Even if you have an employee member data, you initialize it the same way,
but instead of the class name, use the member data name. I guess you
could also initialize a member data using an assignment operator too.
But calling the constructor of Employee like the above will just create a
temporary.
[main included here for reference]
int main(void)
{
Firm andCo = Firm();
Manager smith = Manager("Smith",1, 350000);
andCo.add((smith));
return 0;
}//main
The output looks like andCo.add() succeeded and returned. When 'return 0'
is executed, the destructor comes into play. This is most likely where it
segfaulted. As I mentioned above, an assignment operator function was
probably generated (if you didn't define one). This will perform a byte
copy of the contents of Manager. If Manager inherets from Employee and
Employee has a 'char *' data member, then 'smith' is probably pointing to
freed memory, since the temporary used in
Manager smith = Manager( ... )
was destroyed after the assignment. Thus, when smith is deleted, you're
most likely deleting freed memory.
This is just a guess, since I don't know the class definition of Manager
nor it's destructor definition. But the segfault is most likely due to
some memory de-allocation problem.
With gdb, if you compiled all of the code with -g3 -O0 (enable debugging
with macro symbol and disable optimization), then you can put a break
point in the destructor and walk through it. I.e.
gdb progname
gdb> break Manager.cpp:1234
gdb> run
Where 1234 is the line number of the Manager's destructor. Then step
through the code with 'next':
gdb> next
GDB will display each line of code as it executes it. So the last line
that is displayed is the one causing the fault. It is possible that the
destructor of Employee may be the problem. Just put a breakpoint on that
too and step through that as well.
--jc
P.S. I just looked at the main function again, and I noticed 'smith' is a
local variable. Are you sure you want to add a variable on the stack to
some container in andCo? That doesn't look right. This could also be the
problem. I recommend going through the process of running gdb to
determine the cause of the segfault. This will tell you both the cause of
the segfault and give you experience with using gdb.
P.P.S. Another good command to know in gdb is 'step'. This steps into a
function, instead of stepping over the function. You can get help with
all of the gdb commands by typing 'help <command name>' at the prompt.
Good luck.
--
Jimen Ching (WH6BRR) jching at flex.com wh6brr at uhm.ampr.org
More information about the Programming
mailing list