Skip to main content
gamedev.net
🔒 Locked

incrementing byte array pointer - strange result

Started by papa May 12, 2004 at 6:57 AM 5 replies 400+ views
Original Post
papa
papa
Hi. I am trying to understand this bit of code an one thing is unclear for me.
unsigned char *arr;
arr = new unsigned char[6];
int var;		// int is 4 bytes

//


unsigned char *tmp = arr;
tmp += sizeof(var);	// increment arr byte array by 4 bytes. sizeof(int)

			// the pointer now is pointing at position 4  ie. arr[3]

	
int a = int(arr);	// What is this value ---->   114633960  Why so big?

int b = int(tmp);	// and this value     ---->   114633964


	
int incrementedby = int(tmp-arr); // incrementedby = 4 which is ok as I incremented by four bytes

Ok. Now what exactly int(arr) does. It looks to me as its does some kind of casting doesnt it? But why those big numers? Why this is different resutl int result = int(tmp-arr); than this int result = int(tmp) - int(arr); [edited by - papa on May 12, 2004 8:04:41 AM]
Archimage
Archimage
Well your array hasn't been initialised to anything for starters, so its looking at random bytes. Secondly, I assume you meant to read the contents of the int starting at tmp, which as you pointed out is 4 bytes, yet you only have 2 bytes left out of an array[6]

Finally, unless I'm mistaken, you are looking at the (int) of the pointers' addresses in memory, not the contents.

Archimage


[edited by - Archimage on May 12, 2004 8:07:55 AM]
papa
papa
Well I am not interested in the values inside the array. Regardless if I intialize the arr or not the result is same in terms of what I am interested in. Have a look I am not retrieving values anywhere. Its just a simple quick exampl to save some GameDev disk space

I am interested in the pointer incrementing and why int(arr) gives me those big numbers . What are they?
Why this strange casting? is it actually casting or something else?

[edited by - papa on May 12, 2004 8:12:19 AM]
Archimage
Archimage
I hope you are not assuming that all new allocations occur from 0 in system memory? You ask it to allocate some memory (via new) and it finds somewhere to allocate it in the heap. It then passes you the address of that block in the pointer. You shouldn't care what the actual value is, just the relative details (as in arr+4 = 4 bytes (unsigned chars) in from the start)

Archimage


[edited by - Archimage on May 12, 2004 8:14:25 AM]
papa
papa
No, I am only assuming that every newly created array has a pointer at position zero. Therefore *arr prints same what arr[0] would print. whereas if you do arr+=4 then *arr would print the contents of arr[4]

I was just confused about the strange int(arr) value.
So you're saying that the value is the address?

[edited by - papa on May 12, 2004 8:42:09 AM]
bobstevens
bobstevens
Yes, it''s the address. You have to dereference it to get the value. int(arr) versus int(*arr).
papa
papa
ok. Thanks all :o)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.