Home » Coding » c++ » Assert vector program terminates
| Assert vector program terminates [message #17348] |
Sat, 06 October 2007 10:58  |
hyderabadblues Messages: 3 Registered: September 2007 |
Junior Member |
|
|
Following is part of my program that I am trying to code to simluate
folder information which contains mp3 files.
At the end of main funcion there is assert, I have tried to debug the
program , what I found is resize() call was successful, but when main
terminates there is an assert in the vectors destructor.
#include <iostream>
#include <string>
#include <vector>
typedef int tU32;
typedef bool tBool;
typedef short tU8;
typedef void tVoid;
typedef char * tcString;
#define NULL 0
using namespace std;
enum tenFileType {
FI_EN_FOLDER = 0UL,
FI_EN_FILE = 1UL,
FI_EN_PLAYLIST = 2UL
};
enum tenGenre {
FI_EN_ROOT = 0UL,
FI_EN_PLAYLIST_1 = 1UL,
FI_EN_ARTIST = 2UL,
FI_EN_ALBUM = 3UL,
FI_EN_GENRE = 4UL,
FI_EN_TRACK = 5UL,
FI_EN_COMPOSER = 6UL,
FI_EN_AUDIOBOOK = 7UL,
FI_EN_PODCAST = 8UL,
FI_EN_UNKNOWNACTIVE = 9UL
};
enum FileAttributes
{
FILETYPE_FOLDER,
FILETYPE_FILE,
FILETYPE_ARTIST,
FILETYPE_ALBUM
};
struct trFolder{
tU32 u32Id;
tcString name;
};
struct trTime{
tU8 u8NoOfHrs;
tU8 u8NoOfMin;
tU8 u8NofSec;
};
struct trId3Data
{
tenFileType m_enFileType;
tenGenre m_enGenre;
char * m_szArtistName;
char * m_szFileName;
trTime m_trPlayTime;
trId3Data( )
{
m_szArtistName = NULL;
m_szFileName = NULL;
}
~trId3Data( )
{
if( m_szArtistName != NULL )
delete m_szArtistName;
if( m_szFileName!= NULL )
delete m_szFileName;
}
trId3Data( const trId3Data& rftrId3Data)
{
}
trId3Data(trId3Data& rftrId3Data)
{
m_enFileType = rftrId3Data.m_enFileType;
m_enGenre = rftrId3Data.m_enGenre;
vCopyStirng( m_szArtistName, rftrId3Data.m_szArtistName);
vCopyStirng( m_szFileName, rftrId3Data.m_szFileName );
m_trPlayTime.u8NoOfHrs = rftrId3Data.m_trPlayTime.u8NoOfHrs;
m_trPlayTime.u8NoOfMin = rftrId3Data.m_trPlayTime.u8NoOfMin;
m_trPlayTime.u8NofSec = rftrId3Data.m_trPlayTime.u8NofSec;
}
tVoid vCopyStirng( char *&dest , char *&src )
{
dest = (char*)malloc(strlen(src));
strcpy( dest, src );
}
tVoid vsetFileType( tenFileType enFileType )
{
m_enFileType = enFileType;
}
tVoid vsetGenre( tenGenre enGenre )
{
m_enGenre = enGenre;
}
tVoid vsetArtistName( char* ArtistName )
{
vCopyStirng( m_szArtistName, ArtistName );
}
tVoid vsetFilename(char * fileName)
{
vCopyStirng( m_szFileName, fileName );
}
tVoid vsetPlayTime( trTime &trPlayTime)
{
m_trPlayTime.u8NofSec = trPlayTime.u8NofSec;
m_trPlayTime.u8NoOfMin = trPlayTime.u8NoOfMin;
m_trPlayTime.u8NoOfHrs = trPlayTime.u8NoOfHrs;
}
};
/*!
* \fn trFolderInfo
* \brief
* Represents Basic structure of a folder
* \note
* \bug
* no bugs known
* \todo
* -> Review
* \version
* 1.0 - Initial
******/
struct trFolderInfo
{
tU32 u32Id;
char* name;
struct trFolderInfo *parentdir;
std::vector<trId3Data> fileList;
std::vector<trFolderInfo> subdir;
trFolderInfo()
{
name = NULL;
}
~trFolderInfo()
{
if( name != NULL )
delete name;
}
tVoid vCopyStirng( char *&dest , char *&src )
{
dest = (char*)malloc(strlen(src));
strcpy( dest, src );
}
tVoid vsetFolderName(char * FolderName)
{
vCopyStirng( name, FolderName );
}
} ;
void main()
{
vector<trId3Data> fileList;
fileList.resize(10);
}
|
|
|
| Re: Assert vector program terminates [message #17349 is a reply to message #17348 ] |
Sat, 06 October 2007 11:19   |
alfps Messages: 315 Registered: July 2007 |
Senior Member |
|
|
* hyderabadblues:
> Following is part of my program that I am trying to code to simluate
> folder information which contains mp3 files.
> At the end of main funcion there is assert, I have tried to debug the
> program , what I found is resize() call was successful, but when main
> terminates there is an assert in the vectors destructor.
>
>
> #include <iostream>
> #include <string>
> #include <vector>
>
> typedef int tU32;
> typedef bool tBool;
> typedef short tU8;
> typedef void tVoid;
It's not a good idea to obscure code by using names such as tVoid
instead of standard void.
> typedef char * tcString;
> #define NULL 0
Don't #define NULL or any other library identifier when using standard
libraries, that's explicitly verboten, yielding Undefined Behavior.
Instead, #include <cstddef>.
> using namespace std;
>
> enum tenFileType {
> FI_EN_FOLDER = 0UL,
> FI_EN_FILE = 1UL,
> FI_EN_PLAYLIST = 2UL
> };
>
> enum tenGenre {
> FI_EN_ROOT = 0UL,
> FI_EN_PLAYLIST_1 = 1UL,
> FI_EN_ARTIST = 2UL,
> FI_EN_ALBUM = 3UL,
> FI_EN_GENRE = 4UL,
> FI_EN_TRACK = 5UL,
> FI_EN_COMPOSER = 6UL,
> FI_EN_AUDIOBOOK = 7UL,
> FI_EN_PODCAST = 8UL,
> FI_EN_UNKNOWNACTIVE = 9UL
> };
>
> enum FileAttributes
> {
> FILETYPE_FOLDER,
> FILETYPE_FILE,
> FILETYPE_ARTIST,
> FILETYPE_ALBUM
> };
In general (style issue), don't use all uppercase names except for macros.
One reason is that that helps you avoid name collisions.
Another reason is that all uppercase names are very hard on the eardrums
(shouting).
> struct trFolder{
> tU32 u32Id;
> tcString name;
>
> };
It's a good idea to use std::string instead of char*.
> struct trTime{
> tU8 u8NoOfHrs;
> tU8 u8NoOfMin;
> tU8 u8NofSec;
> };
>
> struct trId3Data
> {
> tenFileType m_enFileType;
> tenGenre m_enGenre;
> char * m_szArtistName;
> char * m_szFileName;
> trTime m_trPlayTime;
>
> trId3Data( )
> {
> m_szArtistName = NULL;
> m_szFileName = NULL;
> }
>
> ~trId3Data( )
> {
> if( m_szArtistName != NULL )
> delete m_szArtistName;
>
> if( m_szFileName!= NULL )
> delete m_szFileName;
> }
You don't need to check for NULL, just delete.
But you don't need to delete if instead you use std::string.
> trId3Data( const trId3Data& rftrId3Data)
> {
> }
Not a good idea to define copy construction that doesn't copy.
Will do Bad Things.
> trId3Data(trId3Data& rftrId3Data)
> {
> m_enFileType = rftrId3Data.m_enFileType;
> m_enGenre = rftrId3Data.m_enGenre;
> vCopyStirng( m_szArtistName, rftrId3Data.m_szArtistName);
> vCopyStirng( m_szFileName, rftrId3Data.m_szFileName );
> m_trPlayTime.u8NoOfHrs = rftrId3Data.m_trPlayTime.u8NoOfHrs;
> m_trPlayTime.u8NoOfMin = rftrId3Data.m_trPlayTime.u8NoOfMin;
> m_trPlayTime.u8NofSec = rftrId3Data.m_trPlayTime.u8NofSec;
> }
Not a good idea to define both forms of copy constructor.
Besides, you don't need this if you use std::string.
> tVoid vCopyStirng( char *&dest , char *&src )
> {
> dest = (char*)malloc(strlen(src));
> strcpy( dest, src );
> }
This is very incorrect.
It will do bad things.
But you don't need this if you use std::string.
> tVoid vsetFileType( tenFileType enFileType )
> {
> m_enFileType = enFileType;
> }
>
> tVoid vsetGenre( tenGenre enGenre )
> {
> m_enGenre = enGenre;
> }
>
> tVoid vsetArtistName( char* ArtistName )
> {
> vCopyStirng( m_szArtistName, ArtistName );
> }
>
> tVoid vsetFilename(char * fileName)
> {
> vCopyStirng( m_szFileName, fileName );
> }
>
> tVoid vsetPlayTime( trTime &trPlayTime)
> {
> m_trPlayTime.u8NofSec = trPlayTime.u8NofSec;
> m_trPlayTime.u8NoOfMin = trPlayTime.u8NoOfMin;
> m_trPlayTime.u8NoOfHrs = trPlayTime.u8NoOfHrs;
> }
Why not let trTime support assignment?
> };
>
> /*!
> * \fn trFolderInfo
> * \brief
> * Represents Basic structure of a folder
> * \note
> * \bug
> * no bugs known
> * \todo
> * -> Review
> * \version
> * 1.0 - Initial
> ******/
> struct trFolderInfo
> {
> tU32 u32Id;
> char* name;
> struct trFolderInfo *parentdir;
> std::vector<trId3Data> fileList;
> std::vector<trFolderInfo> subdir;
>
> trFolderInfo()
> {
> name = NULL;
> }
>
> ~trFolderInfo()
> {
> if( name != NULL )
> delete name;
> }
Use std::string.
> tVoid vCopyStirng( char *&dest , char *&src )
> {
> dest = (char*)malloc(strlen(src));
> strcpy( dest, src );
> }
Use std::string.
>
> tVoid vsetFolderName(char * FolderName)
> {
> vCopyStirng( name, FolderName );
> }
Use std::string.
> } ;
>
> void main()
'main' must have result type 'int'.
> {
> vector<trId3Data> fileList;
> fileList.resize(10);
> }
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| |
| Re: Assert vector program terminates [message #17356 is a reply to message #17348 ] |
Sat, 06 October 2007 12:07   |
Erik-wikstrom Messages: 412 Registered: July 2007 |
Senior Member |
|
|
On 2007-10-06 16:58, hyderabadblues wrote:
> Following is part of my program that I am trying to code to simluate
> folder information which contains mp3 files.
> At the end of main funcion there is assert, I have tried to debug the
> program , what I found is resize() call was successful, but when main
> terminates there is an assert in the vectors destructor.
Using a debugger you would notice that the problem occurs when you try
to delete m_szArtistName in ~trId3Data().
> tVoid vCopyStirng( char *&dest , char *&src )
> {
> dest = (char*)malloc(strlen(src));
^^^^^^^
> strcpy( dest, src );
> }
> tVoid vCopyStirng( char *&dest , char *&src )
> {
> dest = (char*)malloc(strlen(src));
^^^^^^
> strcpy( dest, src );
> }
Memory allocated with malloc must be deallocated with free, (likewise,
memory allocated with new must be deallocated with delete). In C++
programs new should be used unless you need to allocate memory
deallocated by libraries written in C (which is bad design, so you
probably should not use such libraries any way).
--
Erik Wikström
|
|
| | | | |
| Re: Assert vector program terminates [message #17366 is a reply to message #17363 ] |
Sat, 06 October 2007 13:41  |
Jerry Coffin Messages: 135 Registered: August 2007 |
Senior Member |
|
|
In article <5vPNi.10564$ZA.7046@newsb.telia.net>, Erik-
wikstrom@telia.com says...
[ ... ]
> I forgot about good old C. This makes me wonder however, why is no
> reference made to the relevant sections of the C standard when
> discussing the integer types instead of hiding that away down in section
> 18.2.2?
There is, at least indirectly -- in 3.9.1 footnote 39 mentions that
int's most be "large enough to contain any value in the range INT_MIN
and INT_MAX as defined in the header <climits>"
That's about it though, and an extremely minimal reference (at best) it
is too...
--
Later,
Jerry.
The universe is a figment of its own imagination.
|
|
|
Goto Forum:
Current Time: Fri May 16 02:38:39 EDT 2008
Total time taken to generate the page: 0.40213 seconds |