Removing Information from an Array

Started by
14 comments, last by ApochPiQ 17 years, 8 months ago
Ok, I made some code to add information to an array when it is constructed, but I don't know how I could remove it when it is destructed. Could someone help? Here is the code:

std::vector<Information*> Infor;

void WINAPI Hooked_InfoConstructor(void)
{
	static Information* Info = NULL;
	__asm mov [Info], ecx;
	__asm call pReal_InfoConstructor;
	try
	{
		if(Info)
		{
			bool bValid = true;
			for(UINT i = 0; i < Infor.size(); i++) bValid = Infor == Info ? false : bValid;
			if(bValid)
			{
				Infor.push_back(Info);
				fileSave ( L"Info Created:   %i %s", Infor.size(), Info->GetName());
			}
		}
	}
	catch ( ... ) { }
}
Advertisement
Why are you messing around with assembly and fake "constructors"?
Eek! The readability of that code ....!
Let me suggest writing it like this:
std::vector<Information*> Infor;void WINAPI Hooked_InfoConstructor(void){	static Information* Info = NULL;	__asm mov [Info], ecx;	__asm call pReal_InfoConstructor;	try	{		if(Info)		{			if (std::find(Infor.begin(), Infor.end(), Info) == Infor.end())			{				Infor.push_back(Info);				fileSave ( L"Info Created:   %i %s", Infor.size(), Info->GetName());			}		}	}	catch ( ... ) { }}
I still can't fathom what you're doing here though, and why you have asm in your code. More explanation please![smile]
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Question:

Why are you copying a count value into a pointer that hasn't allocated any memory? And then, you're calling a routine without passing it the location of Info.

I'm confused already.

Anyways, C++ is nice because it will destruct on the "freeing" of an object. But, because you're using ASM, I don't think that it will do that for you, because I don't think it knows it was made.

So I guess you're going to having to call a destructor in ASM...

(PS: I only know NASM, feel free to correct my ASM syntax)
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Ah, I didn't know about those tags. My friend was helping me out on this, and he suggested using the ASM for it. But pretty much, everytime new Information is brought into play, it is stored into an array, and I need to be able to take it out of the array when it is destructed. If anyone has some suggestions for just using core C++ instead of ASM, please tell me lol.
Is there a specific reason for which you cannot use the following?

class Information {public:   Information() {     if (std::find(Infor.begin(), Infor.end(), this) == Infor.end()) {      Infor.push_back(this);      FileSave ( L"Info Created:   %i %s", Infor.size(), this->GetName());    }   }  ~Information() {     std::vector<Information>::iterator found, last = Infor.begin() +(Infor.size() - 1);    found = std::find(Infor.begin(),Infor.end(),this);    if (found == Infor.end()) return;    std::swap(*found,*last);    Inform.pop_back();  }};
Where is std::find defined? I've never used it before.
#include<algorithm>
Ok, now I am getting these errors:

.\Source.cpp(85) : error C2923: 'std::vector' : 'Infor' is not a valid template type argument for parameter '_Ty'.\Source.cpp(26) : see declaration of 'Infor'.\Source.cpp(85) : error C2440: 'initializing' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'std::_Vector_iterator<_Ty,_Alloc>'with[	_Ty=Information *,	_Alloc=std::allocator<Information *>]and[	_Ty=int,	_Alloc=std::allocator<int>]No constructor could take the source type, or constructor overload resolution was ambiguous.\Source.cpp(86) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion)with[	_Ty=Information *,	_Alloc=std::allocator<Information *>]C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(392): could be 'std::_Vector_iterator<_Ty,_Alloc> &std::_Vector_iterator<_Ty,_Alloc>::operator =(const std::_Vector_iterator<_Ty,_Alloc> &)'	  with	  [		  _Ty=int,		  _Alloc=std::allocator<int>	  ]  while trying to match the argument list '(std::_Vector_iterator<_Ty,_Alloc>, std::_Vector_iterator<_Ty,_Alloc>)'	  with	  [		  _Ty=int,		  _Alloc=std::allocator<int>	  ]  and	  [		  _Ty=Information *,		  _Alloc=std::allocator<Information *>	  ]  .\Source.cpp(87) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion)	  with	  [		  _Ty=int,		  _Alloc=std::allocator<int>	  ]C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\guiddef.h(192): could be 'int operator ==(const GUID &,const GUID &)'C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(189): or 'bool std::_Vector_const_iterator<_Ty,_Alloc>::operator ==(const std::_Vector_const_iterator<_Ty,_Alloc> &) const'  with  [	  _Ty=int,	  _Alloc=std::allocator<int>  ]  while trying to match the argument list '(std::_Vector_iterator<_Ty,_Alloc>, std::_Vector_iterator<_Ty,_Alloc>)'	  with	  [		  _Ty=int,		  _Alloc=std::allocator<int>	  ]  and	  [		  _Ty=Information *,		  _Alloc=std::allocator<Information *>	  ]  .\Source.cpp(90) : error C2451: conditional expression of type 'void' is illegal	  Expressions of type void cannot be converted to other types	  .\Source.cpp(92) : error C2819: type 'std::vector<_Ty>' does not have an overloaded member 'operator ->'	  with	  [		  _Ty=Information *	  ]  did you intend to use '.' instead?	  .\Source.cpp(92) : error C2232: '->std::vector<_Ty>::size' : left operand has 'class' type, use '.'	  with	  [		  _Ty=Information *	  ]  Build log was saved at "file://c:\Documents and Settings\Kyle\My Documents\Visual Studio 2005\Projects\XDS-Core\XDS-Core\Release\BuildLog.htm"	  XDS-Core - 7 error(s), 10 warning(s)


Here is the source, I had to edit up the class and turn them to functions for several reasons:

void PawnConstruct(void){	if(std::find(Infor.begin(), Infor.end(), Info) == Infor.end())	{		Infor.push_back(Info);		fileSave ( L"Info Created:   %i %s", Infor.size(), Info->GetName());	}}void PawnDestroy(void){	std::vector<Infor>::iterator Found, Last = Infor.begin() + (Infor.size() - 1);	Found = std::find(Infor.begin(), Infor.end(), Info);	if(Found == Infor.end()) return;	std::swap(*Found, *Last);	Infor.pop_back();	if(Infor.pop_back())	{		fileSave ( L"Info Destroyed: %i %s", Infor->size(), Info->GetName());	}}
  1. Use std::vector<Information*> instead of std::vector<Infor>.
  2. What's the point of if(Infor.pop_back()) ? None.
  3. Infor->size() should be Infor.size().


This ought to correct all the errors.

This topic is closed to new replies.

Advertisement