|
From: Liam W. <lw...@ca...> - 2005-05-16 19:04:54
|
I am writing a program to parse a certain webpage and I am checking to =
see if the first characters returned are 'HTTP' (which is what the =
standard specifies)
When I call my parsing function I pass it a pointer to a char and malloc =
memory inside the parsing function for the char. However when I return =
from the parsing program the char that I passed it doesn't point to the =
data parsed.
Basically I have a char*
char* dataToken
then I have a function
parseData(dateToken, data)
if I malloc data for dataToken before calling parseData the function =
works as expected
and I can use dataToken after the call to parseData. However if I pass =
in a simple char*
and malloc memory for dataToken inside parseData when parseData returns =
dataToken
contains seemingly random data.
Is malloced memory, created in a function call, released after the =
function has executed; in the same was as the function's local variables =
are released once the function is executed?
Here is code form my project.
int checkHTTPData(char* HTTPData)
{
int err =3D 0;
if (strlen(HTTPData) < 12)
{
printf("HTTP Data too small");
return AP_HTTP_DATA_TOO_SMALL;
}
=20
char* dataToken;
int tokenSize;
tokenSize =3D TOKEN_STRING_SIZE;
/* if I include this next block of commented out code and reserve memory =
for dataToken
before I pass it as an argument to getDataToken everything works =
fine.
dataToken =3D (char*) malloc(tokenSize);
if (dataToken =3D=3D '\0')
{
return AP_MALLOC_ERROR;
}
memset(dataToken, 0, tokenSize);
*/
/* here is the function that does the parsing dataToken is the char* */
err =3D getDataToken(HTTPData, dataToken, 0);
/* after this function is called dataToken seems to point at random =
memory*/
printf("dataToken:%s", dataToken);
=20
if (strncmp(dataToken, "HTTP", 4) =3D=3D 0)
{
printf("It is HTTP data");
}
return err;
}
/* Here is the getDataTokenFunction */
int getDataToken(char* data, char* dataToken, int tokenNbr)
{
char* tempToken;
char currentChar;
int tokernPos;
int whiteSpaceNbr=3D 0;
int tokenPlace =3D 0;
int skipWhiteSpaceReturn;
int startOfChar;
int stringPos;
int tokenSize;
int i =3D 0;
int j =3D 0;
/*this function skips over any white space at the beginning of the =
token*/
skipWhiteSpaceReturn =3D skipWhiteSpace(data, i, &startOfChar);=20
/* this skips over continuous groups of characters separated by =
whitespace until the desired token is reached */
for (i=3DstartOfChar; i < strlen(data); i++)
{
if ((data[i] =3D=3D ' ') || (data[i] =3D=3D '\t') || (data[i] =
=3D=3D '\n'))
{
if (whiteSpaceNbr =3D=3D tokenNbr)
{
i =3D strlen(data);
}
else
{ =20
whiteSpaceNbr++;
=20
// set the string position to the start of the new =
token
skipWhiteSpaceReturn =3D skipWhiteSpace(data, i, =
&startOfChar);
=20
//startOfChar points to the new string after the =
white space has been skipped
i =3D startOfChar;
}
}
}
// set string position to the start of the first non-whitespace =
character
stringPos =3D startOfChar;
/* if I pass in dataToken already malloced from checkHTTPData I don't =
include this next bit
However if I pass dataToken in as a simple char* without mallocing =
in checkHTTPData then I include
this part
tokenSize =3D TOKEN_STRING_SIZE;
dataToken =3D (char*) malloc(tokenSize);
if (token =3D=3D '\0')
{
return AP_MALLOC_ERROR;
}
memset(token, 0, tokenSize);
*/
=20
while ((data[stringPos] !=3D ' ') && (data[stringPos] !=3D '\0'))
{
if (tokenPlace >=3D tokenSize)
{
tokenSize =3D TOKEN_STRING_SIZE * ((tokenSize / =
TOKEN_STRING_SIZE) + 1);
tempToken =3D (char*) realloc(dataToken, tokenSize);
=20
if (tempToken =3D=3D '\0')
{
free(token);
return AP_REALLOC_ERROR;
}
=20
datatoken =3D tempToken;
}
=20
=20
/* here is where the string gets copied when I run through this =
function in gdb the correct data
gets assigned to dataToken however once the function returns the =
data is no longer there
and dataToken points to random data (or it seems random anyway)
if however I malloced dataToken before passing it to this =
function everything works as expected.
*/
=20
dataToken[tokenPlace] =3D data[stringPos];
dataTokenPlace++;
stringPos++;
} =20
=20
dataToken[tokenPlace] =3D '\0';
return 0;
}
|