I have an OpenGL program, but can’t alloc correctly.
m_VertexData = (GLfloat*)malloc(sizeof(m_TempVertexData));
m_NormalData = (GLfloat*)malloc(sizeof(m_TempNormalData));
NSLog(@"sizeOfTempVertex: %d sizeOfTempNormal: %d", sizeof(m_TempVertexData),sizeof(m_TempNormalData));
NSLog(@"sizeOfVertex: %d sizeOfNormal: %d",sizeof(m_VertexData),sizeof(m_NormalData));
NSLog:
sizeOfTempVertex: 432 sizeOfTempNormal: 432
sizeOfVertex: 4 sizeOfNormal: 4
m_VertexData and m_normalData are pointers, so their size is sizeof (whatever type it has *), so it allocates the correct amount of memory. You need to allocate sizeof(member of the array) * number of items bytes of memory. By the way, a few things related to malloc:
sizeof(type), rathersizeof(variable). If you ever change the type of the variable, it’s gonna cause hard-to-track-down errors.Considering these points, use the following code: