D
David Pratt
I recently replied to MC felons post on returning strings and was in error
on what I was trying to do. After running completely thru my code, I did
find that I was misunderstanding how C++ works with char arrays. After much
testing, I would like to submit the following code for comments:
#include <Stdlib.h>
char *Func1()
/*-------------------------------------------------------------------
allocates memory and fills it with the string, "Hello World!" and
passes ownership to the calling function
---------------------------------------------------------------------*/
{
char *tmp = (char*)malloc(20); // allocate space for the array
char T2[20] = "Hello World!";
int i = 0;
// fill the char array
do
{
tmp = T2;
i++;
}while(T2 != 0);
tmp = 0;
return tmp;
};
//---------------------------------------------------------------
void Func2(char *tmp)
/*--------------------------------------------------------------
Takes a string argument by reference and changes it to
"Not Hello World"
---------------------------------------------------------------*/
{
char* T2 = "Not Hello World\0";
int i=0;
do
{
tmp = T2;
i++;
}while(T2!=0);
tmp=0;
};
//---------------------------------------------------------------
int main()
{
char *Tmp = Func1(); // allocates memory for array and fills it with "Hello
World"
Func2(Tmp); // change string to "Not Hello World!"
free(Tmp); // free memory allocated in Func1 and assigned
to Tmp;
};
In all my test runs, I get the desired results without any errors. While I
acknowledge that using the string functions would be preferable, I still
like to code my own solutions sometimes just for the learning experience.
on what I was trying to do. After running completely thru my code, I did
find that I was misunderstanding how C++ works with char arrays. After much
testing, I would like to submit the following code for comments:
#include <Stdlib.h>
char *Func1()
/*-------------------------------------------------------------------
allocates memory and fills it with the string, "Hello World!" and
passes ownership to the calling function
---------------------------------------------------------------------*/
{
char *tmp = (char*)malloc(20); // allocate space for the array
char T2[20] = "Hello World!";
int i = 0;
// fill the char array
do
{
tmp = T2;
i++;
}while(T2 != 0);
tmp = 0;
return tmp;
};
//---------------------------------------------------------------
void Func2(char *tmp)
/*--------------------------------------------------------------
Takes a string argument by reference and changes it to
"Not Hello World"
---------------------------------------------------------------*/
{
char* T2 = "Not Hello World\0";
int i=0;
do
{
tmp = T2;
i++;
}while(T2!=0);
tmp=0;
};
//---------------------------------------------------------------
int main()
{
char *Tmp = Func1(); // allocates memory for array and fills it with "Hello
World"
Func2(Tmp); // change string to "Not Hello World!"
free(Tmp); // free memory allocated in Func1 and assigned
to Tmp;
};
In all my test runs, I get the desired results without any errors. While I
acknowledge that using the string functions would be preferable, I still
like to code my own solutions sometimes just for the learning experience.