15,507,388 members
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
{
public:
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);
}

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

glEnd;

}```

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.
Posted
Updated 8-Aug-21 0:06am
v3

## Solution 1

Yes, that memory needs to be allocated before it is used. A statement like this could be used :
C++
`wcPt2D* vertRot = new wcPt2D[ nVerts ];`
and, of course, it must be released after it is used :
C++
`delete [] vertRot;`

Southmountain 7-Aug-21 19:42pm
thank you for the confirmation! now I am very confident on this subject now.

## Solution 2

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.