Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:31:04+00:00 2026-06-18T08:31:04+00:00

I’m very very confused in dealing with buffers ( copying, concatenating, etc) especially the

  • 0

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..:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:31:05+00:00Added an answer on June 18, 2026 at 8:31 am

    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:

    std::vector<unsigned char> Block;
    

    It’s size (which can be read by Block.size()) is now 0. To allocate memory for it, we can write:

    Block.reserve(size);
    

    You can also do it in constructor directly:

    std::vector<unsigned char> Block (size, 0); // fill it with zeros
    

    Now, if you want to concatenate it with another block, it’s as simple as:

    std::vector<unsigned char> Block2;
    
    Block.insert( Block.end(), Block2.begin(), Block2.end() );
    

    or

    std::copy(Block2.begin(), Block2.end(), std::back_inserter(Block));
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am confused How to use looping for Json response Array in another Array.
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.