Thread: A mind for program look clear and resource quickly release and easy error process

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    23

    A mind for program look clear and resource quickly release and easy error process

    Code:
    /*My english is not good*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #include<string.h>
    int main(int argc, char *argv[])
    {
      char * temp1;
      char * temp2;
      char * temp3;
      temp1=(char *)malloc(7);/******Look clear and resource quickly release********/
      /*  ......  */
      /*If  error goto labeltemp1 */
      temp2=(char *)malloc(7);/***Look clear and resource quickly release*****/
      /*  ......  */
      /*If  error goto labeltemp2 */
      temp1[0]='H';  temp1[1]='e';  temp1[2]='l';  temp1[3]='l';  temp1[4]='o';  temp1[5]=' ';  temp1[6]=0;
      temp2[0]='w';  temp2[1]='o';  temp2[2]='r';  temp2[3]='l';  temp2[4]='d';  temp2[5]='.';  temp2[6]=0;
      printf("%s",temp1);
      temp3=temp2;
      temp2=temp1;
      temp1=temp3;
     labeltemp2:
      free(temp2);            /***Look clear and resource quickly release*****/
      printf("%s",temp1);
     labeltemp1:
      free(temp1);            /******Look clear and resource quickly release********/
      return 0;
    }
    
    
    /*
    
    I think
    
    malloc temp1
    malloc temp2
    ......
    free temp2
    free temp1
    
    look clear than
    
    malloc temp1
    malloc temp2
    ......
    free temp1
    free temp2
    
    
    and error process is ease to use goto
    
    
     */

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well as an example of how to make the code look complicated, it achieves it's aim.

    It's also broken as well.
    Code:
     labeltemp2:
      free(temp2);            /***Look clear and resource quickly release*****/
      printf("%s",temp1);
    If your previous 'goto labeltemp2' actually happened due to malloc failing (as suggested by the comment), then temp1 would still contain garbage data which would be printed.



    If you want something clearer, perhaps
    Code:
    #include<stdio.h>
    #include<stdlib.h>  /* malloc lives here */
    #include<string.h>
    int main(int argc, char *argv[])
    {
      char * temp1;
      char * temp2;
      temp1 = malloc(7); /* casts are not necessary if you're compiling with a C compiler */
      temp2 = malloc(7);
      if ( temp1 != NULL && temp2 != NULL ) {
        /* all resources acquired, do the work */
        strcpy(temp1,"hello ");
        strcpy(temp2,"world.");
        printf("%s%s\n", temp1, temp2);
      }
      free(temp1);  /* free(NULL) is a safe thing to do */
      free(temp2);  /* in case only one of the malloc's failed */
      return 0;
    }
    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.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    Well as an example of how to make the code look complicated, it achieves it's aim.

    It's also broken as well.
    Code:
     labeltemp2:
      free(temp2);            /***Look clear and resource quickly release*****/
      printf("%s",temp1);
    If your previous 'goto labeltemp2' actually happened due to malloc failing (as suggested by the comment), then temp1 would still contain garbage data which would be printed.



    If you want something clearer, perhaps
    Code:
    #include<stdio.h>
    #include<stdlib.h>  /* malloc lives here */
    #include<string.h>
    int main(int argc, char *argv[])
    {
      char * temp1;
      char * temp2;
      temp1 = malloc(7); /* casts are not necessary if you're compiling with a C compiler */
      temp2 = malloc(7);
      if ( temp1 != NULL && temp2 != NULL ) {
        /* all resources acquired, do the work */
        strcpy(temp1,"hello ");
        strcpy(temp2,"world.");
        printf("%s%s\n", temp1, temp2);
      }
      free(temp1);  /* free(NULL) is a safe thing to do */
      free(temp2);  /* in case only one of the malloc's failed */
      return 0;
    }
    The hello world program is a cursory,I have writed a program about print long long int type use list before some days,now post it and can ignore debug code in program.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/stat.h>
    #include<unistd.h>
    #include<io.h>
    #include<malloc.h>
    #include<time.h>
    #include<string.h>
    #define MYDEBUG0
    #ifdef  MYDEBUG0
    #define DEBUGTEMP1 debugtemp1
    #define DEBUGTEMP2 debugtemp2
    #define DECLMEMVAR(x) long long x=0;if(DEBUGTEMP2!=0){printf("Memory leakXXXXXXXXXXX   ");printll(DEBUGTEMP2);printf("    \r\n");}else{}
    #define DECLMEMVAR2(x) long long x=0;
    DECLMEMVAR2(DEBUGTEMP2)
    #define MYDEBUGM1(x)      DEBUGTEMP1+=(long long)x;
    #define MYDEBUGF1(x)      DEBUGTEMP1-=(long long)x;
    #define REPORTDEBUG2 if(DEBUGTEMP1!=0){tempreturn=-10;DEBUGTEMP2++;}else{}
    #else
    #define DECLMEMVAR(x)
    #define DECLMEMVAR2(x)
    #define MYDEBUGM1(x)
    #define MYDEBUGF1(x)
    #define REPORTDEBUG2
    #endif
    struct str1
    {
      unsigned char thevalue;
      struct str1 * theprior;
    };
    int printll(long long arg1)
    {
      DECLMEMVAR2(DEBUGTEMP1)
      struct str1 * temp3;
      struct str1 * temp4;
      int tempreturn;
      long long temp6;
      tempreturn=0;
      goto onlytemp0;
     onlytemp0:
      if(arg1==0)
        {
          printf("0");
          goto labeltemp0;
        }
      else
        {
          temp6=arg1;
        }
      temp3=(struct str1 *)malloc( sizeof(struct str1) );
      if(temp3==NULL)
        {
          printf("Malloc error.\r\n");
          tempreturn=-1;
          goto labeltemp0;
        }
      else
        {
          temp3->theprior=0;
          MYDEBUGM1(temp3)
        }
      goto onlytemp3;
     onlytemp3:
      if(arg1<0)
        {
          temp6++;
          temp6*=-1;
          temp3->thevalue= (unsigned char) (temp6%10);
          if(temp3->thevalue==9)
            {
              temp3->thevalue=0;
            }
          else
            {
              (temp3->thevalue)++;
            }
          temp6=arg1;
          temp6=(long long)(temp6/10);
          temp6*=-1;
        }
      else
        {
          temp3->thevalue=(unsigned char) (temp6%10);
          temp6=(long long)(temp6/10);
        }
      while(temp6!=0)
        {
          goto onlyloop1;
        onlyloop1:
          temp4=(struct str1 *)malloc( sizeof(struct str1) );
          if(temp4==NULL)
            {
              printf("Malloc error.\r\n");
              temp6=0;
              tempreturn=-1;
              goto labelloop1;
            }
          else
            {
              MYDEBUGM1(temp4)
                ;
            }
          temp4->theprior=temp3;
          temp3=temp4;
          temp3->thevalue=(int) (temp6%10);
          temp6=(long long)(temp6/10);
        labelloop1:
          ;
        }
      if(tempreturn==-1)
        {
          goto labeltemp3;
        }
      else if(tempreturn==1)
        {
        }
      else
        {
        }
     labeltemp3:
      if(tempreturn==0)
        {
          if(arg1<0)
            {
              printf("-");
            }
        }
      while(temp3->theprior != 0)
        {
          if(tempreturn==0)
            {
              printf("%d",temp3->thevalue);
            }
          temp4=temp3->theprior;
          MYDEBUGF1(temp3)
          free(temp3);
          temp3=temp4;
        }
      if(tempreturn==0)
        {
          printf("%d",temp3->thevalue);
        }
      MYDEBUGF1(temp3)
          free(temp3);
      goto labeltemp0;
     labeltemp0:
      if(tempreturn==-1)
        {
          goto labelreturn;
        }
      else if(tempreturn==1)
        {
        }
      else
        {
        }
     labelreturn:
      if(tempreturn==-1)
        {
        }
      else if(tempreturn==1)
        {
        }
      else
        {
        }
      REPORTDEBUG2
      return tempreturn;
    }
    #ifdef  MYDEBUG0
    #define MYDEBUGM(x)      DEBUGTEMP1+=(long long)x;printf("----Malloc---------");printll(DEBUGTEMP1);printf("\r\n");
    #define MYDEBUGF(x)      DEBUGTEMP1-=(long long)x;printf("----Free-----------");printll(DEBUGTEMP1);printf("\r\n");
    #define REPORTDEBUG1 if(DEBUGTEMP1!=0){tempreturn=-10;DEBUGTEMP2++;printf("Memory leakXXXXXXXXXXX   ");printll(DEBUGTEMP2);printf("    \r\n");}else{}
    #else
    #define MYDEBUGM(x)
    #define MYDEBUGF(x)
    #define REPORTDEBUG1
    #endif
    int main(int argc, char *argv[])
    {
      DECLMEMVAR(DEBUGTEMP1)
        int tempreturn;
      long long temp1;
      tempreturn =0;
      temp1=0;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=1;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=2;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=3;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=4;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=-1;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=-2;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=-3;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=-4;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=-123789452664;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=+123789452664;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      temp1=-9223372036854775808;
      if( printll(temp1)!=0 )
        {
          printf("\r\n");
          printf("Memory leak.\r\n");
        }
      else
        {
          printf("\r\n");
        }
      REPORTDEBUG1
      return tempreturn;
    }

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code not good much you writed these days too. You language might with to find own forum.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by algorism View Post
    Code not good much you writed these days too. You language might with to find own forum.
    I think this mind let program look clear and resource quickly release and easy error process.
    The key things is ----pointer swap---- and -----very much goto----- ,

    ---pointer swap---

    temp3=temp2; temp2=temp1; temp1=temp3;

    let

    malloc temp1 malloc temp2 free temp1 free temp2
    ==>
    malloc temp1 malloc temp2 free temp2 free temp1

    so a object's life not go out early object's life ,look clear,and release resource quickly .


    ----goto----- let error process easy

  6. #6
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    A mind for program  look clear and resource quickly release and easy error process-cprogcs1-jpg

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Of you that to more seem but why if not true? Turnips!

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OP why exactly do you need a lot of gotos? There is a misunderstanding here.

    It is true that something like this can be used. Did you see code of this form somewhere and thought it would be good for your code?
    Code:
    /* some work that may fail */
    goto ErrorCleanup;
    
    /* somewhere near the exit of the function */
    if (0) {  
    ErrorCleanup:
       /* report the error */
       /* do the cleanup */
    }
    return;
    This is to make error cleanup tidy, but you only get the benefits if these other things are true:

    • You will need to break out of deeply nested curly braces.
    • The cleanup that would happen at the end of the function is not normal.
    • You are trying to write functions with single exit points (meaning one and only one return statement)


    There are many ways to handle errors, not just this one.

    Try using more hand written functions. For example, you often will allocate a node, fill it with some place value number, and then link it with a bigger list. You can group these operations as well. This often clarifies where you report errors and what you need to check in order to run well.

    Code:
    longsllist * createNode (long init) {
    longsllist *node = malloc(sizeof *node);
    if (!node) {
       printf("Could not acquire new node\n");
    }
    node->value = init;
    node->next = NULL;
    
    return node;
    }
    
    
    longsllist * push(longsllist *theList, longsllist *node) {
       node->next = theList;
    
       return node;
    }
    
    /* code could look like this: */
    long sample = 48247;
    longsllist *digits = createNode(sample % 10);
       if (digits) {
           sample /= 10;
           while (sample > 0) {
              longsllist *node = createNode(sample % 10);
              if (node) {
                digits = push(digits, node);
                sample /= 10;  
              }
           }
       }
    Last edited by whiteflags; 01-12-2017 at 07:27 PM.

  9. #9
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by whiteflags View Post
    OP why exactly do you need a lot of gotos? There is a misunderstanding here.

    It is true that something like this can be used. Did you see code of this form somewhere and thought it would be good for your code?
    Code:
    /* some work that may fail */
    goto ErrorCleanup;
    
    /* somewhere near the exit of the function */
    if (0) {  
    ErrorCleanup:
       /* report the error */
       /* do the cleanup */
    }
    return;
    This is to make error cleanup tidy, but you only get the benefits if these other things are true:

    • You will need to break out of deeply nested curly braces.
    • The cleanup that would happen at the end of the function is not normal.
    • You are trying to write functions with single exit points (meaning one and only one return statement)


    There are many ways to handle errors, not just this one.

    Try using more hand written functions. For example, you often will allocate a node, fill it with some place value number, and then link it with a bigger list. You can group these operations as well. This often clarifies where you report errors and what you need to check in order to run well.

    Code:
    longsllist * createNode (long init) {
    longsllist *node = malloc(sizeof *node);
    if (!node) {
       printf("Could not acquire new node\n");
    }
    node->value = init;
    node->next = NULL;
    
    return node;
    }
    
    
    longsllist * push(longsllist *theList, longsllist *node) {
       node->next = theList;
    
       return node;
    }
    
    /* code could look like this: */
    long sample = 48247;
    longsllist *digits = createNode(sample % 10);
       if (digits) {
           sample /= 10;
           while (sample > 0) {
              longsllist *node = createNode(sample % 10);
              if (node) {
                digits = push(digits, node);
                sample /= 10;  
              }
           }
       }
    The mind is to try one's best auto very very quickly release resource,so not

    ErrorCleanup:

    if temp1!=0 then free temp1
    if temp2!=0 then free temp2
    if temp3!=0 then free temp3
    .
    .
    .

    should like that above hello world print program,
    the list program is cursory,I didn't think very much,I think it can improved

  10. #10
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by algorism View Post
    Of you that to more seem but why if not true? Turnips!
    In some angles I think the mind is so not bad,

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    should like that above hello world print program,
    the list program is cursory,I didn't think very much,I think it can improved

    Is your topic not a question but a programming idea?

    The mind is to try one's best auto very very quickly release resource,
    I've never put too much thought into it, but I always tried to allocate and free resources in the same place. If I allocated in main, it meant freeing when I left main. If I allocated in some other function, it meant freeing there too. It kept things organized, and it was "quick" enough for me. Maybe I don't understand why you would want to use your idea though.

    Your idea seems to only create code that looks harder than what it actually does.

  12. #12
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by whiteflags View Post

    Is your topic not a question but a programming idea?


    I've never put too much thought into it, but I always tried to allocate and free resources in the same place. If I allocated in main, it meant freeing when I left main. If I allocated in some other function, it meant freeing there too. It kept things organized, and it was "quick" enough for me. Maybe I don't understand why you would want to use your idea though.

    Your idea seems to only create code that looks harder than what it actually does.


    The topic can say a programming idea,
    The idea call attention to someone if he chase speed,at the same time, pay attention to clear,if he chase clear,at the same time, pay attention to speed, to try one's best find a way to balance,after all,the resource is very very rarity, no matter human energy or computer memory and so on

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The OP needs to learn how to program before doing anything else.

    Simply knowing the keywords isn't enough.

    The code is riddled with pointless goto statements and far too many meaningless variable names called 'temp'.

    Take this little gem.
    Code:
      goto onlytemp0;
     onlytemp0:
      if(arg1==0)
        {
          printf("0");
          goto labeltemp0;
        }
    A goto to the very next line - ugh!!!!!
    Now ask yourself, if arg1 is zero, what stops this botched loop from printing an infinite number of zeros?
    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.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The idea call attention to someone if he chase speed,at the same time, pay attention to clear,if he chase clear,at the same time, pay attention to speed, to try one's best find a way to balance,after all,the resource is very very rarity, no matter human energy or computer memory and so on
    Most of your code only hides the fact that freeing at the end of the function is exactly what you wanted to happen. Code isn't faster just because you used a jump. It's often better if you just forgot about jumping and wrote something normal with ifs or loops.

  15. #15
    Registered User
    Join Date
    Jan 2017
    Posts
    23
    Quote Originally Posted by Salem View Post
    The OP needs to learn how to program before doing anything else.

    Simply knowing the keywords isn't enough.

    The code is riddled with pointless goto statements and far too many meaningless variable names called 'temp'.

    Take this little gem.
    Code:
      goto onlytemp0;
     onlytemp0:
      if(arg1==0)
        {
          printf("0");
          goto labeltemp0;
        }
    A goto to the very next line - ugh!!!!!
    Now ask yourself, if arg1 is zero, what stops this botched loop from printing an infinite number of zeros?
    Some goto is mnemonic designation, onlytempxxx tempxxx label is signing the zone of goto in them, goto onlytemp0;onlytemp0: is writed to avoid gcc warning,
    meaningless variable names called 'temp' in some reason because pointer swap can let the variable change meaning,
    botched loop perhaps can not avoid in the face of the complex world.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lnk 2005 error in release only
    By harryGrat in forum Windows Programming
    Replies: 1
    Last Post: 02-11-2009, 09:41 AM
  2. release resource for WebClient class
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-02-2008, 01:14 PM
  3. is there easy way to clear nested vector?
    By timchen in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2007, 10:18 PM
  4. MFC: LineDraw Release Error
    By mrafcho001 in forum Windows Programming
    Replies: 1
    Last Post: 06-25-2005, 10:24 AM
  5. my very cool new program has a mind boggling probem!!!
    By face_master in forum C++ Programming
    Replies: 11
    Last Post: 12-20-2001, 04:37 PM

Tags for this Thread