shorten the string

This is a discussion on shorten the string within the C Programming forums, part of the General Programming Boards category; Hi everyone, if I had a char array and I want to somehow shorten it. what is the most easier ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    58

    shorten the string

    Hi everyone,

    if I had a char array and I want to somehow shorten it. what is the most easier way to do it? was it copy to another string? or is there a function to shorten it??


    eg. ABCDEFG
    I only need it to be ABCD.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    put \0 after the last char you still want in your string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Mmm, that would be somewhat of a memory leak. I'd copy it to a new string of a certain length and free the old one, but of course, this means you need to use malloc instead of static memory.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Mmm, that would be somewhat of a memory leak
    O, yeah, really? And what part of the memory is exactly leaking?

    Code:
    int a[2] = {0,1};
    Is also makes a memory leak?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Code:
    int a[2] = {0,1};
    Not even in the same context as strings but ...

    The premise behind a terminating zero is that, in strings, "beyond this point is garbage." Since you would never access anything beyond that then why keep it around? It's really a rather simple idea to be smart instead of clever. And also, if you do keep accessible data after a terminating zero and then royally mess up your indexing, you've hidden a seg fault.
    Last edited by whiteflags; 11-13-2006 at 02:15 AM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by citizen
    Since you would never access anything beyond that
    The difference between leak and unused memory lives here - would never and could never

    If you could never access the memory - it is a leak.

    If you would never acess this memory - it just your preference of the working.

    You can try to look at the strtok code to learn some new for you ideas of acessing memory after the '\0' charachter.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Meh.

    > You can try to look at the strtok code to learn some new for you ideas of acessing memory after the '\0' charachter.
    Or not worry about it and avoid screwing up my strings the way strtok does.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by citizen
    avoid screwing up my strings the way strtok does.
    Still it just your way of working.

    But putting any value inside the string even if it is zero does not produce the memory leak.

    [code]
    char a[100] = "A";
    [code]

    98 bytes are not leaked - they are just not used yet.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Then I apologize. It isn't a memory leak, just a bad practice that is as bad as one.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,342
    Quote Originally Posted by citizen
    Then I apologize. It isn't a memory leak, just a bad practice that is as bad as one.
    I disagree with you that it is bad practice. In fact, it is quite useful at times. For example;
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
         char buffer[100];
         char *temp;
         while (fgets(buffer, 100, stdin) != EOF)
         {
               temp = strchr(buffer, '\n');
               if (temp != NULL) *temp = '\0';    /*  buffer now has the input characters without the newline */
               /*   do something with this part of the line */
         }
    }
    This code allows inout to be read from stdin, one line at a time. If any line is long enough to overflow the buffer, the code copes by readin the line in pieces. The overwrite of *temp actually truncates buffer, as far as standard string functions are concerns (to strip off any trailing '\n'; if there is no trailing '\n' that is a sign of a long line). However, this does not cause a memory leak --- buffer still remains an array of 100 chars, so the next call to fgets() can happily read to the whole buffer without causing undefined behaviour.

    While, yes, it would be possible to achieve the same effect by reading one character at a time from stdin, and dynamically allocating memory, there is no need to.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Isn't that a bit different from what we're actually doing here, since streams like stdin are supposed to be continuous in the first place?

    There may be more data in the stream, but you aren't purposely shoving it in a buffer after the terminating zero.

    Data after a string's terminating zero is just wrong. I won't debate it any further.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    > Data after a string's terminating zero is just wrong. I won't debate it any further.

    If you keep the size of the char[] you can probably look at it as an hack. However it is ugly, I agree.

    Also in no way it answers the original OP question, which was how to shorten a string, not how to make a string look smaller.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A string is zero or more characters terminated by a null character. Thus, if you have a string of more than zero non-null characters, by moving the null closer to the start of the string you have effectively shortened your string. Pretty simple.

    He asked how to shorten a string, not how to reallocate a smaller block of memory and copy a string into it. That would be creating a new string. Not shortening an existing one.

    It can be argued either way.


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

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    std::vector regularly allocates more memory than it uses at the moment. Is making an algorithm O(1) wrong? Or would you have a "clean", O(n) dynamic array algorithm?

    a bad practice
    Data after a string's terminating zero is just wrong. I won't debate it any further.
    Please explain, elaborate on and defend your comments, which I consider rash and inappropriate. You don't need to debate it, but you do need to have solid ground to stand on.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    But is is still a strange thing to have

    Code:
    char a[] = "look ma! No segfault!";
    a[8] = '\0';
    printf("%s%c", a, '\n');
    printf(a + 9);
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21