Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Erzengeldeslichtes

Memcpy and altering addresses.

This topic is 5382 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

I have a couple functions that are inteded to dynamically size arrays. I can do it fine if I use a for loop, however I''m trying to convert these functions over to memcpy. However, I''m managed to confuse myself with this function:
void RemoveFromArray(int* &Array, int &size, int Slot)
{
	size--;
	int* TempArray = new int[size];
	if(Array) 
	{
		int PointerSize = sizeof(int);
		int CopySize = PointerSize * Slot;
		int RemainingSize = (PointerSize * (size+1)) - (CopySize+PointerSize);
		memcpy(TempArray, Array, CopySize);
		int* TempArrayPos = TempArray+CopySize;
		int* ArrayPos = Array+CopySize;
		ArrayPos += PointerSize;
		memcpy(TempArrayPos, ArrayPos, RemainingSize);
		TempArray[Slot];
		delete[]Array;
	}
	Array = TempArray;
}
I''ve fount that when ArrayPos is created (which was because it wasn''t working in line so I moved it out to a variable so I could see what was happening) doesn''t point to the given slot. I don''t know where it points, but it doesn''t point at the given slot. What am I doing wrong in the calculations? memcpy(TempArray, Array, CopySize); works fine, and copies over all the slots prior to the given slot. So why doesn''t looking at the address of Array+CopySize show me the given slot?

Share this post


Link to post
Share on other sites
Advertisement
Hello Erzengeldeslichtes,

If you copy the array the the first slot is at array (use array name is a address of first slot) so if the arry size is 2 array + 2 points outside the array

so Array+CopySize is past your arrays end this could be a bad thing if you set it to something. If you want the last element in the arrary it should be Array+(CopySize-1)

array => first slot
array + 1 => 2nd slot
array + 2 => 3rd slot

int temp[5] = {1,2,3,4,5};

*temp => gives value 1
*(temp+1) give value 2
*(temp+2) gives value 3
*(temp+3) gives value 4
*(temp+4) gives value 5

any use of just the name of an array give you the first element of the arrary, and all C/C++ array are zero( 0 ) base arrays.

Hope that help you understand

Lord Bart

Share this post


Link to post
Share on other sites
The language which you are using already has dynamically sized arrays. Don''t reimplement them in an inferior way. Use them.

But, assuming your function type is immutable, I would think you''d want something like (untested etc.)

void RemoveFromArray(int*& input, int& size, int index)
{
if(NULL == input || 0 == size || index >= size) { throw std::logic_error("@_@"); }
int* output(new int[size - 1]);
memcpy(output.get(), input, index * sizeof(int));
memcpy(output.get() + index, input + index + 1, (size - index - 1) * sizeof(int));
delete[] input;
--size;
input = output;
}

The problem I think you''re having is that you''re misunderstanding pointer arithmetic. Pointers are not completely stupid. When you say, for instance, (TempArray + 4), that gives you a pointer not to four bytes from TempArray , but four integers from TempArray. That is to say, you don''t need to scale offsets by the size of the object yourself -- so you don''t need to say TempArray + CopySize or Array + CopySize. You just need to specify the offsets themselves -- in this case, Array + Slot or TempArray + Slot.

Share this post


Link to post
Share on other sites
Watch this!

void RemoveFromArray(int* &array, int &size, int slot)
{
int* temp = new int[size-1];
std::copy(array, array+slot, temp);
std::copy(array+slot+1, array+size, temp+slot);
array = temp;
size--;
}



How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
You''ve leaked the input array, which seems undesirable, and you''ve not used memcpy, which I assumed was one of the initial criteria.

Share this post


Link to post
Share on other sites
quote:
Original post by DrPizza
You''ve leaked the input array, which seems undesirable, and you''ve not used memcpy, which I assumed was one of the initial criteria.


Indeed I have leaked the input array. *shrug* it was a 20 second code job; I''d be surprised if that was the only error I made. As for memcpy, if it''s really a requirement (don''t see why it would be), it''s fairly simple to go from one to the other.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Thank you all, I was assuming that when I add 1 to a pointer would be 1 byte rather than 1 slot. Thanks for correcting me. That fixes my problems.

As for why I want to do dynamic arrays in an inferior way: I want to be able to do something before using the standard libraries. I also don''t like the standard libraries because sometimes they give odd results and I can''t step through them to see exactly where I inputted an error into them. I can do that with functions I''ve made. I''m using memcpy because it''s as low level (I think) as I can go without getting into assembly.

Share this post


Link to post
Share on other sites
quote:
Original post by Erzengeldeslichtes
I also don''t like the standard libraries because sometimes they give odd results and I can''t step through them to see exactly where I inputted an error into them.

Of course you can. If I had a quarter for every time I''ve stepped into a STL function to see what was happening, I''d have enough money to hire someone else to debug my code.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
I said I can't. Whenever I try on my laptop it asks me to locate the cpp for the file and I can't find it anywhere, so I end up in disassembly code. When I do it on my desktop I can step through it fine. But as you might imagine, I don't have my desktop with me all the time like I do with my laptop.

[edited by - Erzengeldeslichtes on September 23, 2003 11:36:17 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!