Re: Help with error
From: Toros Caglar (tcaglar_at_vt.edu)
Date: 04/22/04
- Next message: Doug Ly: "C# in Linux"
- Previous message: Karl Heinz Buchegger: "Re: Help with error"
- In reply to: Robert W Hand: "Re: Help with error"
- Next in thread: Karl Heinz Buchegger: "Re: Help with error"
- Reply: Karl Heinz Buchegger: "Re: Help with error"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 22 Apr 2004 09:02:24 -0400
Thank you all for your comments. You are right about the Network net being
passed for no reason. I thought I had an access problem in another function
and when I did that, it looked like it fixed it. But apparently it didn't..
I think the main problem was copying that Network net all around the place
by value for no reason. It was for some reason overriding some of the memory
locations and the problem was gone when I removed that.
And I think I am novice enough to not know what you mean by rule of 3.
Also I would really appreciate it if you could point out some of my "bad
habits". I don't think I will be taking any more programming classes to fix
them anytime soon.
Thank you,
Toros
"Robert W Hand" <rwhand@NOSPAMoperamail.com> wrote in message
news:803f80pagsobc650een9fsci7qrgpg0ms6@4ax.com...
> On Wed, 21 Apr 2004 11:08:18 -0400, "Toros Caglar" <tcaglar@vt.edu>
> wrote:
>
> >Here is some of my code. They probably won't compile without the rest of
the
> >code and I didn't want to post all that in here:
>
> I do not see a reason for your observed behavior in the code below. I
> suspect that there must be something wrong with the allocation steps
> or the copying functions that you do not show. Let me make a few
> comments.
>
> >void Population::print(Network net)
>
> I do not see any dependency on Network object since objective_function
> does not do anything with it either. So I am not sure why it is a
> parameter.
>
> Furthermore, if it is a large structure, copying it may be expensive.
> I would think about making the parameter either a constant reference
> or eliminating it.
>
> Also, I see change in the internal state of Population object in the
> function, so I would make it a constant function.
>
> >{
> > cout<<endl<<"The objective functions of the individuals in this
> >population are: "<<endl;
> > for(int pos_ind = 0; pos_ind <= size-1; pos_ind++)
> > {
> > /**/ cout<<pos_ind + 1<<". "<<
> >pop[pos_ind].objective_function(net)<<endl;
> > }
> >}
> >
> >double Individual::objective_function(Network net)
>
> This function appears to calculate the perimeter of the path. Again,
> I do not see any dependency on Network. I would either make it a
> constant reference of eliminate it. And again, I would make the
> member function constant.
>
> >{
> > double cost = 0;
> > for(int pos_node = 0; pos_node <= path_size - 2; pos_node++)
> > {
> > double x_dist = path[pos_node]->x_coor -
path[pos_node+1]->x_coor;
> > double y_dist = path[pos_node]->y_coor -
path[pos_node+1]->y_coor;
> > cost += pow((pow(x_dist,2) + pow(y_dist,2)),0.5);
> > }
> > cost += pow((pow((path[path_size-1]->x_coor - path[0]->x_coor),2) +
> >pow((path[path_size-1]->y_coor - path[0]->y_coor),2)),0.5);
>
> This line is a little to complex for the average reader. I have not
> put it through a compiler or text editor to check the nested pow
> functions and parentheses, but I do not see an obvious error.
>
> > return cost;
> >}
> >
> >** line causes the problem. There is no compilation error or anything.
path
> >is a private member of Individual and is a dynamic array of structs
called
> >Node. as you can see, in objective_function, members of the struct are
being
> >accessed. A call from the main function is successfull. But when the
> >objective function is called from Population class (as in line **),
> >everything in the array path is destroyed. actually I checked and for
some
> >bizarre reason y_coor are still there, members node_index and x_coor are
> >filled with garbage, and they stay like that..
> >
> >Any ideas??
>
> The access that you describe in the above two functions does not
> involve changing values. If there is a lot being placed on the stack,
> it is possible that you have a stack error. Alternatively, you may be
> having a problem with the copying or allocating steps for the class.
>
> If the program is very long, could you post it to a web site? I would
> be willing to look at it if you want to E-mail it to me.
> --
>
> Best wishes,
>
> Bob
- Next message: Doug Ly: "C# in Linux"
- Previous message: Karl Heinz Buchegger: "Re: Help with error"
- In reply to: Robert W Hand: "Re: Help with error"
- Next in thread: Karl Heinz Buchegger: "Re: Help with error"
- Reply: Karl Heinz Buchegger: "Re: Help with error"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|