#### Archived

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

# pointers

This topic is 6129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how much does ogl rely on pointers? they seem to be a concept i cannot grasp and i know DirectX is *huge* on pointers... thanks ~SuperRoy

##### Share on other sites
Well, OGL is probably slightly less pointy than D3D but at the end of the day, you aren''t going to get very far in the world of programming without understanding pointers. Don''t hide from them - they seem complicated and weird to begin with, but suddenly it will all click and you will look back and laugh at how unbelievably simple they are.

##### Share on other sites
Agreed pointers are quite simple, and definetly worth your time to learn. OpenGL doesn''t rely on you using pointers very much.. but it''d be worth your while to learn about them, and to use them in many programs (whether opengl, d3d, or a database program).

A Variable (or array) is simply a starting location in memory.

int x[3]={1,2,3};
say x is in memory at address 0x0000001
it takes up 3 * sizeof(int) bytes in memory.
so x starts at 0x0000001 and ends at 0x0000001+12 (sizeof(int)=4 in MSVC).
So, when you check the value of x, you''re really checking what''s in memory at address 0x0000001. So, basically X Points to the memory address 0x0000001. If you create a pointer to X:
int *ptrx = x;
then, it simply sets ptrx to the same memory location as x (0x0000001). Now they can both modify the same thing because they both point to the same memory.

ptrx[0]=5;
This sets 0x0000001 = 5. So if you do printf("%d",x[0]); it now reads in what''s stored in memory at 0x0000001, and prints a 5 intead of the original 1 that you set it to.

Billy

ps. Ok, i suck at explaining things, so sue me :o).

##### Share on other sites
A useful analogy might be to imagine your computer's memory space as a city.

Every house has an address. Lets say you want to perform an operation on a house, say, build a conservatory. There are two possible ways to do this....

1. Build an exact copy of the house at the builders yard, let them add the conservatory, then build an exact copy of the newly improved house back at its original location, then knock down the one in the builders yard.

2. Send the builders the address of your house, and have them come around and do the work on the original.

Similarly, every variable or struct in memory has an address. Sometimes, you send the whole thing to a function, which results in a temporary copy being made. (analogous to 1.) For basic types (chars, ints, floats etc) this isn't a major problem. For larger objects like structs or classes, this is very inefficient.
Alternatively you can send a pointer. This is analogous to 2, and is generally a lot faster.

  // method 1 (no pointers)House BuildConservatory(House h){ // h is a copy of the house that was passed in. // this means we access the structure with the . operator h.hasConservatory = true; return h;}int main(void){ House myHouse; myHouse = BuildConservatory(myHouse); return 0;}// method 2 (pointers)void BuildConservatory(House* ph){ // ph is a pointer to the house structure that has been passed in. // this means we use the -> operator instead of . ph->hasConservatory = true;}int main(void){ House myHouse; BuildConservatory(&myHouse); return 0;}

Edited by - Sandman on January 9, 2002 12:50:58 PM

Edited by - Sandman on January 9, 2002 12:54:53 PM

##### Share on other sites
I don''t want to insult the anonymous poster, but there is a small flaw in his post. The array ends at 0x0000001+11 not 0x0000001+12. Why? Simple: 1,2,3,...,12 are twelve numbers, BUT the last number is 11 higher than the first.

##### Share on other sites
Thanks guys, Especially Sandman...That analogy helped a lot.

~SuperRoy

##### Share on other sites
The problem with learning pointers isn''t necessarily that they are difficult, but it''s how they are taught. If you open up your average "Learn C or C++" book, pointers aren''t mentioned until about chapter 5 (or later). Furthermore, at no time is computer architecture ever really mentioned when discussing pointers...and this is what pointers are all about!!!

Pointers are very easy if you think of them as just what they are named...pointers. They point to physical addresses spaces (forget virtual memory for now). Pointers are NOT abstract in any way...and are the tools to the underlying memory architecture (again, forget virtual memory).

##### Share on other sites
SuperRoy; here is a example I wrote recently.
Its not really answering your question, but it does show the pointer usefulness.
Pseudocode...

Print gets a pointer sent to it(str)

It assigns it to a temporary pointer.
temp-pointer = Pointer

Loop begins.
if(temp == 0)
null value; break loop.
else if(temp == ''/n'')
linefeed
else
send that value to the char renderer funtion
Increments the address. Because its a char(1 byte)it increments the memory address by one.

end of loop.

This code uses GLUT and is fairly slow due to the GLU function in there; however, Im going to be writing my own character renderer soon.

But this code shows the usefullness of pointers.

bool Print(char *str,cPoint point, float scale = 0.033, float nrow = 10){		glLoadIdentity();		//Move to start co-ord. Specified in current world co-ords.		glTranslatef(point.x, point.y,point.z);		//Scale to a resonable size...0.04 works well.		glScalef(scale,scale,scale);		//temporary sting used for pointer arithmatic		char *temp = str;		//index for automatic linefeeds		int linefeed = 0;		//Counts linefeeds		int k = 0;		//Basic error check		if(temp == NULL)		{			//Just returns false. Implement error check elsewhere.			return false;		}			//indexes thru the string. string _must_ be null-terminated. :-D		while(*temp != 0)		{			//If its a newline, linefeed			if(*temp == ''\n'')			{				glLoadIdentity();				glTranslatef(point.x, point.y -= nrow, point.z);				glScalef(scale,scale,scale);				k++;			}			//output char.			glutStrokeCharacter(GLUT_STROKE_ROMAN,*temp);			temp++;			linefeed++;		}		return true;} `

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 12
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632991
• Total Posts
3009749
• ### Who's Online (See full list)

There are no registered users currently online

×