• Advertisement
Sign in to follow this  

Removing Information from an Array

This topic is 4229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ( ... ) { }
}

Share this post


Link to post
Share on other sites
Advertisement
Why are you messing around with assembly and fake "constructors"?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}
};

Share this post


Link to post
Share on other sites
Where is std::find defined? I've never used it before.

Share this post


Link to post
Share on other sites
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());
}
}


Share this post


Link to post
Share on other sites
  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.

Share this post


Link to post
Share on other sites
He was assuming you kept your global there. Alternatively you can include an Infor static member in your Information class, like so:


class Information
{
static std::vector<Information*> Infor;
public:
//...
};

// In Information.cpp (assuming that's where you're implementation
// of the class is) you'll need a line like this:
std::vector<Information*> Information::Infor;


Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
He was assuming you kept your global there.


I guess he did keep his global, since he didn't get errors on Infor.begin() (or others).

Share this post


Link to post
Share on other sites
Ok, now I'm reall confused... Anyways, I had the if(Infor.pop_back()) so, when the Information was taken out of the array, it would write it to a log to show it is working properly. Anyways, here is my source now:


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 = std::find(Infor.begin(), Infor.end(), Info);
std::vector<Infor>::iterator Last = Infor.begin() + (Infor.size() - 1);
if(Found == Infor.end()) return;
std::swap(*Found, *Last);
Infor.pop_back();
fileSave ( L"Info Destroyed: %i %s", Infor.size(), Info->GetName());
}



And here are the errors:


.\Source.cpp(86) : error C2923: 'std::vector' : 'Infor' is not a valid template type argument for parameter '_Ty'
.\Source.cpp(26) : see declaration of 'Infor'
.\Source.cpp(86) : 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(87) : error C2923: 'std::vector' : 'Infor' is not a valid template type argument for parameter '_Ty'
.\Source.cpp(26) : see declaration of 'Infor'
.\Source.cpp(87) : 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(88) : 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 *>
]
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 - 5 error(s), 10 warning(s)

Share this post


Link to post
Share on other sites
*cough*

Quote:
Original post by ToohrVyk
  1. Use std::vector<Information*> instead of std::vector<Infor>.



*cough*

Share this post


Link to post
Share on other sites
Hahah, its 5:17 AM, I haven't gone to sleep, wow... I feel stupid now... Here are the errors and all I'm getting now:


------ Build started: Project: XDS-Core, Configuration: Release Win32 ------
Compiling...
Source.cpp
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(44) : warning C4311: 'type cast' : pointer truncation from 'BYTE *' to 'INT'
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(44) : warning C4312: 'type cast' : conversion from 'int' to 'BYTE *' of greater size
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(52) : warning C4311: 'type cast' : pointer truncation from 'BYTE *' to 'INT'
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(52) : warning C4312: 'type cast' : conversion from 'int' to 'BYTE *' of greater size
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(134) : warning C4267: 'argument' : conversion from 'size_t' to 'INT', possible loss of data
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(141) : warning C4267: 'argument' : conversion from 'size_t' to 'INT', possible loss of data
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(142) : warning C4267: 'argument' : conversion from 'size_t' to 'INT', possible loss of data
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(150) : warning C4267: 'argument' : conversion from 'size_t' to 'INT', possible loss of data
c:\documents and settings\kyle\my documents\headers\432core\inc\UnMem.h(151) : warning C4267: 'argument' : conversion from 'size_t' to 'INT', possible loss of data
.\Source.cpp(43) : warning C4996: 'fopen' was declared deprecated
C:\Program Files\Microsoft Visual Studio 8\VC\include\stdio.h(234) : see declaration of 'fopen'
Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
c:\documents and settings\kyle\my documents\headers\432core\inc\UnTemplate.h(114) : warning C4311: 'type cast' : pointer truncation from 'unsigned char *const ' to 'DWORD'
c:\documents and settings\kyle\my documents\headers\432core\inc\UnCache.h(119) : see reference to function template instantiation 'T Align<BYTE*>(const T,INT)' being compiled
with
[
T=BYTE *
]
c:\documents and settings\kyle\my documents\headers\432core\inc\UnTemplate.h(114) : warning C4312: 'type cast' : conversion from 'DWORD' to 'unsigned char *' of greater size
Compiling resources...
Linking...
Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: wchar_t const * __thiscall UObject::GetName(void)const " (__imp_?GetName@UObject@@QBEPB_WXZ)
C:\Documents and Settings\Kyle\My Documents\Visual Studio 2005\Projects\XDS-Core\Release\XDS-Core.dll : fatal error LNK1120: 1 unresolved externals
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 - 2 error(s), 12 warning(s)




//edit: Ok, I just fixed the linking errors, but the warnings are annoying..

Share this post


Link to post
Share on other sites
The warnings indicate that you're handling data incorrectly - or, at best, sloppily. Sometimes there are legitimate reasons to do that sort of thing (in which case there are ways you can explain to the compiler what you intend, and thereby remove the warnings) but usually it's a bad sign. You'll need to post updated code from UnMem.h for us to know what the actual problem is, though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement