The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-17-2006
Registered User
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
*** 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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-17-2006
Registered User
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
after looking through it again, i see that i have to trace my pointer usage a lot better, ill post back when i fix it.
Reply With Quote
  #3 (permalink)  
Old 05-18-2006
Registered User
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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];
}
calls
Code:
init_ptr()
which returns
Code:
new (nothrow) T[Size];
init_ptr() is of type T* , and ptr is of type T*, i dont understand why this isnt working.

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.
Reply With Quote
  #4 (permalink)  
Old 05-18-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 923
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #5 (permalink)  
Old 05-18-2006
Registered User
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by Corona688
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
i thought that might be the problem as well, but it does it even if i set the size to something bigger than zero, it also does it if there is no pointer initiliazation at all.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 02:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102