Thread: now a question about strings

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    now a question about strings

    hey all, I posted some questions last night about bit fields which I got some nice answers for, now I have a question about the way I am treating strings in my program.

    basically I am re-using strings (in a loop). I am not zeroing them, I am simply writing to them with the addresses of their first elements and appending NULL terminators, when they may already contain data. everything is fine in my program, I don't get any errors or funky results, however I am just wondering what is happening to the data that they may already contain when I write to them again.

    do the bytes they contain get pushed aside (could possibly cause memory leaks, hence the reason I am asking), or will they just get overwritten by the new bytes? I'm thinking they are just getting overwritten, hence the reason why I'm not getting funky results, but would just like to make sure this is what is happening, since I do plan on releasing this project as open source, I don't want to release a source containing possible memory leaks.

    any help here would be greatly appreciated. thank you in advance!
    Last edited by Bleech; 11-13-2006 at 09:07 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This will depend on what you mean by "reusing strings". Not a leak:
    Code:
    char buf[ BUFSIZ ];
    
    strcpy( buf, "some stuff here" );
    ...
    strcpy( buf, "something else" );
    ...
    strcpy( buf, "foo" );
    All of the above is fine, you're just reusing the buffer. String functions will usually stop upon reaching the null terminator (except for fgets, for example).
    Code:
    char *ptr;
    
    ptr = malloc( BUFSIZ );
    strcpy( ptr, "something here" );
    ...
    ptr = malloc( BUFSIZ );
    Here you're reusing the pointer, but you didn't free the old allocated space first. That constitutes a leak. This doesn't:
    Code:
    ptr = malloc( BUFSIZ );
    strcpy( ptr, "something here" );
    ...
    strcpy( ptr, "foo" );
    Here you're just reusing the same allocated space.

    If you're still confused, provide a small sample, preferrably a small compilable program, as to what it is you're doing. Try to keep it a small illustration.


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

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    A simple way to detect leaks is to run the code in question several hundred million times. Many (but not all) common, serious leaks will manifest themselves this way. Or use Valgrind.
    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;}

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by quzah
    This doesn't [constitute a leak]:
    Code:
    ptr = malloc( BUFSIZ );
    strcpy( ptr, "something here" );
    ...
    strcpy( ptr, "foo" );
    Here you're just reusing the same allocated space.
    Someone in another thread of the OP's said that that code was a leak.

    To clarify, a leak is when you discard an malloc()ed pointer without freeing it. Reusing a pointer that has been allocated isn't a leak. However, it might result in extra memory allocated. For example, after Quzah's code that I have quoted has been run, ptr contains "foo", but it points to more memory than is required to hold "foo".

    It's not a memory leak, more a memory inefficiency.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strings in c
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 06:32 PM
  2. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  3. strings question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 07:28 AM
  4. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-15-2005, 12:14 AM