![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4
![]() |
Unexpected offset?
I don't really understand the output of this piece of code. I expected the second row to print just 0x1 higher value than the first one. But it prints 0x4 higher. Why?
Thanks for your help! /Klarre #include <iostream>
#include <cstdlib>
int main()
{
int* data = (int*)malloc(sizeof(int));
std::cout << (&data + 0) << std::endl;
std::cout << (&data + 1) << std::endl;
free(data);
return 0;
}Ouput: 0x22ff74 0x22ff78
__________________
http://www.klarre.se |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Because an int is 4 bytes, not 1?
Why are you using malloc() instead of new? |
|
|
|
|
|
#3 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4
![]() |
That seems pretty logic. So I guess it is impossible to start read on memory addresses that are not 4 bytes aligned then.
This is a simplified and modified version of a piece of code in a memory manager I am currently working on. I am using malloc since I have been overloading the new operator in the manager project and did not wanted this code to be memory managed by my memory manager. Therefor I chose to use malloc. In this case, of course, it has worked perfectly with new! ![]() Thanks for your reply!
__________________
http://www.klarre.se |
|
|
|
|
|
#4 |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,292
Rep Power: 5
![]() |
Pointer math is scaled to the size of the item being pointed at. This is why pointers need to have a non-void type before you can do any pointer math.
It works like this because typically, the programmer will want to move in units of the pointed-at quantity. For example, if you're pointing at ints, you want to move to the next or previous int by incrementing or decrementing the pointer. You could cast it to an int, do math, and cast it back (though this won't be portable). A safer option would be to cast to pointer to char, then do math, then cast back. However, some (many) architectures will perform much slower for non-aligned memory accesses, and a few will even generate some kind of fault, so be careful about these kinds of stunts.
__________________
Java? Rant? Me? Noooo.... |
|
|
|
|
|
#5 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4
![]() |
lectricpharaoh - If i interpret you answer correct, you mean that if I, in my example above, changes the int to a char instead, the output would be this:
Output: 0x22ff74 0x22ff76 Have I misunderstand your reply, or is your statement incorrect?
__________________
http://www.klarre.se |
|
|
|
|
|
#6 | |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,292
Rep Power: 5
![]() |
Quote:
#include <iostream>
#include <cstdlib>
int main()
{
char *charData = (char*)malloc(sizeof(char));
int *intData = (int*)malloc(sizeof(int));
long long *longlongData = (long long*)malloc(sizeof(long long));
std::cout << (void *)(charData + 0) << std::endl;
std::cout << (void *)(charData + 1) << std::endl << std::endl;
std::cout << intData + 0 << std::endl;
std::cout << intData + 1 << std::endl << std::endl;
std::cout << longlongData + 0 << std::endl;
std::cout << longlongData + 1 << std::endl << std::endl;
free(charData);
free(intData);
free(longlongData);
return 0;
}003556E0 003556E1 00355710 00355714 00355740 00355748
__________________
Java? Rant? Me? Noooo.... |
|
|
|
|
|
|
#7 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4
![]() |
Ah, that made sense. I shouldn't have use the & operator of course. Now it is clear. Thanks for your help!
/Klarre
__________________
http://www.klarre.se |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem with tokenizing | rwm | C++ | 36 | Feb 20th, 2007 2:41 AM |
| unexpected end of file found | Ducky | C++ | 2 | May 3rd, 2006 12:12 PM |
| Little help | whoawhoayoyo | Assembly | 8 | Apr 18th, 2006 8:10 PM |
| unexpected close | teencoder | C++ | 11 | Oct 21st, 2005 5:13 AM |