error during the executing time

Started by
12 comments, last by zip7000 21 years, 8 months ago
hi, the compilation of my program is ok. But when the program is running I get an error.("the memory cannot be read) I can''t understand the following behaviour. here is a function in a class called Ant bool Ant::FindSpatialObject(Garden* g) { int i; int nbNest=Nest::NestCounter; for (i=0; iCompare(g->GetVector())) != SEPARATE ) return true; } return false; } If I call this method I get an error(not by the compiler) Now if I change the i variable by 0, everything works. I checked the i variable : during the executing time a variable takes only 0 as value. So I can''t understand what''s wrong!!! Any suggestion??? ps : ''GetVector()'' return a std::vector ''Compare()'' function needs two SpatialObjects. Ant and Nest are SpatialObjects zip7000
Advertisement
Use [ source ] and [ /source ] (without the spaces) tags, or your code will be difficult to read.


Don''t listen to me. I''ve had too much coffee.
why do you have that for loop in there??
That for loop looks quite non-compilable to me, use source tags...

Johan Ersvik
Johan
It''s bad practice to defer a pointer if you haven''t checked if it is 0. It could be that the ''g'' passed is NULL, in which case that function would crash.
Here's the actual code:
bool Ant::FindSpatialObject(Garden* g){  int i;  int nbNest=Nest::NestCounter;  for (i=0; i< nbNest; i++)  {    if ( (this->Compare(g->GetVector())) != SEPARATE )<br>      return true;<br>  }<br>  return false;<br>}  </pre>  <br><br>All I have to say is, LHTD.<br><br>Later,<br>ZE.      <br><br><font color = "#A8A8A8" size = "1" face = "verdana, tahoma, arial, default-sans-serif">//<a href = "mailto:zealouselixir@yahoo.com">email me.</a>//<a href = "http://hosted.totalim.com/zealouselixir/">zealouselixir software.</a>//<a href = "http://msdn.microsoft.com/library/default.asp">msdn.</a>//<a href = "http://www.gamedev.net/reference/start_here/">n00biez.</a>//<br><a href = "http://www.ojuice.net/">m</a><a href = "http://www.nvidia.com/">i</a><a href = "http://www.gamasutra.com/">s</a><a href = "http://www.gdmag.com/">c</a><a href = "http://www.flipcode.com/">e</a><a href = "http://www.cprogramming.com/">l</a><a href = "http://www.bloodshed.net/">l</a><a href = "http://shilbert.cjb.net/">a</a><a href = "http://www.facehat.com/">n</a><a href = "http://nehe.gamedev.net/">e</a><a href = "http://nexe.gamedev.net/">o</a><a href = "http://www.gamedeveloper.com/">u</a><a href = "http://www.voxelsoft.com/sk/">s </a><a href = "http://www.gamespy.com/">l</a><a href = "http://www.codeproject.com/">i</a><a href = "http://www.sourceforge.com/">n</a><a href = "http://www.codeguru.com/">k</a><a href = "http://www.thinkgeek.com/">s</a><br></font><br><br><SPAN CLASS=editedby>[edited by - zealouselixir on August 15, 2002 5:28:59 PM]</SPAN><br><br><SPAN CLASS=editedby>[edited by - zealouselixir on August 15, 2002 7:22:53 PM]</SPAN>    

[twitter]warrenm[/twitter]

quote:Original post by Zipster
It''s bad practice to defer a pointer if you haven''t checked if it is 0. It could be that the ''g'' passed is NULL, in which case that function would crash.

I disagree with that. I believe that if you always check your pointers if they are NULL before using them that leads to more errors. You should only check if the pointer is NULL if you know that it is either (1.) NULL or (2.) a valid pointer.

Suppose you have a piece of code where you always expect a certain pointer to point to some object. Then there''s a bug that randomly sets that pointer to NULL. Your code would then happily continue to execute, although there''s a bug. Thus you risk to hide bugs and make bugs harder to find.

I don''t want to start a war about this, but it''s worth considering both opinions.


That said, you might still be right that the problem is that the ''g'' is NULL and the code tries to defer it.
hi,

I found. It was a very stupid mistake. I called the function in a loop. So I created lot of objects.

sorry about that!!!
hi,

I found. It was a very stupid mistake. I called the function in a loop. So I created lot of objects.

sorry about that!!!
quote:Suppose you have a piece of code where you always expect a certain pointer to point to some object. Then there''s a bug that randomly sets that pointer to NULL. Your code would then happily continue to execute, although there''s a bug. Thus you risk to hide bugs and make bugs harder to find.


Actually, if I assumed it would always point to a valid object, then there wouldn''t be a NULL-check, the program would crash, and I''d find the error.

I understand your point, and I should clarify what I said. I don''t mean to put a NULL-check every time you defer a pointer - that''s dumb. Was I meant to say was that you only do the check where there''s a possibility the value could be NULL, which in this case, it could be if there was a bug in the code outside the scope of the function that passed it a NULL value. I''m sorry, I thought that was just something understood

This topic is closed to new replies.

Advertisement