Home » Coding » c++ » Assert vector program terminates
Assert vector program terminates [message #17348] Sat, 06 October 2007 10:58 Go to next message
hyderabadblues  is currently offline 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 Go to previous messageGo to next message
alfps  is currently offline 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 #17355 is a reply to message #17349 ] Sat, 06 October 2007 11:59 Go to previous messageGo to next message
Ben Pfaff  is currently offline Ben Pfaff
Messages: 5
Registered: August 2007
Junior Member
"Alf P. Steinbach" <alfps@start.no> writes:

> * hyderabadblues:
>> 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.

It's really weird to use short as the type for tU8, since we
know that short has more than 8 bits.
--
"While the Melissa license is a bit unclear, Melissa aggressively
encourages free distribution of its source code."
--Kevin Dalley <kevin@seti.org>
Re: Assert vector program terminates [message #17356 is a reply to message #17348 ] Sat, 06 October 2007 12:07 Go to previous messageGo to next message
Erik-wikstrom  is currently offline 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 #17358 is a reply to message #17355 ] Sat, 06 October 2007 12:09 Go to previous messageGo to next message
Erik-wikstrom  is currently offline Erik-wikstrom
Messages: 412
Registered: July 2007
Senior Member
On 2007-10-06 17:59, Ben Pfaff wrote:
> "Alf P. Steinbach" <alfps@start.no> writes:
>
>> * hyderabadblues:
>>> 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.
>
> It's really weird to use short as the type for tU8, since we
> know that short has more than 8 bits.

Actually, on a 7-bit machine a short could be 7 bits as well. All we
know is that short has at least as many bits as a signed char.

--
Erik Wikström
Re: Assert vector program terminates [message #17359 is a reply to message #17355 ] Sat, 06 October 2007 12:24 Go to previous messageGo to next message
hyderabadblues  is currently offline hyderabadblues
Messages: 3
Registered: September 2007
Junior Member
On Oct 6, 5:59 pm, Ben Pfaff <b...@cs.stanford.edu> wrote:
> "Alf P. Steinbach" <al...@start.no> writes:
>
> > * hyderabadblues:
> >> 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.
>
> It's really weird to use short as the type for tU8, since we
> know that short has more than 8 bits.
> --
> "While the Melissa license is a bit unclear, Melissa aggressively
> encourages free distribution of its source code."
> --Kevin Dalley <ke...@seti.org>

I am working for a embedded systems company, so to try out something I
use visual studio, above defines are just mine for trials.
Re: Assert vector program terminates [message #17361 is a reply to message #17358 ] Sat, 06 October 2007 12:27 Go to previous messageGo to next message
Jerry Coffin  is currently offline Jerry Coffin
Messages: 135
Registered: August 2007
Senior Member
In article <CqONi.10560$ZA.7038@newsb.telia.net>, Erik-
wikstrom@telia.com says...

[ ... ]

> Actually, on a 7-bit machine a short could be 7 bits as well. All we
> know is that short has at least as many bits as a signed char.

A signed char must support a range from at least -127 to +127. A short
must support a range from at least -32767 to +32767. Each signed type
must also occupy the same amount of storage as its corresponding
unsigned type, and an unsigned char must support a range from at least 0
through 255. An unsigned short must support a ranage from at least 0
through 65535.

IOW, in C or C++ a char or unsigned char must occupy least 8 bits, and a
short or unsigned short must occupy at least 16 bits.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Re: Assert vector program terminates [message #17363 is a reply to message #17361 ] Sat, 06 October 2007 13:22 Go to previous messageGo to next message
Erik-wikstrom  is currently offline Erik-wikstrom
Messages: 412
Registered: July 2007
Senior Member
On 2007-10-06 18:27, Jerry Coffin wrote:
> In article <CqONi.10560$ZA.7038@newsb.telia.net>, Erik-
> wikstrom@telia.com says...
>
> [ ... ]
>
>> Actually, on a 7-bit machine a short could be 7 bits as well. All we
>> know is that short has at least as many bits as a signed char.
>
> A signed char must support a range from at least -127 to +127. A short
> must support a range from at least -32767 to +32767. Each signed type
> must also occupy the same amount of storage as its corresponding
> unsigned type, and an unsigned char must support a range from at least 0
> through 255. An unsigned short must support a ranage from at least 0
> through 65535.
>
> IOW, in C or C++ a char or unsigned char must occupy least 8 bits, and a
> short or unsigned short must occupy at least 16 bits.

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?

--
Erik Wikström
Re: Assert vector program terminates [message #17366 is a reply to message #17363 ] Sat, 06 October 2007 13:41 Go to previous message
Jerry Coffin  is currently offline 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.
Previous Topic:MinGW libraries
Next Topic:c++ tips
Goto Forum:
  


Current Time: Fri May 16 02:45:58 EDT 2008

Total time taken to generate the page: 0.92149 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software