Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 22nd, 2007, 8:10 AM   #1
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4 Klarre is on a distinguished road
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
Klarre is offline   Reply With Quote
Old Apr 22nd, 2007, 8:23 AM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 321
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Because an int is 4 bytes, not 1?

Why are you using malloc() instead of new?
andro is offline   Reply With Quote
Old Apr 22nd, 2007, 8:36 AM   #3
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4 Klarre is on a distinguished road
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
Klarre is offline   Reply With Quote
Old Apr 22nd, 2007, 8:55 AM   #4
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,292
Rep Power: 5 lectricpharaoh will become famous soon enough
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....
lectricpharaoh is offline   Reply With Quote
Old Apr 22nd, 2007, 9:25 AM   #5
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4 Klarre is on a distinguished road
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
This is not the case though. Even if I changes the data type to char, it will output the same values as for ints. Actually it increments with 4 even if I use a bigger data structure.

Have I misunderstand your reply, or is your statement incorrect?
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Apr 22nd, 2007, 9:43 AM   #6
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,292
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Klarre
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
This is not the case though. Even if I changes the data type to char, it will output the same values as for ints. Actually it increments with 4 even if I use a bigger data structure.

Have I misunderstand your reply, or is your statement incorrect?
My bad. You need to output the pointers, not what they're pointing at- but if you're making them char pointers, you'll need to cast them to void pointers for the output (otherwise, the runtime library will interpret the char pointer as a string, and you'll get gibberish). In other words, you want to print the value of the pointer. This might be more what you're looking for, no?
#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;
}
I get this output:
003556E0
003556E1

00355710
00355714

00355740
00355748
__________________
Java? Rant? Me? Noooo....
lectricpharaoh is offline   Reply With Quote
Old Apr 22nd, 2007, 9:53 AM   #7
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 318
Rep Power: 4 Klarre is on a distinguished road
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
Klarre is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC