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

  • SEARCH
  • Home
  • 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 7846787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:42:22+00:00 2026-06-02T17:42:22+00:00

I’ve been using this tutorial to make C++ CGI script. However, it’s not compiling

  • 0

I’ve been using this tutorial to make C++ CGI script. However, it’s not compiling when I try to read the form POST data:

  char* lpszContentLength = getenv("CONTENT_LENGTH");
  char* lpszBuffer;
  int nContentLength = atoi(lpszContentLength);

  lpszBuffer = malloc(lpszContentLength+1);  // allocate a buffer
  memset(lpszBuffer, 0, lpszContentLength+1);  // zero it out

  fread(lpszBuffer,1,lpszContentLength,stdin);  // get data

Here’s the compiler’s complaint:

cgi.cpp: In function ‘int main()’:
cgi.cpp:12: error: invalid conversion from ‘char*’ to ‘size_t’
cgi.cpp:12: error:   initializing argument 1 of ‘void* malloc(size_t)’
cgi.cpp:12: error: invalid conversion from ‘void*’ to ‘char*’
cgi.cpp:13: error: ‘memset’ was not declared in this scope
cgi.cpp:15: error: invalid conversion from ‘char*’ to ‘size_t’
cgi.cpp:15: error:   initializing argument 3 of ‘size_t fread(void*, size_t, size_t, FILE*)’

Where ln 12 is the one starting with “lpszBuffer”.

I’m new to C++ so I’m unsure how to fix this, or why this might be happening. Maybe it’s just outdated code… I would happily accept some other solution to read the data from a POST request.

Edit:
I’ve updated the code according to your guys’ fixes.

  char* lpszContentLength = getenv("CONTENT_LENGTH");
  char* lpszBuffer;
  int nContentLength = atoi(lpszContentLength);

  lpszBuffer = (char*)malloc(nContentLength+1);  // allocate a buffer
  memset(lpszBuffer, 0, nContentLength+1);  // zero it out

  fread(lpszBuffer,1,nContentLength,stdin);  // get data

However, I still get a Segmentation fault from atoi:

==23419== Invalid read of size 1
==23419==    at 0x498DA8C: ____strtol_l_internal (strtol_l.c:298)
==23419==    by 0x498D7EF: strtol (strtol.c:110)
==23419==    by 0x498AB60: atoi (atoi.c:28)
==23419==    by 0x8048899: main (in .../cgi.cpp.cgi)
==23419==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

What’s the problem here? I’m assuming it has something to do with POST form submission if the POST is blank…

  • 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-02T17:42:25+00:00Added an answer on June 2, 2026 at 5:42 pm

    This is a type-cast issue with C++. lpszBuffer is a char*, however, malloc returns a void*. So you need to cast it to char*. Also note that you are trying to use a char* (lpszContentLength) as an integer value which is not true. This must be updated in your other functions as well – you have converted it earlier using the atoi function; so use that value.

    So the line should read

    lpszBuffer = (char*)malloc(nContentLength+1);
    

    Finally, to use memset, you must #include <string.h> in the beginning of the source file.

    Also, as good practice (especially if the script runs for any significant amount of time), do not forget to free your memory when you are done with it. That is, anything allocated with malloc should have free called on it when you are no longer going to use it. So if you use lpszBuffer through the entire function, at the end of the function simply do free(lpszBuffer);

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.