Click here to Skip to main content
15,507,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looking at one example on OpenGL college textbook. I think I find an issue here about pointer as this: class wcPt2D* vertRot inside function body:
class wcPt2D
    GLfloat x, y;

//this is a rotate transformation
void rotatePolygo(wcPt2D* verts, GLint nVerts, wcPt2D pivPt, GLdouble theta)
   wcPt2D* vertRot; //question here: do we need to allocate the dynamic memory first?
   GLint k;

   for(k=0; k< nVerts; k++){
       vertsRot[k].x = pivPt.x + (verts[k].x - pivPt.x) * cos(theta)
                      -(verts[k].x - pivPt.y ) * sin(theta);
       vertsRot[k].y = pivtPt.y + (verts[k].x - pivPt.x) * sin(theta)
                      +(verts[k].y - pivPt.y) * cos(theta);

      for(k=0; k < nVerts; k++)
         glVertex2f(vertsRot[k].x, vertsRot[k].y);

I know in C++, struct is the same as class type. from my understanding,
vertRot is wcPt2D pointer and need to be allocated based on nVerts first, otherwise, this function will fail to compile.

I am not sure if I am right on this point. so posted here to check with gurus here.

What I have tried:

for these kind of struct pointers, I am very familiar.
Updated 8-Aug-21 0:06am

Yes, that memory needs to be allocated before it is used. A statement like this could be used :
wcPt2D* vertRot = new wcPt2D[ nVerts ];
and, of course, it must be released after it is used :
delete [] vertRot;
Share this answer
Southmountain 7-Aug-21 19:42pm    
thank you for the confirmation! now I am very confident on this subject now.
Yes, Ricks explanation is correct. In C++ the pointer is only the address of data and NOT some data. So you need to allocate and free the data by yourself.
This has the advantage that the expensive memory allocation can finetuned by the programmer. The obvious advantage is when you use pointers as input for functions: no new object gets allocated. Another hugh advantage are type casts which needing good understanding of the topic.
Share this answer
Southmountain 8-Aug-21 17:57pm    
thank you for sharing your insight on function parameters' point. also the link is good info and I will test these usages too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900