Jump to content
  • Advertisement

Archived

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

Some Guy

Pointers, memory, and questions

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

Say I have a pointer to video RAM. int * VRAM = 0xa000; Now, to draw a pixel to memory, you have a basic function to do so by manipulating that pointer. inline void PlotPixel( int x, int y, char color ) { VRAM[ x + (y * VIDEOBUFFER_WIDTH)] = color; } ------- Now, look back at VRAM. I''m assigning a pointer to a hexadecimal value. Converting 0xa000 to decimal, you get 40960. Couldn''t the pointer be initialized with THAT value, and still point to video RAM? Like this: int * VRAM = 40960; Doesn''t seem right, does it? Yeah, that''s where I''m confused. So, I guess this question goes into stacks, memory, registers... you know the deal. Please help me out here! ------- Next, I''m looking at the function for plotting a pixel. VRAM is indexed and manipulated, like an array. Kinda silly, but can ALL pointers be indexed like that, with the []''s and everything. Also, couldn''t VRAM be declared as a C++ reference rather than a pointer or const pointer?

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Some Guy
Also, couldn''t VRAM be declared as a C++ reference rather than a pointer or const pointer?


Definitely couldn''t be declared as a reference, references are only aliases for another variable. Pointers point to memory, and in this case, you are doing low-level hardware access via memory. Pointers are the only way you can access the VRAM, well, since, pointers point to memory.

As for the other questions, I wonder the same, so I am awaiting the answers as anxiously as you are Good questions, you are truely wanting to learn how everything works.

E-mail: i8degrees@cox-internet.com
AIM: i8 degrees

Share this post


Link to post
Share on other sites
quote:
Original post by Some Guy
Converting 0xa000 to decimal, you get 40960. Couldn''t the pointer be initialized with THAT value, and still point to video RAM?

Sure. However, 40960 is a hard-to-recognize "magic number" while 0xa000 suggest a special location in memory.

quote:

...can ALL pointers be indexed like that, with the []''s and everything.

Yes. Just that if you''re messing with an arbitrary pointer and you haven''t allocated memory then you''ll be trampling unsafely. Since 0xa000 is a special memory location, it''s okay to do it here. Modern operating systems wont allow you to, though, unless you have ring0 access.

quote:

Also, couldn''t VRAM be declared as a C++ reference rather than a pointer or const pointer?

Absolutely not. You can''t dereference a reference (imagine that!), so operator [] would result in a compile-time error.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
It doesn''t matter if you use decimal or hexadecimal constants. As long as they are equal in value, absolutely the same code will be generated.

Yes, all pointers can be indexed using [] and manipulated as arrays. If I''m not mistaken, the array variable is actually a const pointer to its first element:

int array[];
const int *array;

would produce the same results when used, for example, in a parameter list. Of course, you can''t allocate an array using a pointer.

I think you actually can use a reference to access memory, like so:

int *pVRAM = 0xa000;
int& pixel = *vRAM;
// or
int& pixel = vRAM[100];

However, a reference is a reference to one object of whatever type that element is. Since you probably want to treat memory like an array, references will be of little use.

Share this post


Link to post
Share on other sites
On another note: can you have std::vector that''s appropriately sized and starts at 0xa000 or some other arbitrary value?

Share this post


Link to post
Share on other sites
quote:
Original post by IndirectX
If I''m not mistaken, the array variable is actually a const pointer to its first element...



I don''t know about it being const. I''ll have to look this up, but if it was const, then how could you dereference the other array elements?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There are these two statues in a park; one of a nude man and one of a nude woman. They had been facing each other across a pathway for a hundred years, when one day an angel comes down from the sky and, with a single gesture, brings the two to life. The angel tells them, "As a reward for being so patient through a hundred blazing summers and dismal winters, you have been given life for thirty minutes to do what you''ve wished to do the most."
He looks at her, she looks at him, and they go running behind the shrubbery. The angel waits patiently as the bushes rustle and giggling ensues. After fifteen minutes, the two return, out of breath and laughing.
The angel tells them, "Um, you have fifteen minutes left. Would you care to do it again?" He asks her, "Shall we?" She eagerly replies, "Oh, yes, let''s! But let''s change positions. This time I hold the pigeon down and you crap on its head!"

Share this post


Link to post
Share on other sites
quote:
Original post by Some Guy
but if it was const, then how could you dereference the other array elements?

There''s a difference between const pointer and pointer to const data. See

// Mutable pointer to mutable data
char *p;
// Mutable pointer to const data
const char *p;
// Const pointer to mutable data
char * const p;
// Const pointer to const data
const char * const p;

With and only with mutable pointers, you can do

p = new char[...];

With and only with pointers to mutable data, you can do

p[0] = ''a'';


I''m not sure about memory limit. You can have segment restrictions, memory banks, and maybe other things that complicate raw video memory access. But assuming all your video memory fits in one segment, there''s only sense in using the part of the segment that is actually mapped to VRAM.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!