CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: realloc

  1. #1
    Join Date
    Apr 1999
    Location
    UK, London
    Posts
    155

    realloc

    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;
    }


  2. #2
    Join Date
    Apr 1999
    Location
    UK, London
    Posts
    155

    realloc

    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;
    }


  3. #3
    Join Date
    Apr 1999
    Location
    UK, London
    Posts
    155

    Re: realloc

    well it falls over because realloc returns a null pointer but i dont know why!


  4. #4
    Join Date
    Apr 1999
    Location
    UK, London
    Posts
    155

    Re: realloc

    well it falls over because realloc returns a null pointer but i dont know why!


  5. #5
    Join Date
    Apr 1999
    Location
    UK, London
    Posts
    155

    Re: realloc

    WORD amount=0;

    should be
    DWORD amount=0;

    duuuuh and he answers his own question

    Bryce


  6. #6
    Join Date
    Apr 1999
    Location
    UK, London
    Posts
    155

    Re: realloc

    WORD amount=0;

    should be
    DWORD amount=0;

    duuuuh and he answers his own question

    Bryce


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured