0

I copy some code from another project and I work fine in previous project but in new project I get linking error:

OpengLWaveFrontCommon.h:50:22: error: cannot initialize a variable of type 'VertexTextureIndex *' with an rvalue of type 'void *' VertexTextureIndex *ret = malloc(sizeof(VertexTextureIndex));

This file (OpengLWaveFrontCommon.h) is a part of openGL iPhone project: Wavefront OBJ Loader. https://github.com/jlamarche/iOS-OpenGLES-Stuff

Should I make some special flag or something, because it is C structured?

#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

typedef struct {
    GLfloat red;
    GLfloat green;
    GLfloat blue;
    GLfloat alpha;
} Color3D;

static inline Color3D Color3DMake(CGFloat inRed, CGFloat inGreen, CGFloat inBlue, CGFloat inAlpha)
{
    Color3D ret;
    ret.red = inRed;
    ret.green = inGreen;
    ret.blue = inBlue;
    ret.alpha = inAlpha;
    return ret;
}


#pragma mark -
#pragma mark Vertex3D
#pragma mark -
typedef struct {
    GLfloat x;
    GLfloat y;
    GLfloat z;
} Vertex3D;

typedef struct {
    GLuint  originalVertex;
    GLuint  textureCoords;
    GLuint  actualVertex;
    void    *greater;
    void    *lesser;

} VertexTextureIndex;


static inline VertexTextureIndex * VertexTextureIndexMake (GLuint inVertex, GLuint inTextureCoords, GLuint inActualVertex)
{
    VertexTextureIndex *ret = malloc(sizeof(VertexTextureIndex));
    ret->originalVertex = inVertex;
    ret->textureCoords = inTextureCoords;
    ret->actualVertex = inActualVertex;
    ret->greater = NULL;
    ret->lesser = NULL;
    return ret;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
ButterBeast
  • 521
  • 10
  • 25
  • Is that compiled as C++ or Objective-C++? If yes, you should add that (important) information to your question. - And btw, that is a *compiler* error, not a *linker* error. – Martin R Oct 30 '13 at 15:20
  • Where can I see with witch compiler the file is compiled? – ButterBeast Oct 30 '13 at 15:38

1 Answers1

5

Cause Of Issue:

malloc() returns a pointer of type void *, you need to type cast it to corresponding data-type.

malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object that has an alignment requirement less than or equal to that of the fundamental alignment.

Reference malloc()

Fix for the Issue:

VertexTextureIndex *ret = (VertexTextureIndex *)malloc(sizeof(VertexTextureIndex));
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Thanks this solution work! I post another question downstairs, can you be so nice and look if is some stupid mistake as here, please? – ButterBeast Oct 30 '13 at 15:15
  • 1
    Note that casting the return value of malloc() is only necessary if the file is compiled as C++ (or Objective-C++). In plain C or Objective-C, it is *not necessary* and - as far as I know - bad style. Compare http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc. – Martin R Oct 30 '13 at 15:16
  • Can I chose with witch compiler should the file be compiled, because in previous project the file does not return any error – ButterBeast Oct 30 '13 at 15:24
  • @user1436690: sorry, I was not at my desk :) What's your question, is that about compiler ? You can change the compiler settings in your IDE, but both GCC and LLVM compilers will show this error (probably) – Midhun MP Oct 30 '13 at 15:59