1

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

Community
  • 1
  • 1
Lajos Viktor
  • 148
  • 7

4 Answers4

8

sizeof tells you the size of the type (calculated at compile-time). It tells you nothing about how much memory was allocated dynamically.1


1. Except in the special case of C99's variable-length arrays.
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
2

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:

  1. Don't cast the return value of malloc. It makes code unreadable and it's unnecessary as void * is implicitly promoted to whatever pointer type it is assigned to.
  2. Don't use sizeof(type), rather sizeof(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:

m_VertexData = malloc(sizeof(*m_VertexData) * numberOfItems);
  • I do not believe that is guaranteed that `sizeof(type*) == sizeof(void*)`. – Joe May 23 '12 at 13:36
  • Yes it is. Function pointers having the same size as data pointers is not guaranteed. –  May 23 '12 at 14:08
  • 1
    While it will likely be equal there is no guarantee, see the [answer here](http://stackoverflow.com/a/399030/418715) – Joe May 23 '12 at 14:26
2

According to the C standard, Section 6.5.3.4.2:

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

In your case, the operands m_VertexData and m_NormalData are pointers (which makes sense, because you have just assigned to them the result of calling malloc). Therefore, their size is the same as the size of any other pointer on your system - 4 bytes.

In case you are wondering, there is no standard way to determine how much memory you have malloc-ed; if you need that size for future reference, you need to store that number in a separate variable.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

sizeof may not work how you think it works. sizeOfVertex and sizeOfNormal are both 32-bit pointers, so 4 is the correct size for both of them. There's no portable way to determine the actual size of the allocated memory; see determine size of dynamically allocated memory in c.

Community
  • 1
  • 1
kfb
  • 6,252
  • 6
  • 40
  • 51