I’m very very confused in dealing with buffers ( copying, concatenating, etc) especially the usage of ‘new’ or malloc. i can code correctly without compilation errors, but problem occurs during runtime. i get runtime errors such as Access Violation, Assertion failed, etc..
For example, I’ve given source code of winhttp api demonstration from MSDN. In that code, they Query data from server using a while loop until all data received. after each query data is printed on console.
But I want it to store ( append ) data received each time into a new variable and finally print entire data on console. It would also be helpful in case of dlls
More details are provided in the code itself.
#include <Windows.h>
#include <iostream>
#include <winhttp.h>
#pragma comment(lib,"winhttp")
using namespace std;
int strcat_b(LPTSTR &dest, LPTSTR &src, int size);
int main()
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
LPTSTR pszFullCode;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
hSession = WinHttpOpen( L"Test WinHttp", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
hConnect = WinHttpConnect( hSession, L"google.com", INTERNET_DEFAULT_HTTP_PORT, 0);
hRequest = WinHttpOpenRequest( hConnect, L"GET", 0, 0, 0, 0 ,0);
WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
WinHttpReceiveResponse( hRequest, NULL);
do
{
dwSize = 0;
WinHttpQueryDataAvailable( hRequest, &dwSize);
pszOutBuffer = new char[dwSize+1];
ZeroMemory(pszOutBuffer, dwSize+1);
WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded);
// Actual code here was :
printf("%s", pszOutBuffer);
// I want to append pszOutBuffer into pszFullCode..
// So I tried something like strcat(pszFullCode, pszOutBuffer)
// And finally, when while loop ends, pszFullCode will be printed or Copied into another variable.
// But I get "Access Violation error" at runtime.
delete [] pszOutBuffer;
} while (dwSize > 0);
// Here it should be able print final result: printf("%s", pszFullCode);
// In case of Dll, It should be able to copy data from pszFullCode to required Pointer.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
cin.get();
return 0;
}
Can anyone give some links to learn about concepts like this?? ( dealing with buffers, ‘new’ operator. malloc, minimizing access violation errors, etc )
Thanks a lot..:)
I think that pretty much answers the question asked:
If you want to deal with “buffers, new operator, malloc, minimizing access violation errors”, you should learn to use Standard C++ Library. It’s full of useful stuff that will take the burden of managing memory of you.
For example, consider contiguos memory block. It can be expressed as:
It’s size (which can be read by
Block.size()) is now 0. To allocate memory for it, we can write:You can also do it in constructor directly:
Now, if you want to concatenate it with another block, it’s as simple as:
or
Everything will reallocate properly and you won’t get any runtime errors. If you however will ever be in need of supplying data to badly designed APIs or APIs written in C, you can still get raw acess to
vectors memory by.data()method (C++11 only; use familiar&Block[0]in older compilers).