Results 1 to 3 of 3

Thread: c coding problem

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2000
    Location
    bangalore,karnataka,india
    Posts
    3

    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.

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Portsmouth, Hampshire, England
    Posts
    926

    Re: c coding problem

    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.

  3. #3
    Senior Member Regular Contributor
    Join Date
    May 2000
    Location
    Oxford, England
    Posts
    472

    Re: c coding problem

    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).]

Similar Threads

  1. i am having problem with this coding?? can someone help me??
    By TeeLaPulak in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 01-16-2014, 03:11 PM
  2. help with coding
    By jaffacakes in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-26-2008, 01:04 PM
  3. need help for coding
    By vanitha in forum OpenGL: User Software
    Replies: 0
    Last Post: 03-13-2006, 01:11 AM
  4. coding problem with cube
    By coda in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-08-2003, 06:22 PM
  5. Coding??
    By in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 08-07-2002, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean