Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

outRider

pointers, malloc help

This topic is 6528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having some problems allocating memory for a pointer. Heres what I have so far. typedef struct tagVERTEX { float x, y, z, u, v; } VERTEX; typedef struct tagTRIANGLE { VERTEX vertex[3]; } TRIANGLE; typedef struct tagSECTOR { int nTriangles; TRIANGLE *triangle; } SECTOR; void ReadStr(FILE *file, char *string) { do { fgets(string, 255, file); } while ((string[0] == ''/'') || (string[0] == ''\n'')); return; } void LoadSector(char *SectorFile, SECTOR *sector) { int nTriangles, loop, vert; char cTextLine[255]; FILE *FileIn; FileIn = fopen(SectorFile, "rt"); ReadStr(FileIn, cTextLine); sscanf(cTextLine, "#P %d\n", &nTriangles); sector.triangle = malloc(TRIANGLE[nTriangles]); sector.nTriangles = nTriangles; for (loop = 0; loop < nTriangles; loop++) { for (vert = 0; vert < 3; vert++) { ReadStr(FileIn, cTextLine); sscanf(cTextLine, "%f %f %f %f %f", sector.triangle[loop].vertex[vert].x, sector.triangle[loop].vertex[vert].y, sector.triangle[loop].vertex[vert].z, sector.triangle[loop].vertex[vert].u, sector.triangle[loop].vertex[vert].v); } } fclose(FileIn); return; } The original code was written using new instead of malloc, so I might be doing something wrong here, but I don''t know what. Any help would be appreciated. The errors I get are as follows: Error E2294 C:\source\main.c 68: Structure required on left side of . or .* in function LoadSector Error E2108 C:\source\main.c 68: Improper use of typedef ''TRIANGLE'' in function LoadSector Error E2294 C:\source\main.c 69: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 76: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 76: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 77: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 78: Structure required on left side of . or .* in function LoadSector Error E2294 C:\source\main.c 79: Structure required on left side of . or .* in function LoadSector ------------ - outRider -

Share this post


Link to post
Share on other sites
Advertisement
You''re passing sector in as a pointer but your not using it
like a pointer.

Your code that looks like:

sector.triangle = malloc(TRIANGLE[nTriangles]);
sector.nTriangles = nTriangles;

Should use the "->" operator and not the "." operator.

Change it to:

sector->triangle = malloc(TRIANGLE[nTriangles]);
sector->nTriangles = nTriangles;

Same thing with pointer used elsewhere.

// CHRIS

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi,
Is the line triangles = malloc(TRIANGLE[nTriangle]);
correct?
is TRIANGLE an array of sizes?
ifnot u might want to do something like,
if triangle was a struct that u wanted to make and
array from,

traingles = (struct triangles *)malloc(sizeof(struct triangles)*nTriangle);

Evan,
evan@achaia.dircon.co.uk

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yep,
ignore most of that last post!
what your other problem not pointed out by the first reply to ur post, is that the malloc field isn''t used correctly,
you want:

sector->triangle = (TRIANGLE *)malloc(sizeof(TRIANGLE)*nTriangles);

I think u must of been confused cause of the way the new keyword works in C++ (don''t understand it my self)
somming like new [ARRAYNUMBER] TYPE
??
Evan,
evan@achaia.dircon.co.uk

Share this post


Link to post
Share on other sites
(not that it has anything to do with the original post, but it felt like someone was asking about the new operator, maybe?)

to allocate memory for an array of structs using the new-operator you write:

new TYPE[NumberOfItems]



Share this post


Link to post
Share on other sites
I''m not entirely sure I understand why you would rather use malloc than new. The new operator is simpler to use, and much, much safer than malloc.
My suggestion is toss the malloc, and use new.

-Neophyte

~Death awaits you all, with sharp, pointy, big teeth~

Share this post


Link to post
Share on other sites
The new operator is simpler than malloc? Not really, malloc is just a function call with how much you wanna allocate. Then you check for errors. New may be less buggy (although i never heard of a malloc bug, please feel free to inform me so I can improve my habits), but not really much simpeler

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!