![]() |
|
|
|||||||
| Home | Forums | Register | Rules & FAQ | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
Other UNIX.COM Threads You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ftp zip files corruption | colesga | Shell Programming and Scripting | 3 | 08-02-2007 08:22 AM |
| *** glibc detected *** free(): invalid next size (normal): 0x0000000000503e70 *** | vbreddy | High Level Programming | 1 | 04-11-2006 09:18 AM |
| NTFS corruption under w2k but not under suse 9.2 | mickepe | Windows & DOS: Issues & Discussions | 4 | 08-05-2005 05:19 PM |
| data corruption with ftp transfer | malcom | UNIX for Advanced & Expert Users | 12 | 08-04-2003 04:38 AM |
| file corruption | shibz | UNIX for Advanced & Expert Users | 5 | 09-06-2002 08:56 AM |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
*** glibc detected *** double free or corruption: 0x40236ff4 ***
when i try to use the class i wrote, i either get this:
*** glibc detected *** double free or corruption: 0x40236ff4 *** and the proccess exits with an error code of 0; or it segfaults. could someone look at my header file (with imp.) to give me some insight as to why its not working? Code:
#ifndef _DYNAMICARRAY_H
#define _DYNAMICARRAY_H
/*****
Written by Joseph Oldak
May 2006
*****/
using namespace std;
template <class T>
class Dynamicarray
{
public:
Dynamicarray();
Dynamicarray(int s);
~Dynamicarray();
void add(T elem);
T get_val(int loc); // returns first element if element requested does not exist.
void erase(int loc); // location
T * init_ptr();
int size();
private:
int Size;
T * buffer;
T * ptr;
};
template <class T>
Dynamicarray<T>::Dynamicarray()
{
Size = 0;
ptr = init_ptr();
//ptr = new T[Size];
}
template <class T>
Dynamicarray<T>::Dynamicarray(int s)
{
Size = s;
ptr = init_ptr();
//ptr = new T[Size];
}
template <class T>
Dynamicarray<T>::~Dynamicarray()
{
if(ptr != NULL)
{
delete[] ptr;
ptr = NULL;
}
if(buffer != NULL)
{
delete[] buffer;
buffer = NULL;
}
}
template <class T>
T* Dynamicarray<T>::init_ptr()
{
return new (nothrow) T[Size];
}
template <class T>
void Dynamicarray<T>::add(T elem)
{
int sbuffer;
buffer = new T[Size+1];
sbuffer = Size+1;
for (int i = 0; i < Size; i ++)
{
buffer[i] = ptr[i];
}
delete ptr;
Size = sbuffer;
buffer[Size-1] = elem;
ptr = new T[Size];
for(int i = 0; i < Size; i ++)
{
ptr[i] = buffer[i];
}
delete[] buffer;
buffer = NULL;
}
template <class T>
T Dynamicarray<T>::get_val(int loc)
{
if (loc > Size)
{
return ptr[0];
}
return ptr[loc];
}
template <class T>
void Dynamicarray<T>::erase(int loc)
{
if(get_val(loc) != -1)
{
int sbuffer;
buffer = new T[Size-1];
sbuffer = Size-1;
for(int i = 0; i < loc; i++)
{
buffer[i] = ptr[i];
}
for(int i = loc+1; i < Size; i++)
{
buffer[i] = ptr[i];
}
Size = sbuffer;
for(int i = 0; i < Size; i++)
{
ptr[i] = buffer[i];
}
delete[] buffer;
buffer = NULL;
}
}
template <class T>
int Dynamicarray<T>::size()
{
return Size;
}
#endif /* _DYNAMICARRAY_H */
Last edited by norsk hedensk : 05-18-2006 at 08:21 AM. |
| Forum Sponsor | ||
|
|
|
|||
|
i fixed some small typeos i had here and there. the problem is happening right in the constructor as far as i can tell.
so the default constructor Code:
template <class T>
Dynamicarray<T>::Dynamicarray()
{
Size = 0;
ptr = init_ptr();
//ptr = new T[Size];
}
Code:
init_ptr() Code:
new (nothrow) T[Size]; it worked before without templates, specifying the type of the pointer, so i can only assume my usage of templates is incorrect. can anyone shed any light on this? even if i comment out all the code in the constructors except the Size = 0; or = s; it still segfaults. i guess it has to be a problem with the template usage. |
|
|||
|
Is it legal to allocate an array of size zero? I know you can in C, it just bumps the size up to the minimum, but in C++, I'm not so sure.
[edit] Occurred to me that it could be something in T crashing, rather than the template class Last edited by Corona688 : 05-18-2006 at 09:02 AM. |
|
|||
|
Quote:
|
|||
| Google UNIX.COM |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|