Click to See Complete Forum and Search --> : realloc


bryce burrows
September 13th, 1999, 10:12 AM
cananyone tell me why this falls over on the following line?


strncpy(&file[loops * sz],block,dwBytesRead);



char * CPubMedChecker::alternateRead(CString url)
{
HINTERNET m_Session;
CString Url = url;
HINTERNET hHttpFile;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
DWORD dwFileSize;
DWORD dwBytesRead;
BOOL bSuccessful;
CString Contents;

char * file=NULL;
CString m_ErrorMessage;
// Setting default error message
Contents = m_ErrorMessage;


m_Session = ::InternetOpen(NULL,
INTERNET_OPEN_TYPE_PRECONFIG, // Use registry settings.
NULL, // Proxy name. NULL indicates use default.
NULL, // List of local servers. NULL indicates default.
0) ;
// Opening the Url and getting a Handle for HTTP file
hHttpFile = InternetOpenUrl(m_Session, (const char *) Url, NULL, 0, 0, 0);

if (hHttpFile)
{
// Getting the size of HTTP Files
//BOOL bQuery = ::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL) ;
//DWORD fred = GetLastError();
//if(bQuery==TRUE)
{
// Allocating the memory space for HTTP file contents
// dwFileSize=atol(szSizeBuffer);

//LPSTR szContents = Contents.GetBuffer(dwFileSize);
//file = (char *)malloc(dwFileSize);
// Read the HTTP file
char block[1024];
BOOL bRead=false;
file =NULL;
const sz = 1024;
int loops=0;
WORD amount=0;
do {
bRead = ::InternetReadFile(hHttpFile, block /*szContents*/, sz, &dwBytesRead);
amount += dwBytesRead;
file = (char *)realloc((char*)file,amount);
strncpy(&file[loops * sz],block,dwBytesRead);
loops ++;
TRACE("%d\n",amount);
//file = (char *)realloc(file,_msize(file) + dwBytesRead);
}while ((bRead ==true)&& (dwBytesRead == sz));

// if (bRead)
// bSuccessful = TRUE;

::InternetCloseHandle(hHttpFile); // Close the connection.
}
}
return file;
}

Bryce Burrows
September 13th, 1999, 10:12 AM
cananyone tell me why this falls over on the following line?


strncpy(&file[loops * sz],block,dwBytesRead);



char * CPubMedChecker::alternateRead(CString url)
{
HINTERNET m_Session;
CString Url = url;
HINTERNET hHttpFile;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = sizeof(szSizeBuffer);
DWORD dwFileSize;
DWORD dwBytesRead;
BOOL bSuccessful;
CString Contents;

char * file=NULL;
CString m_ErrorMessage;
// Setting default error message
Contents = m_ErrorMessage;


m_Session = ::InternetOpen(NULL,
INTERNET_OPEN_TYPE_PRECONFIG, // Use registry settings.
NULL, // Proxy name. NULL indicates use default.
NULL, // List of local servers. NULL indicates default.
0) ;
// Opening the Url and getting a Handle for HTTP file
hHttpFile = InternetOpenUrl(m_Session, (const char *) Url, NULL, 0, 0, 0);

if (hHttpFile)
{
// Getting the size of HTTP Files
//BOOL bQuery = ::HttpQueryInfo(hHttpFile,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL) ;
//DWORD fred = GetLastError();
//if(bQuery==TRUE)
{
// Allocating the memory space for HTTP file contents
// dwFileSize=atol(szSizeBuffer);

//LPSTR szContents = Contents.GetBuffer(dwFileSize);
//file = (char *)malloc(dwFileSize);
// Read the HTTP file
char block[1024];
BOOL bRead=false;
file =NULL;
const sz = 1024;
int loops=0;
WORD amount=0;
do {
bRead = ::InternetReadFile(hHttpFile, block /*szContents*/, sz, &dwBytesRead);
amount += dwBytesRead;
file = (char *)realloc((char*)file,amount);
strncpy(&file[loops * sz],block,dwBytesRead);
loops ++;
TRACE("%d\n",amount);
//file = (char *)realloc(file,_msize(file) + dwBytesRead);
}while ((bRead ==true)&& (dwBytesRead == sz));

// if (bRead)
// bSuccessful = TRUE;

::InternetCloseHandle(hHttpFile); // Close the connection.
}
}
return file;
}

bryce burrows
September 13th, 1999, 10:15 AM
well it falls over because realloc returns a null pointer but i dont know why!

Bryce Burrows
September 13th, 1999, 10:15 AM
well it falls over because realloc returns a null pointer but i dont know why!

bryce burrows
September 13th, 1999, 11:09 AM
WORD amount=0;

should be
DWORD amount=0;

duuuuh and he answers his own question :)

Bryce

Bryce Burrows
September 13th, 1999, 11:09 AM
WORD amount=0;

should be
DWORD amount=0;

duuuuh and he answers his own question :)

Bryce