Please support our C++ advertiser: Programming Forums
Views: 2512 | Replies: 11
![]() |
•
•
•
•
| |
Here's what I'm trying to do:
create an empty/NULL char * in one place, pass it to a function who will populate it (and tell me the size), and then use the char * elsewhere. However, when I get out of the function..
Here's the function that expects the char * and attempts to populate it:
Here's the code (assume in main()) trying to use it.
I am finding that the char * is populated inside the function (loadFullFileBinary) but when I attempt to look at it after the function has returned, it is null... What am I doing wrong? (You can ignore the DHException and multiLog bit, they are not related to the problem...)
create an empty/NULL char * in one place, pass it to a function who will populate it (and tell me the size), and then use the char * elsewhere. However, when I get out of the function..
Here's the function that expects the char * and attempts to populate it:
bool FileUtilities::loadFullFileBinary( const string &filename, char * fileContents, long & sizeOfContents )
{
FILE *file = fopen(filename.c_str(), "rb");
if ( !file )
returnfalse;
fseek(file, 0, SEEK_END);
sizeOfContents = ftell(file);
cout<<"SIZE:"<<sizeOfContents<<endl;
rewind(file);
fileContents = newchar[sizeOfContents];
if ( fileContents )
{
if ( !fread(fileContents, sizeOfContents, 1, file) == 1 )
{
//puts(buffer);
delete [] fileContents;
sizeOfContents=0;
returnfalse;
}
}
fclose(file);
returntrue;
}
Here's the code (assume in main()) trying to use it.
char * me=NULL; long size; bool status = FileUtilities::loadFullFileBinary("test.txt", me, size ); if ( !status ) throw DHException(*this, "Couldn't read file" ); else { multiLog.writeLog( "SIZE WAS '%ld'", size ); if ( me != NULL ) { cout<<"TEST:"<<me[0]<<endl; cout<<"TEST:"<<me[1]<<endl; } else cout<<"WAS NULL"<<endl; }
I am finding that the char * is populated inside the function (loadFullFileBinary) but when I attempt to look at it after the function has returned, it is null... What am I doing wrong? (You can ignore the DHException and multiLog bit, they are not related to the problem...)
OK, I've tried to simplify the example. Same problem happens here:
Result:
./Test
tester is null
#include <iostream.h>
void test( char * fillMe )
{
fillMe = new char[10];
memset( fillMe, '\0', 10 );
strcpy( fillMe, "TESTING" );
}
int main()
{
char * tester=NULL;
test( tester );
if ( tester == NULL )
{
cout<< "tester is null"<<endl;
}
else
cout<<tester[0]<<endl;
}Result:
./Test
tester is null
I was able to get it to work, however is this the right way?
Result:
./Test
TESTING
#include <iostream.h>
void test( char ** fillMe)
{
*fillMe = new char[10];
memset( *fillMe, '\0', 10 );
strcpy( *fillMe, "TESTING" );
}
int main()
{
char * tester=NULL;
test( &tester );
if ( tester == NULL )
{
cout<< "tester is null"<<endl;
}
else
cout<<tester<<endl;
if ( tester !=NULL )
delete [] tester;
}./Test
TESTING
fillMe is a copy of the pointer, so you're allocating memory to a copy, then the copy is destroyed, thus destroying your only reference to the memory. This is affectionately called a memory leak. The solution in C is to pass a pointer to the pointer so that you can get to the original object:
Notice that I included <cstring>, which is required for memset and strcpy, and I also removed memset because there's no point in using it since you'll just use strcpy right after.
The C++ version using references is even easier:
If I might direct you here, you'll probably have an easier time with pointers.
#include <cstring>
#include <iostream>
using namespace std;
void test( char ** fillMe )
{
*fillMe = new char[10];
strcpy( *fillMe, "TESTING" );
}
int main()
{
char * tester = NULL;
test( &tester );
if ( tester == NULL )
{
cout<< "tester is null"<<endl;
}
else
cout<< tester <<endl;
}The C++ version using references is even easier:
#include <cstring>
#include <iostream>
using namespace std;
void test( char *& fillMe )
{
fillMe = new char[10];
strcpy( fillMe, "TESTING" );
}
int main()
{
char * tester = NULL;
test( tester );
if ( tester == NULL )
{
cout<< "tester is null"<<endl;
}
else
cout<< tester <<endl;
} I'm here to prove you wrong.
>must be somehow automatically included based on my compiler setup.
That's nice. Now forget about it. Your code is not portable if you rely on such things.
>doesn't that take care of any memory leaks?
Yes, but only because you correctly assigned the memory to tester by using a pointer to a pointer. If you had not made that change, no amount of deleting would stop the memory leak. Once again, I urge you to read the tutorial so that you know what the problem was and why the fix fixed it.
That's nice. Now forget about it. Your code is not portable if you rely on such things.
>doesn't that take care of any memory leaks?
Yes, but only because you correctly assigned the memory to tester by using a pointer to a pointer. If you had not made that change, no amount of deleting would stop the memory leak. Once again, I urge you to read the tutorial so that you know what the problem was and why the fix fixed it.
I'm here to prove you wrong.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Hybrid Mode