Re: Returning Char array/pointer? Continuing of thread I am confused
with these concepts.
rockdale wrote:
Hi,
First thanks for all you guys that help understand more about c++ lang
in the thread "I am confused with these concepts". As trying to
improve my understanding, I did the following test, which cause the
"Access violation reading location". (this is also one of the
confusing thing left from that thread).
I tried to read a binary file and store it in my char array.
this is my function:
-----------------------------------
long ReadFileToByteArray(char const* aSrcFile, char* aDestArr){
long lngFileSize = 0;
int intBeenRead = 0;
ifstream in;
in.open(aSrcFile, ios::in| ios::binary | ios::ate);
if(in.is_open()||in.bad())
{
lngFileSize = in.tellg();
}else{
throw exception("could not open input file"); // could not open in
file
return 0;
}
/*************this line is different from
function2*********************/
aDestArr = (char*)malloc(lngFileSize+1);
in.seekg(0,ios::beg);
in.read(aDestArr, lngFileSize);
aDestArr[lngFileSize] = '\0';
in.close();
return lngFileSize;
}
---------------------------
This is how I call this function:
char* myByte;
long lngSize = 0;
lngSize = ReadFileToByteArray("myFileName",myByte);
and when I try to access myByte, I got the Access Violation error.
----------------------------------------
I know this code is wrong before I run it, because I did not
initialize myByte (malloc or new to give it a size), but in this case,
I do not know the file size until I went into the function, Also, I
did not free the aDestArr memory in my function.
Here is my another function,
----------------------------------------
char* ReadFileToByteArray(char const* aSrcFile, long* aSize ){
long lngFileSize = 0;
int intBeenRead = 0;
ifstream in;
in.open(aSrcFile, ios::in| ios::binary | ios::ate);
if(in.is_open()||in.bad())
{
lngFileSize = in.tellg();
}else{
throw exception("could not open input file"); // could not open in
file
return 0;
}
/*************this line is different from
function1*********************/
char* aDestArr = new char[lngFileSize];
in.seekg(0,ios::beg);
in.read(aDestArr, lngFileSize);
aDestArr[lngFileSize] = '\0';
in.close();
return aDestArr;
}
Caller of this function
char* myByte2;
long lngSize2 = 0;
myByte2 = ReadFileToByteArray2("myFileName", &lngSize2);
-----------------------------------
the function2 works fine. as always, the caller needs to delete the
returned aDestArr to free memory.
But to me, the function2 looks so wierd, is this the way that you
would implement the function?
I really do not like the idea that return the pointer and let the
caller to delete the array.
to me function1 is more making sense, passing the array pointer as
param and returns a long data type, what should I change to let it
work? or both implementation are bad and there are some better way to
return an size unknown char array.
Rockdale:
The difference does not have to do with malloc versus new (though you should
always use new in a C++ program). Rather, to make your first function work you
must pass the pointer by reference:
long ReadFileToByteArray(char const* aSrcFile, char*& aDestArr);
But you would be much better using std::string than doing your own memory
allocation. Modern C++ is in many ways simpler to use the C, despite the fact
that it is a much richer language.
--
David Wilkinson
Visual C++ MVP