[H-GEN] 'C' code help needed

Andrae Muys A.Muys at mailbox.uq.edu.au
Sun Oct 26 20:16:17 EST 1997


On Sun, 26 Oct 1997, luke Grant wrote:

> Ok, i have this "c" assignment due in on tuesday and
> i am having trouble with dynamic allocation of memory.
> As far as i can see the procedure below should work properly
> but when i go to use the command "realloc()" i get told that
> the memory alocation has failed.
> 
Well a few comments.  One realloc is a function you want to avoid most of
the time.  In this case it is unnecessary, and inefficient.  You would be
better off reading the file into a linked list and then either returning
that or copying it into an the array AFTER you know how long it needs to
be.  Then again it looks like you're reading in a periodic table so you
should already know a good limit.  I reciently needed something similar
and just defined MAXELEMENTS = 140, and a data structure

struct periodic {
        int atomicN;
        char atomName[MAXNAME];
        int buttonN;
        int Niso;
        struct isoStruct {
                char isoName[MAXISONAME];
                float spin;
                float gn;
                float abundance;
        } isoList[MAXISOTOPES];
        int Nspin;
        float spin[MAXSPINS];
} elements[MAXELEMENTS];

Fun problem that particular piece was.  Still I would strongly recommend
that you forget about realloc for now, and just use a linked list to store
the data as you read it, and then just copy it across to an array if
necessary.

Andrae Muys

P.S.

You haven't declared dummy, or maxnum.  You mention count() in your
comments but don't use it.  Basically looking at the function it will
almost certainly be faster to simply scrap the entire thing, take the
lessons learnt from it, and rewrite it from scratch.  (This time use a
linked list for temporary dynamic storage. :).

----------------------- HUMBUG General List --------------------------------
echo "unsubscribe general" | mail majordomo at humbug.org.au # To Unsubscribe



More information about the General mailing list