c coding problem

i’am a mechanical engg doing a project in comp graphics. i’am not very fluent in c coding as well as opengl.i’ve been defined a problem.
it is that i’ve to read the points from the file ,which i’ve done.but the problem is that i’ve store the points read so that i dont have to read from the file again.i,ve been told that use of arrays of structures or linked lists is inefficient.please suggest a solution.

Hmmm. If you’ve been told not to use arrays or linked lists, what are you supposed to store them in?? Arrays are a bit inflexible, and linked lists can lead to memory fragmentation if each node is allocated seperatly. If you know the number of points that you’re gonna be reading in, then theres no reason not to use a dynamically allocated array.

Whats the size of this file you’re reading in, and how many files are to be read? And how frequently will they be read?

Nutty.

Arrays are only inefficient when you create a global array of elements that you may or may not use.

For example,

You wish to load ‘n’ number of integers from a file, but you dont know how large n will be. You could say, well I doubt it’ll be more than 1000, so you do the following; (in psuedo code)

#define MAXDATASIZE 1000

// global array of 1000 integers
int data[MAXDATASIZE];

main()
{
open file;
int i=0;
while ( not at end of file )

   {

     	 read integer from file, to data[i];
 		 i++;
   }

}

The problem is this. if the number of elements contained is small, a lot of memory will be wasted. Not very efficient in terms of data storage.

You could then say, “well if I dont want to waste memory, but I still dont know how much space this will take up, I could use a linked list, then no memory will be wasted”. The other advantage would be the ease with which extra elements can be inserted or removed. If this is essential, then this is likely to be your answer.

There is but one problem with linked lists…

malloc, calloc, & realloc incur a fair amount of system overhead to execute. If you are talking about a small amount of data elements, then its something you can probably live with. If however a large amount of data is to be read in, then the loading part will take a hell of a long time while the memory is allocated for each piece of data.

In this case, I suggest using calloc, or malloc to allocate memory for your arrays… ie

int *dataPtr;

main()
{
int DataCount;

openfile;

DataCount = runDataCount();

dataPtr = (int *)malloc( DataCount * sizeof(int) );
// or dataPtr = (int *)calloc( DataCount , sizeof(int) );

int i=0;

while( not at end of file )
{
read integer from file and place in dataPtr[i];
i++;
}
}

With this method, memory is only allocated once, so your file will load a lot quicker. It is of course harder to remove and insert elements from this array.

The only other real option left is to look at C++ and vectors. Vectors act in a very similar way to arrays, however they offer a few more options to manage the data. Its a kind of medium between the two…

[This message has been edited by Rob The Bloke (edited 12-04-2000).]