Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


deallocing parts of memory

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Thu Jul 03, 2008 5:27 pm    Post subject: deallocing parts of memory Reply with quote

I was wondering if it was possible to deallocate certain parts of memory. For example in an array, it's easy to allocate more memory to it, and deallocating memory, but what about deallocating specific memory, i.e. removing a certain index. Take this example;

Code:

int i;
int array[] = {1, 2, 3};
for(i = 0; i<3; i++) {
   cout<<array[i]<<", ";
}
void DeleteArrayIndex(2);
for(i = 0; i<2; i++) {
   cout<<"\n"<<array[i]<<", ";
}


Example Output wrote:

1, 2, 3
1, 3

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Jul 03, 2008 5:32 pm    Post subject: Reply with quote

If you made a vector or something yes, just keep in mind that it takes a while because it has to move back all of the other pieces to keep it consistent.
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Thu Jul 03, 2008 6:00 pm    Post subject: Reply with quote

The point is to be able to do this without having to use vectors or linked lists.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Jul 03, 2008 6:11 pm    Post subject: Reply with quote

Then use a loop to move back all of the items to fill the space, and then dealloc the remainder off of the end.
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Thu Jul 03, 2008 8:19 pm    Post subject: Reply with quote

memmove(array+(index*sizeof(type)), array+((index+1)*sizeof(type), (count-1)*sizeof(type));
then de/re/unalloc the last index.

_________________
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Thu Jul 03, 2008 9:06 pm    Post subject: Reply with quote

Wait why are you saying the number of bytes to move is the count-1 multiplied by the size of whatever type. When it would really only be the size of that type.

Code:

memmove(array+(index*sizeof(int)), array+((index+1)*sizeof(int)), sizeof(int));


I have a question, when you use memmove does it just replace the memory at the specified index with the memory at the next index, and then the memory that was at the next index is just gone and everything is moved down one (i.e specified index 6's value becomes index 7's value, index 8's memory is moved to where index 7's was)?

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing


Last edited by oib111 on Thu Jul 03, 2008 9:18 pm; edited 1 time in total
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Thu Jul 03, 2008 9:12 pm    Post subject: Reply with quote

actually, it would be (count-index-1)*sizeof(type). It's copying everything after the index into index-1.

EDIT:
couple of mistakes from untested code:
second argument should be index*sizeof(type) instead of index+1
count-index-1 should be count-index for last argument

_________________


Last edited by HalfPrime on Thu Jul 03, 2008 10:30 pm; edited 1 time in total
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Thu Jul 03, 2008 9:28 pm    Post subject: Reply with quote

Wait so if I did this:

Code:

memmove(array+((index-1)*sizeof(int)), array+((index+1)*sizeof(int)), ((count-1)-index)*sizeof(int));


It would move all of the memory from and after index+1 into index-1. And then for all the extra memory (after index+1) it would just fill in (the index number passed gets filled with index+1, index+1 gets filled with index+2, etc)?

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Thu Jul 03, 2008 10:25 pm    Post subject: Reply with quote

It would move everything from index+1 onward into index
If you've got the array
0123456789
and you call it with index 3, it would become
0134567899
then you'd cut off the trailing 9.
array+((index-1)*sizeof(int)) would point at 2
array+((index+1)*sizeof(int)) would point at 3
(count-index)*sizeof(int) is 7 numbers
if you knew what kind of array it was, you could do
memmove(&array[index], &array[index+1], (count-index)*sizeof(int)) and it would be the same thing.
Also, couple of edits in my last post

_________________
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Thu Jul 03, 2008 10:36 pm    Post subject: Reply with quote

Woudn't index+1 in that case point to 4?

Code:

void DeleteArrayIndex(int array, int index) {
   memmove(&array[index], &array[index+1], ((count-1)-index)*sizeof(int)); //elements start at 0 so you have to subtract 1
   free(&array[count-1]);
   count--;
}


I'm not quire sure if that's how I would delete the last element, but that's my guess. If anybody can say otherwise please do.

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites