• Advertisement

Archived

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

Pointers, memory, and questions

This topic is 5755 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
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!

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
quote:
Original post by 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!"


In the words of James Brown: "Haha! SHUT UP!"

Share this post


Link to post
Share on other sites
I was also wondering, if a register is a speical value in the cpu/hardware eg 0x400 (just made up by the way) can you use that value or not? also I''ve heard of ring-0 mode before on some hacker news item, so what exactly is it?

CEO Plunder Studios

Share this post


Link to post
Share on other sites
quote:
Original post by elis-cool
I was also wondering, if a register is a speical value in the cpu/hardware eg 0x400...

Registers are memory locations, not values.

quote:

also I''ve heard of ring-0 mode before on some hacker news item, so what exactly is it?

It''s the same thing as kernelmode - operating system level access. On modern protected mode operating systems generally only OS components and drivers have ring-0 access. Very few applications need it.

For more information, STFW.

[ 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
So if I had:

int *pVar;
pVar = 0x400 // 0x400 being a register

What would happen? eg if I tryed to derefernce it, or plant a value there?
Is there a list of registers for cpu''s/hardware that you can use?

CEO Plunder Studios

Share this post


Link to post
Share on other sites
Registers are not memory-mapped... you can't do things like that.

Edit: Just to clarify, a register is a special piece of memory, which holds just 4 bytes (well, some of them hold more, some less).

A memory-mapped piece of hardware allows you to talk to the hardware by reading/writing to certain memory addresses. The old VGA cards mapped 0x0A000 -> 0x19A00 (that's 64k bytes, or 320x200) as the frame buffer, so you could directly access the framebuffer by reading/writing those memory locations.

High-level languages like C/C++ don't allow direct access to registers, though most compilers have features for writing assembly directly (like the __asm directive under VC++) which would allow you to directly access the registers.


codeka.com - Just click it.

[edited by - Dean Harding on April 20, 2002 6:51:37 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Dean Harding
A memory-mapped piece of hardware allows you to talk to the hardware by reading/writing to certain memory addresses. The old VGA cards mapped 0x0A000 -> 0x19A00 (that''s 64k bytes, or 320x200) as the frame buffer, so you could directly access the framebuffer by reading/writing those memory locations.



So, what about the higher resolution modes? Ones that consume ~300kb of VRAM (640x480x8 I think) and stuff?




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

Share this post


Link to post
Share on other sites
Higher resolutions weren''t standard. Different cards did it different ways. That''s why there was the VESA standard, which tried to get venders using the same standard (remember SciTech''s DisplayDoctor? That presented a VESA interface for cards which didn''t support VESA).

There were basically two ways of doing high resolutions, bank-switching and a flat model. Bank switching was where the screen was divided up into "banks", and you wrote to part of the screen, switched banks (with an interrupt) and wrote to another (using the same memory-mapped location). So, depending on which bank you were accessing, 0xA000 could point to the top-left, top-middle, middle-left and the center of the screen (for a 640x400 screen). There were many, many different ways of doing banking. That''s why DirectX is so dang cool, cause there''s only one interface to all hardware.

The flat model was what VESA gave you, and it basically just mapped a larger portion of memory to the frame buffer.


codeka.com - Just click it.

Share this post


Link to post
Share on other sites

  • Advertisement