| 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?
|
Posted: Thu Jul 03, 2008 5:27 pm Post subject: deallocing parts of memory |
|
|
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 |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jul 03, 2008 5:32 pm Post subject: |
|
|
| 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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 03, 2008 6:00 pm Post subject: |
|
|
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 |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jul 03, 2008 6:11 pm Post subject: |
|
|
| 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 |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Thu Jul 03, 2008 8:19 pm Post subject: |
|
|
memmove(array+(index*sizeof(type)), array+((index+1)*sizeof(type), (count-1)*sizeof(type));
then de/re/unalloc the last index. _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 03, 2008 9:06 pm Post subject: |
|
|
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 |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Thu Jul 03, 2008 9:12 pm Post subject: |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 03, 2008 9:28 pm Post subject: |
|
|
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 |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Thu Jul 03, 2008 10:25 pm Post subject: |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Jul 03, 2008 10:36 pm Post subject: |
|
|
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 |
|
 |
|