473,804 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why am I getting an assert failure message with this code?

This function loads info from a binary data file, then loads objects
(currently 23 in the test file). Here is the loading function:

bool UnitDefLoad(cha r* sFile)
{
UnitDefCleanLis t(false); // tested. This works.
int iUnitCount = 0;
UNIT_DEF uData; // Theses objects are read from file
ifstream fin(sFile,ios:: binary);
if (!fin)
{
return false;
} // end if
else
{
fin.read((char* ) &Unit_Def_Node: :Arthur, sizeof
(Unit_Def_Node: :Arthur));
fin.read((char* ) &Unit_Def_Node: :iMonth, sizeof
(Unit_Def_Node: :iMonth));
fin.read((char* ) &Unit_Def_Node: :iDay, sizeof
(Unit_Def_Node: :iDay));
fin.read((char* ) &Unit_Def_Node: :iYear, sizeof
(Unit_Def_Node: :iYear));
fin.read((char* ) &Unit_Def_Node: :Description, sizeof
(Unit_Def_Node: :Description));
fin.read((char* ) &Unit_Def_Node: :iVersion, sizeof
(Unit_Def_Node: :iVersion));
fin.read((char* ) &Unit_Def_Node: :iBuild, sizeof
(Unit_Def_Node: :iBuild));
fin.read((char* ) &iUnitCount, sizeof (iUnitCount));

for (int iCount = 1; iCount < iUnitCount; iCount++)
{
fin.read((char* ) &uData, sizeof (uData));
UnitDefADD(uDat a);
cout << "Number of Unit_Def_Nodes: " << iUnitsDefined << "\n";

}; // end for
};
fin.close();
return true;
};
I believe the problem is the
fin.read((char* ) &uData, sizeof (uData));
in the for loop. I have 23 UNIT_DEF objects saved in the file. After
the 23rd object is loaded, it exits the loop and crashes... if I
comment out the above line, the program works fine. Is there
something wrong with my syntax?
Jul 22 '05 #1
2 1182
"Skywise" <th********@hou ston.rr.com> wrote in message
news:21******** *************** ***@posting.goo gle.com...
This function loads info from a binary data file, then loads objects
(currently 23 in the test file). Here is the loading function:

bool UnitDefLoad(cha r* sFile)
{
UnitDefCleanLis t(false); // tested. This works.
int iUnitCount = 0;
UNIT_DEF uData; // Theses objects are read from file
ifstream fin(sFile,ios:: binary);
if (!fin)
{
return false;
} // end if
else
{
fin.read((char* ) &Unit_Def_Node: :Arthur, sizeof
(Unit_Def_Node: :Arthur));
fin.read((char* ) &Unit_Def_Node: :iMonth, sizeof
(Unit_Def_Node: :iMonth));
fin.read((char* ) &Unit_Def_Node: :iDay, sizeof
(Unit_Def_Node: :iDay));
fin.read((char* ) &Unit_Def_Node: :iYear, sizeof
(Unit_Def_Node: :iYear));
fin.read((char* ) &Unit_Def_Node: :Description, sizeof
(Unit_Def_Node: :Description));
fin.read((char* ) &Unit_Def_Node: :iVersion, sizeof
(Unit_Def_Node: :iVersion));
fin.read((char* ) &Unit_Def_Node: :iBuild, sizeof
(Unit_Def_Node: :iBuild));
fin.read((char* ) &iUnitCount, sizeof (iUnitCount));

for (int iCount = 1; iCount < iUnitCount; iCount++)
{
fin.read((char* ) &uData, sizeof (uData));
UnitDefADD(uDat a);
cout << "Number of Unit_Def_Nodes: " << iUnitsDefined << "\n";

}; // end for
};
fin.close();
return true;
};
I believe the problem is the
fin.read((char* ) &uData, sizeof (uData));
in the for loop. I have 23 UNIT_DEF objects saved in the file. After
the 23rd object is loaded, it exits the loop and crashes... if I
comment out the above line, the program works fine. Is there
something wrong with my syntax?


I don't see anything wrong with your syntax, but the syntax is not where the
problem lies. If you don't supply a complete example, it is hard for anyone
to help you. I doubt that anyone else browsing this group knows what
Unit_Def_Node is, what a UNIT_DEF is, what UnitDefADD does, what the file
contains, etc. The best advice you can get from your current post is "use a
debugger".

--
David Hilsee
Jul 22 '05 #2

"David Hilsee" <da************ *@yahoo.com> wrote in message
news:ob******** ************@co mcast.com...
<snip>
I don't see anything wrong with your syntax, but the syntax is not where the problem lies. If you don't supply a complete example, it is hard for anyone to help you. I doubt that anyone else browsing this group knows what
Unit_Def_Node is, what a UNIT_DEF is, what UnitDefADD does, what the file
contains, etc. The best advice you can get from your current post is "use a debugger".


The second best advice would be to check to see if the calls to read()
actually succeed. The third best would be to use a text file instead of a
binary file so it's easier to follow.

--
David Hilsee
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3598
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a very early draft for a new "assert" syntax: This was inspired in Ruby's assert syntax. I'm not familiar with Ruby at all, so the chances are that this piece of code is broken, but I think the idea is very obvious. In Ruby, assert is simply a...
12
6702
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that assert could be used to start an interactive debugger automatically. How do I realize that on a Linux machine using gcc?
11
4686
by: David | last post by:
I am compiling C++ under both MacOsX and under Bloodshed Dev C++ on Windows XP. Because the console window disappears under Bloodshed when using the standard assert() via #include <cassert> I wrote my own "asserting.h", which goes as follows: ============================================= #include <iostream> #include <cstdlib> #define asserting(e) \ if (!(e)) {\
47
3123
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I can see that it would be poor style to use it for commonly encountered errors, but what about truly exceptional errors that would rarely if ever be encountered?
3
2284
by: sundew | last post by:
so here is the test case: <head> <script type='text/javascript'> // this function simply returns the number of enumerable namespaces(objects) function numNSpaces(){ var num = 0; for(var i in window) ++num;
8
528
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could show me the actual source code of assert() function in assert.h header file. I need to write the assert function that prints out error message when the expression is wrong. For eg.,
29
2526
by: mailforpr | last post by:
Sometimes, I can't think of any good reason why I should have the program's logic thrown an exception. Except for catching the exception and printing "Uh, oh" to the screen. I also think that in most cases there's simply no way to handle an exception properly, because what can one do about an integer overflow? Reassign values? Restart the program? The problem will still be existing. I think that an integer overflow is not an exception, but...
10
1322
by: Paul Rubin | last post by:
So I have some assert statements in my code to verify the absence of some "impossible" conditions. They were useful in debugging and of course I left them in place for "real" runs of the program. Umpteen hours into a run, an assertion failed, and of course since failure was "impossible", I didn't catch the exception so the whole program crashed. I don't know what I'd have done with the exception anyway, since it would have had to be...
30
2199
by: Tomás Ó hÉilidhe | last post by:
In C89, do we have to pass an int as an argument to assert? I've got code at the moment that does an assertion on pointer, e.g.: assert(p); , but I'm wondering if I should change that to: assert(0 != p);
32
2096
by: bingfeng | last post by:
hello, please see following two code snatches: 1. int foo (const char* some) { assert(some); if (!some) { return -1; //-1 indicates error }
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.