Memory leak in copying one char to another

This is a discussion on Memory leak in copying one char to another within the C++ Programming forums, part of the General Programming Boards category; Code: do{ for (int i = 0;i <= New->PathNumbers;i++, ActualPath++) { if ( strcmp(";", New->Paths) == 0 ) { ActualPath++; ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Memory leak in copying one char to another

    Code:
    	do{
    		for (int i = 0;i <= New->PathNumbers;i++, ActualPath++)
    		{
    			if ( strcmp(";", New->Paths) == 0 )
    			{
    				ActualPath++;
    				i++;
    				break;
    			}
    			strcpy( &Path[i], &New->Paths[i] );
    		}
    Path is char*
    New->Paths is char*

    New->Paths has the string "Game" in it.

    Whats wrong with this?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, if you have a memory leak, maybe you're not deleteing something.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    sorry its not memory leak. I dont know what error is >.<

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    maybe its because it is always comparing New->Paths with ";". So if New-Paths == "game" then the loop will never do anything. Maybe this is what you want? strcmp() is overkill, just compare the characters.
    Code:
    	do{
    		for (int i = 0;i <= New->PathNumbers;i++, ActualPath++)
    		{
    			if ( New->Paths[i] == ';' )
    			{
    				ActualPath++;
    				i++;
    				break;
    			}
    			strcpy( &Path[i], &New->Paths[i] );
    		}

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for (int i = 0;i <= New->PathNumbers;i++, ActualPath++)
    Well your loop probably goes one past the end. More likely it should be:
    Code:
    		for (int i = 0;i < New->PathNumbers;i++, ActualPath++)
    > strcpy( &Path[i], &New->Paths[i] );
    And unless Path and New->Paths are arrays of char *, this statement doesn't make much sense. If you are just copying one char, at a time, you would use:
    Code:
    Path[i] = New->Paths[i];
    But then you would need to terminate the char array at the end with of the for-loop with:
    Code:
    Path[i] = '\0';

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I would guess that the code is intended to break up a list of paths (like "C:\windows;C:\windows\system") into Paths[]? Or maybe just count how many paths are in Path? Or am I completely off?
    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.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Yes, it is suposed to break a list of paths that is inside a file, the program is a self-updater that i am doing, it downloads the file with the version and the paths, so if the version is old, it download the paths, almost everything is working except that part.

    I think the main problem is here:
    Code:
    strcpy( &Path[i], &New->Paths[i] );

  8. #8
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    ok, i tryed this:
    Code:
    Path[i] = New->Paths[i];
    but it isnt still working, the debugger says that it is the problem

  9. #9
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    I think i got it, the problem is that "Path" and "New->Paths" were char*, i simply converted them to char*[30], it was causing a buffer overflow. Thank you for the help

  10. #10
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    It isn&#180;t still working as i want, when i have like "Dir1;Dir2;Dir3", even with the strcmp, it thinks that there is no ';', so it don&#180;t download only "Dir1", but download all the string.

    Code:
    typedef struct {
    	float Version;
    	int PathNumbers;
    	char *Paths[60];
    } sFile;
    
    char *Path[60];
    
    ...
    
    sFile *New;
    
    ActualPath = 0;
    	do{
    		for (int i = 0;i < New->CharNumbers;i++, ActualPath++)
    		{
    			if ( strcmp(";", (const char*)New->Paths) == 0 )
    			{
    				ActualPath++;
    				i++;
    				goto Download;
    			}
    			Path[i] = New->Paths[i];
    		}
    
    Download:
    		if ( pDownloader->DownloadFile( (const char*)Path, (const char*)Path ) != 0 )
                               return ERROR;
    	}while ( ActualPath < New->CharNumbers );
    Last edited by Scarvenger; 08-05-2006 at 08:28 AM.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Scarvenger
    It isnīt still working as i want, when i have like "Dir1;Dir2;Dir3", even with the strcmp, it thinks that there is no ';', so it donīt download only "Dir1", but download all the string.
    Well, that's closer to stating your actual intent. Posting a small but complete and compileable snippet of code that demonstrates the problem will only get you an answer more quickly.

    So... you're trying to parse a semicolon-delimited string?
    Parsing a String into Tokens Using strcspn 2

    Consider a break instead of the goto.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  5. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM

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