Thread: allocating memory screws up data being read in from file

  1. #1

    allocating memory screws up data being read in from file

    Okay where do I begin. Basically I am reading stuff in from a file. I dont' want to post all of the structures, but the following works (seemingly) perfectly:

    for(int index = 0; index < pHeader.numMeshes; index++) {	
    	fseek(filePtr, offset, SEEK_SET);
    	fread(&pMesh, sizeof(Mesh), 1, filePtr);
    Skin		*pSkin		= new Skin[pMesh.numSkins];
    Face		*pTriangle	= new Face[pMesh.numTriangles];
    TexCoord	*pTexCoord	= new TexCoord[pMesh.numVertices];
    Triangle	*pVertices  = new Triangle[pMesh.numVertices * pMesh.numMeshFrames];
    fread(pSkin, sizeof(Skin), pMesh.numSkins, filePtr);
    fseek(filePtr, offset + pMesh.triStart, SEEK_SET);
    fread(pTriangle, sizeof(Face), pMesh.numTriangles, filePtr);
    fseek(filePtr, offset + pMesh.uvStart, SEEK_SET);
    fread(pTexCoord, sizeof(TexCoord), pMesh.numVertices, filePtr);
    fseek(filePtr, offset + pMesh.vertexStart, SEEK_SET);
    fread(pVertices, sizeof(Triangle), pMesh.numVertices * pMesh.numMeshFrames, filePtr);
    delete[] pSkin;
    delete[] pTriangle;
    delete[] pTexCoord;
    delete[] pVertices;
    offset += pMesh.meshSize;
    Now, whenever I add this line of code into the loop the 'data' that is being read turns into garbage, i.e instead of getting sub-50 values for the specific file I am trying to read in, the range goes up into the thousands (could be reading hexadecimal memory addresses perhaps?)

    m1.theObjects[location].pVertices = new CVector3[pMesh.numVertices];

    m1 is a Model structure, each Model structures has a vector of type Object (NOTvector of pointers!), each Object has among other things an array of Vertices that must be allocated at run time.

    Here are those data structures
    struct object {
    CVector3 * pVertices; //ALL OF THE VERTICES IN THE MODEL

    struct model {
    vector<object> theObjects; //ARRAY OF MESHES

    I dont know if I've posted enough for you to help me, but I didn't want to over do this. Anyway to sum it up allocating memory for the pVertices is what is screwing me up. When I don't allocate memory for the vertices and just read in stuff from the file and check it, it is all good.

    Thanks if you can help me. I suck with allocating memory

    And yes I know pHeader and pMesh arent pointers

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    your code would be much simplified if you gave that struct a constructor/destructor and did your memory allocation there. Then when the object is constructed the memory needed is allocated at the same time.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3
    I didn't know you could give a struct a constructor/destructor, I honestly thought that was only with classes. Anyway do you think that will solve the problem? It seems my allocation of memory shouldn't be screwing up the data being read into completely different variables.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Tbh i didnt look for your error just at the design. If the design was a little better and your memory allocation was done where it should be then errors will become easier to spot. Also easier to debug each class/struct at a time with some small driver progs.
    As for structs they are identical to classes in c++ with the exception that structs default access to public whereas classes default to private....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  5. #5
    well then I guess I am just dumb because I thought structs were supposed to be methodless. I have always considered structs to just contain data members, and that if I ever needed methods I revert to classes. in my opinion it would not really matter if I made the constructor doing the allocation of memory, it is still going to be allocating the memory, but just to be fair I am not at my source and I cannot test putting the allocation in the constructor to see if it actually works.

    Thanks for replying, I had my doubts as to if anyone would actually care

  6. #6
    it seems using malloc instead of new DOESN'T SCREW ANYTHING UP...this is totally weird, can someone please try to explain to me WHY allocating memory in one place using the 'new' keyword was causing the data in the buffers read in from the file to be corrupted? this is stupid I hate programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  4. Read data from file !!!
    By frankiepoon in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 11:45 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM