Pointers, memory, and questions

Started by
15 comments, last by Some Guy 21 years, 12 months ago
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?
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
"I am governed by none other than the Laws of the Universe."
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!
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;// orint& 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.
---visit #directxdev on afternet <- not just for directx, despite the name
On another note: can you have std::vector that''s appropriately sized and starts at 0xa000 or some other arbitrary value?
---visit #directxdev on afternet <- not just for directx, despite the name
quote:Original post by IndirectX
On another note: can you have std::vector that''s appropriately sized and starts at 0xa000 or some other arbitrary value?

Nope.

[ 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!
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?
Also, is there a limit to how much data can be held at 0xa000?
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!"
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 datachar *p;// Mutable pointer to const dataconst char *p;// Const pointer to mutable datachar * const p;// Const pointer to const dataconst 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.
---visit #directxdev on afternet <- not just for directx, despite the name

This topic is closed to new replies.

Advertisement