Thread: why error while the number is very big ?

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

    why error while the number is very big ?

    Code:
    char *pa[12];
    char ........;
    
    void append(char s[],char str[], uint32_t n)
    {
         int i = 0;
         while(str[i] != '\0')
         {
                      s[n++] = str[i];
                      i++;
         }
    }
    
    void substring(char *s,char *r,uint32_t st,int len)
    {
         int i;
         //char *p;
         for(i = 0; i < len; i++)
         {
               s = s+st;
               *r++ = *s++;
               //r[i] = s[i+st];
               //puts(r);
               //s++;
         }
         *r = '\0';
         //puts(r);
    }
    //[13] read2genome: read genome into memory or pointer array[12]
    void read2genome(char *pa[12],char *input)
    {
         FILE *fp = fopen(input,"r");
         extern char ........;
         
         while(! feof(fp))
         {
                 char line[80];//temporarily store line content
                 int i = 0;
                 unsigned long int len;
                 //char tmp[80];
                 //fgets(tmp,fp);
                 fscanf(fp,"%s",line);
                 
                 if(line[0] == '>')
                 {
                            i++;
                            len = 0;
                            ss = (char *) malloc(50000000); 
                            pa[i] = ss;
                            puts(line);
                 }else{
                       if(line[0] == 'A' || line[0] == 'C' || line[0] == 'G' || line[0] == 'T')
                       {
                                  
                                  append(ss,line,len);
                                  len = strlen(line)+len;
                       }
                 }
         }
         fclose(fp);
    }
    Code:
    int main(int argc,char *argv[])
    {
        read2genome(pa,argv[2]);
        substring(pa[1],ss,6553500,12);
        return 0;
    }
    result :
    error

    the size of source file is very big
    why error while the number is very big ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>ss = (char *) malloc(50000000);
    Oh my goodness. You don't need that much memory. That's about 48 MB.

    And I'm guessing that, if the source file is too big, you simply run out of memory.

    And then goes the usual line: post the smallest simplest program that displays the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Elysia View Post
    >>ss = (char *) malloc(50000000);
    Oh my goodness. You don't need that much memory. That's about 48 MB.

    And I'm guessing that, if the source file is too big, you simply run out of memory.

    And then goes the usual line: post the smallest simplest program that displays the problem.
    Maybe the OP really does need that, whose to say? The one really bad thing about that, however, is the retval of malloc() was NOT checked.

    EDIT: Oh, and another thing: If you are going to request that much memory it is best to keep in in PAGE_SIZE (usually a multiple of 4096).
    Last edited by Kennedy; 08-28-2009 at 08:20 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kennedy View Post
    Maybe the OP really does need that, whose to say? The one really bad thing about that, however, is the retval of malloc() was NOT checked.
    I question that, seriously, because it's allocated once for each row. And if the file is 1000 rows, are you going to allocate 46 GB then?
    Last edited by Elysia; 08-28-2009 at 10:27 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Elysia View Post
    I question that, seriously, because it's allocated once for each row. And if the file 1000 rows, are you going to allocate 46 GB then?
    I gotta start reading all the code, huh. . . didn't catch that one.

    hey, that'd also be a memory leak.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah yes, but the current code uses fscanf to read a... string. In a buffer 80 chars long. No wonder the code fails miserably. This is very prone to buffer overruns.
    SourceForge.net: Buffer overrun - cpwiki
    SourceForge.net: Scanf woes - cpwiki
    Stick with fgets instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    read source file into memory is correct;
    error occurs in as follow line:
    Code:
    substring(pa[1],ss,6553500,12);
    if I use as follows code
    Code:
    substring(pa[1],ss,655350,12);
    This program is correct!

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Probably not relate to your problems but I thought I'd point out some things...

    #1.
    Code:
    while(!feof(fp))
    {
        char line[80];//temporarily store line content
        int i = 0;
        unsigned long int len;
        //char tmp[80];
        //fgets(tmp,fp);
        fscanf(fp,"%s",line);
    This method of controlling a loop is often done wrong as is the case here, rarely is it ever done correctly. The problem is that the flag that says you are at the end of the file only gets set after you attempt to read past the end of the file. When you read and process the last line of data from the file, feof is still false, you are at the end of file having read the last line but the indicator is still false. Because of this you'll enter your while loop one last time and attempt the read again (calling fscanf) which will fail - although this time it will set the end-of-file flag to true - leaving the buffer unchanged from the previously correct read. The effect of all this is that the last line of data will essentially be processed twice, something I'm guessing is not what you want to have happen in your program.

    The 'better' way to handle this is to make the read operation a part of the while loops conditional check. In this case, the return value of the fscanf call can be checked to make sure data was read (or not). If not, the loop is never entered and no processing of bad data can take place. If the read is successful then the loop is entered and processed.

    Code:
    char line[80];//temporarily store line content
    
    ...
    
    while(1 == fscanf(fp,"%s",line))
    {
        int i = 0;
        unsigned long int len;
        ...

    #2.
    Code:
    ss = (char *) malloc(...);
    You shouldn't cast the return value from the malloc call in C.
    Last edited by hk_mp5kpdw; 08-31-2009 at 07:20 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  3. how to transform an aray of char in to a big number
    By transgalactic2 in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 06:41 AM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM