0

Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)

I'll try to keep this generic enough for anyone who knows Objective-C, where the problem is rooted (or maybe it's just in C?), and I'll avoid as much OpenGL as possible while still providing context.

I'm storing vertex data for a 3D object in a GLfloat array like this

GLfloat teapot[] = { 1.0, 0.0, .... 0.0 };

(with real values omitted for brevity)

I'm then making a call like this

[self drawObject:teapot];

for the method defined like this

-(void) drawObject:(GLfloat *)object {

    NSLog(@"%ld %ld", sizeof(object), sizeof(teapot));
}

Of course, the goal is to draw stuff, but it doesn't work properly because sizeof(object) doesn't return the correct value. (I've similarly omitted all drawing code since it is irrelevant here, except that it requires using sizeof() for glDrawArrays().

The output from NSLog() looks something like this:

sizeof object = 4, sizeof teapot = 6336

showing that the size of the passed object is different from the size of calling teapot directly. It's weird because the reference works correctly, i.e. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, object) works just fine, so it's definitely reading in the object. At present, I have to also pass in the size of the object as an integer just to get glDrawArrays to work, which is kind of tacky. The passed code looks like this

[self drawObject:teapot ofSize:(int)sizeof(teapot)];

whereas I would really want to avoid passing the additional parameter.

Any ideas? Thanks for your time.

Community
  • 1
  • 1
Mike
  • 157
  • 1
  • 9
  • [This question has been asked quite a few times](http://stackoverflow.com/search?q=sizeof+array). Nothing to do with Objective-C or OpenGL, it's a plain old C language issue. – Kurt Revis Jan 28 '13 at 06:19
  • 1
    Thanks for the link, Kurt. I have seriously been searching Stackoverflow, Apple Developer forums and code, a programming book, and coming up short for the past 45 minutes. Apparently I just didn't know how to describe the problem I was having, but now that you mention it, it makes sense that one could have found other threads of that that if they knew what they were looking for. – Mike Jan 28 '13 at 06:31

1 Answers1

3

Pointers and arrays aren't the same thing. Unfortunately, you'll have to pass the size yourself, either as an extra parameter, or as part of a stucture or an object.

Helpful reading: http://c-faq.com/aryptr/aryparmsize.html

Carl Norum
  • 219,201
  • 40
  • 422
  • 469