Thread: I used calloc to clear the string hear. is it a good idea?

  1. #1
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56

    I used calloc to clear the string hear. is it a good idea?

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #define length(x) strlen(x)
    int main(int argc,char* argv[])
    {
        char *string;
        string=(char *)malloc(20);
        register char special;
        register    short int i=0,a=0;
        register    short int c;
    
        FILE *fp;
        fp=fopen(argv[1],"r");
    
        while(1)
        {
            while(!feof(fp))
            {
                a=0;
                string=(char *)calloc(20,1);//<<========<<Hear
    
    
                while(!feof(fp))//
                {
                    c=getc(fp);
                    special=c;
                    if((c>=65 && c<=122))
                    {
                        string[a]=c;
    
                    }
                    else if(c>=48 && c<=57)
                    {
                        string[a]=c;
    
                    }
                    else break;
    
                    ++a;
                }
    
                i=length(string)-1;
                while(i>=0)
                {
                    putchar(string[i]);
                    --i;
                }
                if(c=='\n')break;
                if((c>=0 && c<=126))
                    putchar(special);
            }
            if(c==EOF)break;
            putchar('\n');
        }
        fclose(fp);
        return 0;
    }

    I calloc, malloc and realloc are used for dynamic memory allocation but I do not need any dynamic memory allocation in this program. is there any other way to avoid dynamic memory allocation and hence speed up the program ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just use an array if you don't need dynamic allocation.


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

  3. #3
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Quote Originally Posted by quzah View Post
    Just use an array if you don't need dynamic allocation.
    but I have to clear the string at line indicated by "//<<=======<<" in the code each time I execute the loop .
    Last edited by sagar474; 09-15-2011 at 05:12 AM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So do like I showed you in the other thread, and use a loop. Or use something like memset, but at this point you should probably learn how to do it manually with a loop. Once you are comfortable with that, switch it out for memset so you know how that works.


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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you should certainly read the FAQ on why using feof() to control a loop is bad.

    Also, you have a massive memory leak, each calloc needs a free(), and you have none.

    Third, it's also comparatively expensive to clear ALL the bytes of a string when all you really need to do is
    string[a] = '\0';
    when you're done.

    You should also drop the use of 'register' and using #define to rename functions.
    You're never going to guess correctly what is best to put in a register any better than an optimising compiler.
    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.

  6. #6
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Quote Originally Posted by Salem View Post
    You're never going to guess correctly what is best to put in a register any better than an optimising compiler.
    why shoudent i use register keyword ? I used them to store counter variables and a single char. where should I use it ?

    to clear the string with this I need to write a loop.
    string[a] = '\0';

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    40 years ago when there was only one C compiler in the world, and it didn't do much apart from actual literal compiling, "register" was an easy optimisation hint.

    Now scroll forward to today.
    Optimize Options - Using the GNU Compiler Collection (GCC)
    There are so many things a modern compiler can detect and improve that random programmer guesses don't really come into it.
    What is more, this happens EVERY time you compile, not whenever you feel the need to look at your code in detail.

    Your use of "register" is at best a static guess. You're certainly not going to be sitting staring at the code for hours every time you make a change to decide whether your use of "register" is the best possible. For a start, you don't even know how many registers are available on your machine - if you have only 2 registers, then putting "register" on 3 variables creates a bit of a problem - no?
    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.

  8. #8
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Thank you I fixed the problem of clearing the string. there is no need to clear a string in my program at all. instead of clearing a string i just used variable a to track the size of string.

    but as suggested by salem feof is not a good idea to test the loop. Instead how can I terminate the loop in order to replace feof can i use c!=EOF ?
    Last edited by sagar474; 09-15-2011 at 05:39 AM.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by sagar474 View Post
    but as suggested by salem feof is not a good idea to test the loop. Instead how can I terminate the loop in order to replace feof can i use c!=EOF ?
    Read: Why feof() is bad-FAQ, in there it shows the correct way to do this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sagar474 View Post
    Thank you I fixed the problem of clearing the string. there is no need to clear a string in my program at all. instead of clearing a string i just used variable a to track the size of string.
    Yeah you were told that in your first thread too: make a string empty in C


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a good idea
    By luoyangke in forum C Programming
    Replies: 6
    Last Post: 12-02-2009, 12:03 PM
  2. Are namespaces a good idea?
    By Crilston in forum Game Programming
    Replies: 6
    Last Post: 06-24-2005, 06:30 PM
  3. good or bad game idea you vote?
    By L_I_Programmer in forum Game Programming
    Replies: 3
    Last Post: 03-15-2003, 07:14 AM
  4. Is my career idea good?
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-06-2002, 01:01 PM
  5. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 12:26 PM