Hi guys. I am a beginner in c++ programming learning all by myself. FYI i have started a 2 yrs ago programming in delphi. Mostly using API and my requirements being writing smaller apps and more optimised ones i decided to start writing code in C++. I am not a complete noob in programming but C++ is totally different from what i have been doing yet.
The master difference in using c++ and delphi have been for me that "strings" are managed in a totally different way. Memory management is ... obviously totally different too. I am a google fan so i did a bit of researching before posting here. but still i am quite confused.
I am going to give a brief example of what i am struggling to do. In delphi we normally write code this way.
Code:
Function SomeRandomThing(a,b :string):String;
var x:string;
begin
x:='Garbage' + a + ' Some other garbage';
result := x + b;
end;
My main issue now is that c++ doesnt have (or i am not aware of) automatic memory management. The following code segment is part of a little application i am trying to code. All this does is try to return a listing of files in a current directory. However there is a huge problem. or many look below.
Code:
char* DirList(char *path)
{
char *FileList = new char[10000];
char *temppath = new char[strlen(path) + 4];
char *tmpstr;
WIN32_FIND_DATA DataStruct;
HANDLE FindHandle;
strcpy(FileList,path);
strcat(FileList,"\r");
strcpy(temppath,path);
strcat(temppath,"*.*");
FindHandle = FindFirstFile(temppath, &DataStruct);
if (FindHandle == INVALID_HANDLE_VALUE)
{
return "!Invalid Directory";
}
do
{
tmpstr = DataStruct.cFileName;
if ((strcmp(DataStruct.cFileName,".") != 0 ) && (strcmp(DataStruct.cFileName,"..") != 0 ))
{
if ((DataStruct.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
strcat(FileList,"\\");
}
strcat(FileList,DataStruct.cFileName);
strcat(FileList,";");
strcat(FileList,IntToStr(DataStruct.nFileSizeLow));
strcat(FileList,"\r");
}
}while((FindNextFile(FindHandle,&DataStruct)) != false);
FindClose(FindHandle);
char * result = new char[strlen(FileList) + 13];
strcpy(result,CMD_DIRECTORY_INFO);
strcat(result,IntToStr(strlen(FileList)));
strcat(result,"#");
strcat(result,FileList);
free(temppath);
free(FileList);
return result;
}
Code:
char *FileList = new char[10000];
i am creating a new variable with size 10000, this is incorrect since. my file list will sometime either exceed this and most of the time be much below that. i should have a calculated array size b but it seems malloc and realloc crash on me
Code:
strcpy(result,CMD_DIRECTORY_INFO);
strcat(result,IntToStr(strlen(FileList)));
strcat(result,"#");
strcat(result,FileList);
is relatively tedious if i have to concat many strings... is there a shorter way?
After running this code (yes it actually works) i get BIG memory leaks. but... i cleared my variables.
this gives me the same result...
what am i doing wrong?
I am thinking about using a replacement class for this. but again size is an issue. std::string wont work for me since my application needs to be tiny. my objective is 10kb max.
thanks in advance for your help.