how can i 'delete []array' if...

Started by
5 comments, last by ELS1 22 years, 1 month ago
ok, ive been thinking of this lately... lets say i have this function:
  

char* AddConsoleText(HWND ConsoleScreen, char* Text)
{
	char* LineFeed = "\r\n" ;

	int LineFeedLength = (int)strlen(LineFeed) + 1 ;
	int TextLength = (int)strlen(Text) + 1 ;
	int CombinedLength = LineFeedLength + TextLength ;

	char* NewTextString = new char[CombinedLength] ;

	strcpy(NewTextString, Text) ;
	strcat(NewTextString, LineFeed) ;
        
        return NewTextString ;

        delete []NewTextString ;  // how can i free the memory BEFORE i send it??

  
This is just an example...but how would i do this??
Advertisement
Since you''re using ''new'' and ''deletel'', you''re using C++, so I recommend using the object ''string'' (std::string) because it rules.

Otherwise, let the user of the function delete the array, which is not a good thing.
Or, modify your function so that is receives as parameter a preallocated array.

Go for ''string'', you won''t have any memory management issue for your strings.

Also, your variable ''LineFeed'' should be const. I try to put ''const'' whereever I can.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com
but what if im not using a string?

lets say i have to dynamically allocate an array? how would i do that?
quote:Original post by ELS1
but what if im not using a string?

lets say i have to dynamically allocate an array? how would i do that?


As he said, you have to delete it after you''re done with it. In this case, that is somewhere _outside_ of this function.

you want:

char *var;

var = AddConsoleText(HWND, "Nyah";

//do some more stuff

delete [] var;
You cannot expect a function to allocate memory, return that memory AND delete it.

It can either return it (and let the user of the function delete it), or delete it (and not return it).

Curiosity question : why can't you use string?

You could use an auto_ptr or reference counted dynamic array. Pretty easy to do, and so powerful with templates.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

[edited by - Blaster on March 18, 2002 1:10:37 PM]
quote:Original post by ELS1
but what if im not using a string?

lets say i have to dynamically allocate an array? how would i do that?


You can't do what you are trying to do. You have two basic options...

1. Use a string or similar class, and let the destructor and copy constructor take care of memory management.

2. Use a static buffer: e.g


    char* AddConsoleText(HWND ConsoleScreen, char* Text){        static char NewTextString[MAX_CONSOLE_STRING_LENGTH];	char* LineFeed = "\r\n" ;	int LineFeedLength = (int)strlen(LineFeed) + 1 ;	int TextLength = (int)strlen(Text) + 1 ;	int CombinedLength = LineFeedLength + TextLength ;	strcpy(NewTextString, Text) ;	strcat(NewTextString, LineFeed) ;         return NewTextString ;}    


3. Modify the original string directly (you must be sure that there is space in the string)

  char* AddConsoleText(HWND ConsoleScreen, char* Text){  char* ptr = Text + strlen(Text);  sprintf(ptr,"\r\n");   return Text;}  



[edited by - Sandman on March 18, 2002 1:11:15 PM]
Or change the syntax of your function to take an allocated buffer as a parameter.

[Edit: ie, Sandman's suggestion 3]

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

[edited by - Oluseyi on March 18, 2002 1:14:59 PM]

This topic is closed to new replies.

Advertisement