I am new to C / C ++, so I assume I will make a rookie mistake:
int main(){ char* clen; clen = getenv("CONTENT_LENGTH"); if (clen==NULL){ cout << "No such ENV var: CONTENT_LENGTH"<<endl; exit(0); } int cl = 0; cl = atoi(clen); if (cl < 1){ return inputPage(); } // if there is no content, we assume that this is a fresh request, // so we showed the input page, otherwise, we'll return dispatch to //the processing code. postTest(clen); }
This is supposed to be a CGI script. As far as I can tell with GDB, printed statements, etc., This segfaults code on the line "cl = atoi (clen)"; I have no idea why this is. K & R suggests that this is correct. I basically copied this line from half a dozen other online lessons. And it looks like he worked last night! I am completely at a dead end.
I do not believe that it really crashes on atoi ()
Could you try this code?
#include <iostream> #include <stdlib.h> #ifndef NULL #define NULL 0 #endif using namespace std; int main(){ char* clen; clen = getenv("CONTENT_LENGTH"); if (clen==NULL){ cout << "No such ENV var: CONTENT_LENGTH"<<endl; exit(0); } int cl = 0; cl = atoi(clen); if (cl < 1){ std::cout << "return inputPage();" << std::endl; return 0; } std::cout << "postTest();" << std::endl; }
, . "" CONTENT_LENGTH, .
./app CONTENT_LENGTH=4 ./app CONTENT_LENGTH=-4 ./app CONTENT_LENGTH=a ./app
getenv() , , segfault cl = atoi(clen). , getenv() NULL, ( C).
cl = atoi(clen)
NULL , ( "script" ) - .
: , ? HTTP 500? , cout << "Content-type: text/html\n\n". -?
cout << "Content-type: text/html\n\n"
EDIT2: , C, atoi(), ; ++ lexical_cast ( TR1 boost).
atoi()
lexical_cast
, GDB. () atoi(), , ? , , atoi()?
, atoi ++ . , CGI, CONTENT_LENGTH GET, . , , . inputPage, , CONTENT_LENGTH .
Source: https://habr.com/ru/post/1726186/More articles:I am trying to create an expert system and my user interface is prepared in .Net, and now I am ready to connect it to LISP - .netPHP: RegEx vs. ctype_ * - phpProduction and development environment synchronization - gitUTF-8 & IsAlpha () in PHP - user-interfaceSQL Server Query Optimization: where (Col = @Col or @Col = Null) - sql-serverOpenGL ES view: how to orient it to the landscape? - iosPyQt 4 UI freezes - pythonLambda expression Where is the navigation property - lambdaкак бы вы определили, находится ли курсор мыши внутри JFrame в java? - javaDetecting mousemove with jquery - javascriptAll Articles