Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. from the index pos in ch[],delete the the string of len

from the index pos in ch[],delete the the string of len

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabase
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wbgxx
    wrote on last edited by
    #1

    I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!

    void StrDelete1(char ch[], int pos,int len)
    /*from the index pos in ch[],delete the the string of len*/
    {
    if (pos<0 || pos >(MAX-len))
    {
    printf("the string delete error!");
    exit(1);
    }
    int i;
    for (i=0; i<MAX; i++)
    printf("%c", ch[i]);

    char \*temp = (char\*)malloc(sizeof(char));
    if (temp == NULL)
    {
       exit(1);
    }
    for (i=pos; i<MAX-len; i++)
    {
        temp = &ch\[i\];
         free(temp);
    }
    
    printf("\\n");
       for(i=0; i<MAX; i++)
    printf("%c", ch\[i\]);
    

    }

    void StrDelete2(char ch[], int pos,int len)
    {
    if (pos<0 || pos >(MAX-len))
    {
    printf("the string delete error!");
    exit(1);
    }
    int i;
    printf("\n");
    for (i=0; i<MAX; i++)
    printf("%c", ch[i]);

    char \*temp = (char\*)malloc(MAX-len);
    if (temp == NULL)
    {
       exit(1);
    }
    for (i=0; i<pos; i++)
    temp\[i\] = ch\[i\];
    for (i=pos; i<MAX-len; i++)
    {
        temp\[i\]=ch\[i+len\];
    }
    free(ch);
    printf("\\n");
    for (i=0; i<MAX-len; i++)
    printf("%c", temp\[i\]);
    

    }

    Richard Andrew x64R G E D 4 Replies Last reply
    0
    • W wbgxx

      I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!

      void StrDelete1(char ch[], int pos,int len)
      /*from the index pos in ch[],delete the the string of len*/
      {
      if (pos<0 || pos >(MAX-len))
      {
      printf("the string delete error!");
      exit(1);
      }
      int i;
      for (i=0; i<MAX; i++)
      printf("%c", ch[i]);

      char \*temp = (char\*)malloc(sizeof(char));
      if (temp == NULL)
      {
         exit(1);
      }
      for (i=pos; i<MAX-len; i++)
      {
          temp = &ch\[i\];
           free(temp);
      }
      
      printf("\\n");
         for(i=0; i<MAX; i++)
      printf("%c", ch\[i\]);
      

      }

      void StrDelete2(char ch[], int pos,int len)
      {
      if (pos<0 || pos >(MAX-len))
      {
      printf("the string delete error!");
      exit(1);
      }
      int i;
      printf("\n");
      for (i=0; i<MAX; i++)
      printf("%c", ch[i]);

      char \*temp = (char\*)malloc(MAX-len);
      if (temp == NULL)
      {
         exit(1);
      }
      for (i=0; i<pos; i++)
      temp\[i\] = ch\[i\];
      for (i=pos; i<MAX-len; i++)
      {
          temp\[i\]=ch\[i+len\];
      }
      free(ch);
      printf("\\n");
      for (i=0; i<MAX-len; i++)
      printf("%c", temp\[i\]);
      

      }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Here, you are freeing memory that the function didn't allocate:

      for (i=pos; i<MAX-len; i++)
      {
      temp = &ch[i];
      free(temp);
      }

      Why are you calling free there?

      W 1 Reply Last reply
      0
      • W wbgxx

        I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!

        void StrDelete1(char ch[], int pos,int len)
        /*from the index pos in ch[],delete the the string of len*/
        {
        if (pos<0 || pos >(MAX-len))
        {
        printf("the string delete error!");
        exit(1);
        }
        int i;
        for (i=0; i<MAX; i++)
        printf("%c", ch[i]);

        char \*temp = (char\*)malloc(sizeof(char));
        if (temp == NULL)
        {
           exit(1);
        }
        for (i=pos; i<MAX-len; i++)
        {
            temp = &ch\[i\];
             free(temp);
        }
        
        printf("\\n");
           for(i=0; i<MAX; i++)
        printf("%c", ch\[i\]);
        

        }

        void StrDelete2(char ch[], int pos,int len)
        {
        if (pos<0 || pos >(MAX-len))
        {
        printf("the string delete error!");
        exit(1);
        }
        int i;
        printf("\n");
        for (i=0; i<MAX; i++)
        printf("%c", ch[i]);

        char \*temp = (char\*)malloc(MAX-len);
        if (temp == NULL)
        {
           exit(1);
        }
        for (i=0; i<pos; i++)
        temp\[i\] = ch\[i\];
        for (i=pos; i<MAX-len; i++)
        {
            temp\[i\]=ch\[i+len\];
        }
        free(ch);
        printf("\\n");
        for (i=0; i<MAX-len; i++)
        printf("%c", temp\[i\]);
        

        }

        G Offline
        G Offline
        Gwenio
        wrote on last edited by
        #3

        void StrDelete1(char ch[], int pos,int len)
        /*from the index pos in ch[],delete the the string of len*/
        {
        if (pos<0 || pos >(MAX-len)) {
        printf("the string delete error!");
        exit(1);
        }
        int i;
        for (i=0; i

        First off, you should remove "char *temp = (char*)malloc(sizeof(char));". The allocated memory is never used, and it is never released.

        To my knowledge, free() releases a block of memory, not just that at the address pointed to. Therefore what you are trying to do may not be possible. I suggest you either call free() on the array (if it is allocated with malloc()) and release it, or set the element at pos to '\0'.

        Also, you are accessing memory you are trying to free at the end of the function. Do not do that.

        EDIT:

        Okay, I think I have figured out away to accomplish you goal. Here is a sample to get you started in the right direction (you may want to revise it):

        char * DeleteString1(char * ch,int pos,int len) {
        memmove(ch+pos,ch+pos+len,strlen(ch)-(pos+len));//Move content past pos+len to pos.
        return realloc(ch,strlen(ch));//Return a downsized memory block - should be the same as ch, but it is good to be sure.
        }

        modified on Wednesday, April 7, 2010 8:27 PM

        K 1 Reply Last reply
        0
        • W wbgxx

          I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!

          void StrDelete1(char ch[], int pos,int len)
          /*from the index pos in ch[],delete the the string of len*/
          {
          if (pos<0 || pos >(MAX-len))
          {
          printf("the string delete error!");
          exit(1);
          }
          int i;
          for (i=0; i<MAX; i++)
          printf("%c", ch[i]);

          char \*temp = (char\*)malloc(sizeof(char));
          if (temp == NULL)
          {
             exit(1);
          }
          for (i=pos; i<MAX-len; i++)
          {
              temp = &ch\[i\];
               free(temp);
          }
          
          printf("\\n");
             for(i=0; i<MAX; i++)
          printf("%c", ch\[i\]);
          

          }

          void StrDelete2(char ch[], int pos,int len)
          {
          if (pos<0 || pos >(MAX-len))
          {
          printf("the string delete error!");
          exit(1);
          }
          int i;
          printf("\n");
          for (i=0; i<MAX; i++)
          printf("%c", ch[i]);

          char \*temp = (char\*)malloc(MAX-len);
          if (temp == NULL)
          {
             exit(1);
          }
          for (i=0; i<pos; i++)
          temp\[i\] = ch\[i\];
          for (i=pos; i<MAX-len; i++)
          {
              temp\[i\]=ch\[i+len\];
          }
          free(ch);
          printf("\\n");
          for (i=0; i<MAX-len; i++)
          printf("%c", temp\[i\]);
          

          }

          E Offline
          E Offline
          Eugen Podsypalnikov
          wrote on last edited by
          #4

          What would you like to see ? :) A possible task could be: 1) Given: "abcdef" 2) Cut: 2 characters from the position 3 3) Result: "abcf"

          virtual void BeHappy() = 0;

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            Here, you are freeing memory that the function didn't allocate:

            for (i=pos; i<MAX-len; i++)
            {
            temp = &ch[i];
            free(temp);
            }

            Why are you calling free there?

            W Offline
            W Offline
            wbgxx
            wrote on last edited by
            #5

            "Why are you calling free there?" Yeah ,I only want to delete the number in the arry,Is it delete the memory in the stack??

            G 1 Reply Last reply
            0
            • G Gwenio

              void StrDelete1(char ch[], int pos,int len)
              /*from the index pos in ch[],delete the the string of len*/
              {
              if (pos<0 || pos >(MAX-len)) {
              printf("the string delete error!");
              exit(1);
              }
              int i;
              for (i=0; i

              First off, you should remove "char *temp = (char*)malloc(sizeof(char));". The allocated memory is never used, and it is never released.

              To my knowledge, free() releases a block of memory, not just that at the address pointed to. Therefore what you are trying to do may not be possible. I suggest you either call free() on the array (if it is allocated with malloc()) and release it, or set the element at pos to '\0'.

              Also, you are accessing memory you are trying to free at the end of the function. Do not do that.

              EDIT:

              Okay, I think I have figured out away to accomplish you goal. Here is a sample to get you started in the right direction (you may want to revise it):

              char * DeleteString1(char * ch,int pos,int len) {
              memmove(ch+pos,ch+pos+len,strlen(ch)-(pos+len));//Move content past pos+len to pos.
              return realloc(ch,strlen(ch));//Return a downsized memory block - should be the same as ch, but it is good to be sure.
              }

              modified on Wednesday, April 7, 2010 8:27 PM

              K Offline
              K Offline
              KarstenK
              wrote on last edited by
              #6

              real fine solution :thumbsup: i wouldnt realloc for performance reasons :rolleyes:

              Press F1 for help or google it. Greetings from Germany

              G 1 Reply Last reply
              0
              • K KarstenK

                real fine solution :thumbsup: i wouldnt realloc for performance reasons :rolleyes:

                Press F1 for help or google it. Greetings from Germany

                G Offline
                G Offline
                Gwenio
                wrote on last edited by
                #7

                Given the sample code given, I assumed they want to release memory, which realloc would do. However, it is true that doing so is not needed unless memory is in short supply.

                K 1 Reply Last reply
                0
                • W wbgxx

                  "Why are you calling free there?" Yeah ,I only want to delete the number in the arry,Is it delete the memory in the stack??

                  G Offline
                  G Offline
                  Gwenio
                  wrote on last edited by
                  #8

                  No, it only releases memory allocated on the heap. To release memory on the stack you have to use assembly langauge (in which case things get complicated) or let it go out of scope (like you should).

                  1 Reply Last reply
                  0
                  • W wbgxx

                    I try to do this but the first function is error ,I don not know why ,could someone help me ,thank in advantage!!

                    void StrDelete1(char ch[], int pos,int len)
                    /*from the index pos in ch[],delete the the string of len*/
                    {
                    if (pos<0 || pos >(MAX-len))
                    {
                    printf("the string delete error!");
                    exit(1);
                    }
                    int i;
                    for (i=0; i<MAX; i++)
                    printf("%c", ch[i]);

                    char \*temp = (char\*)malloc(sizeof(char));
                    if (temp == NULL)
                    {
                       exit(1);
                    }
                    for (i=pos; i<MAX-len; i++)
                    {
                        temp = &ch\[i\];
                         free(temp);
                    }
                    
                    printf("\\n");
                       for(i=0; i<MAX; i++)
                    printf("%c", ch\[i\]);
                    

                    }

                    void StrDelete2(char ch[], int pos,int len)
                    {
                    if (pos<0 || pos >(MAX-len))
                    {
                    printf("the string delete error!");
                    exit(1);
                    }
                    int i;
                    printf("\n");
                    for (i=0; i<MAX; i++)
                    printf("%c", ch[i]);

                    char \*temp = (char\*)malloc(MAX-len);
                    if (temp == NULL)
                    {
                       exit(1);
                    }
                    for (i=0; i<pos; i++)
                    temp\[i\] = ch\[i\];
                    for (i=pos; i<MAX-len; i++)
                    {
                        temp\[i\]=ch\[i+len\];
                    }
                    free(ch);
                    printf("\\n");
                    for (i=0; i<MAX-len; i++)
                    printf("%c", temp\[i\]);
                    

                    }

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    wbgxx wrote:

                    ...but the first function is error...

                    Which means what exactly?

                    wbgxx wrote:

                    ...I don not know why...

                    Have you used the debugger to step through the code to find out why?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Man who follows car will be exhausted." - Confucius

                    W 1 Reply Last reply
                    0
                    • G Gwenio

                      Given the sample code given, I assumed they want to release memory, which realloc would do. However, it is true that doing so is not needed unless memory is in short supply.

                      K Offline
                      K Offline
                      KarstenK
                      wrote on last edited by
                      #10

                      this is an complicated theme, because the memory is from a bigger block which is in a list. So it can happen that only these function cost some CPU operations but also you get fragmented memory. :~

                      Press F1 for help or google it. Greetings from Germany

                      G 1 Reply Last reply
                      0
                      • D David Crow

                        wbgxx wrote:

                        ...but the first function is error...

                        Which means what exactly?

                        wbgxx wrote:

                        ...I don not know why...

                        Have you used the debugger to step through the code to find out why?

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "Man who follows car will be exhausted." - Confucius

                        W Offline
                        W Offline
                        wbgxx
                        wrote on last edited by
                        #11

                        Oh,I don not do this,sorry! I am freshman and does not use the debugger to step through the coder

                        1 Reply Last reply
                        0
                        • K KarstenK

                          this is an complicated theme, because the memory is from a bigger block which is in a list. So it can happen that only these function cost some CPU operations but also you get fragmented memory. :~

                          Press F1 for help or google it. Greetings from Germany

                          G Offline
                          G Offline
                          Gwenio
                          wrote on last edited by
                          #12

                          Yes, down sizing the memory is problematic, but that is one more reason my sample should be revised. It was not intended to be fully functional, just showcase what could be done to get the desired results. I spent less than a minute on it so it is likely full of problems; since the enquirer is a student thinking about improve it would be good practice.

                          K 1 Reply Last reply
                          0
                          • G Gwenio

                            Yes, down sizing the memory is problematic, but that is one more reason my sample should be revised. It was not intended to be fully functional, just showcase what could be done to get the desired results. I spent less than a minute on it so it is likely full of problems; since the enquirer is a student thinking about improve it would be good practice.

                            K Offline
                            K Offline
                            KarstenK
                            wrote on last edited by
                            #13

                            Yes it looks like a 'hot fix'. :rolleyes: And therefor makes an additional comment for that performance risk sense :-O

                            Press F1 for help or google it. Greetings from Germany

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups