Thread: how to malloc memory?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    how to malloc memory?

    To process the file as follows:
    Code:
    AATTTTAATATA
    CGGCCCCCGC
    ...
    I define a struct as follows:
    Code:
    struct SNPData
    {
           char** SNPData;
           //char** MajOrMin;
    };
    int main()
    {
        struct SNPData *s;
        s = readSNPData("1.txt");
    
         return 0;
    }
    but I don't know how to malloc memory to SNPData.

    part code
    Code:
    /*  read into memory from input file */
    struct SNPData* readSNPData(char *input)
    {
           struct SNPData *SNPs;
           FILE *fp;
           fp = fopen(input,"r");
         
           int i = 0;
           int isKnownLen = 0;
           while(! feof(fp))
           {
                   if ( isKnownLen = 0 )
                   {
                        char *buf = (char *) malloc( 1*sizeof(char) );
                        char c;
                        while( (c = fgetc(fp)) != '\n' )
                        {
                               *(buf+i) = c;
                               realloc(buf,i+2);
                               ++i;
                        }
                        isKnownLen = 1;
                   }else
                   {
                        //fscanf(fp,"%s",tmp);
                        char *buf = (char *) malloc( i*sizeof(char) );
                        fgets(buf,i,fp);
                   }                    
           }
           fclose(fp);
    }
    how to malloc memory to SNPData and where is the codes mallocing memory added?
    Last edited by zcrself; 04-21-2010 at 02:00 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First off, you need an instance of your structure. Since all you have is a pointer, you need something like:
    Code:
    SNPs = malloc( sizeof *SNPs );
    That gives you one instance of SNPS to play with. Now, inside that, assuming you're trying to do some sort of rows/cols thing here:
    Code:
    SNPs->SNPData = malloc( rows * sizeof( *(SNPs->SNPData) ) );
    Then, in a loop, for each one of those rows:
    Code:
    SNPs->SNPData[ thisrow ] = malloc( ...some size for each row... );
    Then call free in the reverse of how you allocated.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need to read up about "Schlemiel the painter's algorithm". It's what you have in your program and it is very much a bad thing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > while(! feof(fp))
    This is a FAQ entry.

    > if ( isKnownLen = 0 )
    I'm guessing you mean == here

    > char *buf = (char *) malloc( 1*sizeof(char) );
    This (the casting) is a FAQ entry as well

    > realloc(buf,i+2);
    You need to read the manual to find out what realloc returns.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Salem View Post
    > while(! feof(fp))
    This is a FAQ entry.

    > if ( isKnownLen = 0 )
    I'm guessing you mean == here

    > char *buf = (char *) malloc( 1*sizeof(char) );
    This (the casting) is a FAQ entry as well

    > realloc(buf,i+2);
    You need to read the manual to find out what realloc returns.
    Code:
    char *buf = (char *) malloc( 1*sizeof(char) );
    realloc(buf,i+2);
    the codes above is used to malloc memory with the counts of the char.
    if there is 1 char in input file, I malloc 1*sizeof(char)
    if there is 2 chars in inputfile, I malloc 2*sizeof(char)
    if there is 3 chars int input file , I malloc 3*sizeof(char)
    ....

    Code:
    while(! feof(fp))
    the code above is used to check whether is the input file finished

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zcrself View Post
    Code:
    char *buf = (char *) malloc( 1*sizeof(char) );
    realloc(buf,i+2);
    the codes above is used to malloc memory with the counts of the char.
    if there is 1 char in input file, I malloc 1*sizeof(char)
    if there is 2 chars in inputfile, I malloc 2*sizeof(char)
    if there is 3 chars int input file , I malloc 3*sizeof(char)
    ....
    So the point here is that neither of the two lines of code here is correct. The first is incorrect, as the result of malloc should not be cast. The second is incorrect, as you are asking for memory and then throwing it away.
    Quote Originally Posted by zcrself View Post
    Code:
    while(! feof(fp))
    the code above is used to check whether is the input file finished
    You intend it to do so. However, that is not what it actually does.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by tabstop View Post
    So the point here is that neither of the two lines of code here is correct. The first is incorrect, as the result of malloc should not be cast. The second is incorrect, as you are asking for memory and then throwing it away.

    You intend it to do so. However, that is not what it actually does.
    How to correct it ?
    I only know the second fault. My result corrected is as follows:
    Code:
    buf = realloc(buf,i+2);
    How to dynamic malloc memory ? can anyone give me a example
    Last edited by zcrself; 04-21-2010 at 11:01 PM.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    how to check whether is file end?

    previous:
    Code:
    while( !feof(fp) )
    {
            //readFile();
    }
    after correcting it:
    Code:
    while( feof(fp) == 0 )
    {
    }
    is this done like above correct?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > is this done like above correct?
    No, you shouldn't be using it at all.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
      // do stuff
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by Salem View Post
    > is this done like above correct?
    No, you shouldn't be using it at all.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
      // do stuff
    }
    I want to malloc memory dynamicly,
    If I do like you said, I don't malloc memory dynamicly,
    because BUFSIZ is const

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't do dynamic memory allocation and reading at the same time -- at the time you are trying to read something, you had better already have the memory for it. The usual sort of thing is exactly what Salem posted -- you read in a line at a time, and then add that line to your ever-growing pile of characters.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > I want to malloc memory dynamicly,
    Yes, you
    - read it into a fixed buffer
    - work out what space you need
    - allocate it
    - copy the buffer
    - rinse and repeat until the file is read.

    Or if you really want a char at a time, then go with
    Code:
    int ch;
    while ( (ch=fgetc(fp)) != EOF )
    Extending a buffer using realloc by 1 character at a time can get extremely expensive if the block keeps moving.

    But until you can read a file reliably (ie, not using feof() in the way you're using it), the malloc issue is rather moot.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 04:43 PM
  2. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  3. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  4. Reallocating Memory
    By Booie2k1 in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 06:09 AM
  5. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM