ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Okay, I've struggled with this long enough to risk embarrassing myself.
Help will be appreciated.
Here's my newbie code:
Code:
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <cstdio>
#include <stdio.h>
#include <malloc.h>
using namespace std;
int main()
{
char* billschedContentLength = getenv("CONTENT_LENGTH");
char* billschedBuffer;
int nContentLength = atoi(billschedContentLength);
billschedBuffer = malloc(billschedContentLength+1); // allocate a buffer
memset(billschedBuffer, 0, billschedContentLength+1); // zero it out
fread(billschedBuffer,1,billschedContentLength,stdin); // get data
cout << "Content-type: text/html" << endl << endl
<< "<html>" << endl
<< "<body>" << endl
<< "<p>" << endl
<<billschedBuffer<<""<<endl
<< endl
<< "" << endl
<< "" << endl
<< "" ;
free(billschedBuffer);
return 0;
}
And the errors:
barth@bluegospel:~$ g++ ~/cppexperiments/billscheduler.cpp
/home/barth/cppexperiments/billscheduler.cpp: In function 'int main()':
/home/barth/cppexperiments/billscheduler.cpp:16: error: invalid conversion from 'char*' to 'size_t'
/home/barth/cppexperiments/billscheduler.cpp:16: error: initializing argument 1 of 'void* malloc(size_t)'
/home/barth/cppexperiments/billscheduler.cpp:16: error: invalid conversion from 'void*' to 'char*'
/home/barth/cppexperiments/billscheduler.cpp:17: error: 'memset' was not declared in this scope
/home/barth/cppexperiments/billscheduler.cpp:19: error: invalid conversion from 'char*' to 'size_t'
/home/barth/cppexperiments/billscheduler.cpp:19: error: initializing argument 3 of 'size_t fread(void*, size_t, size_t, FILE*)'
barth@bluegospel:~$
Okay, I've struggled with this long enough to risk embarrassing myself.
Help will be appreciated.
Here's my newbie code:
Code:
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <cstdio>
#include <stdio.h>
#include <malloc.h>
using namespace std;
int main()
{
char* billschedContentLength = getenv("CONTENT_LENGTH");
char* billschedBuffer;
int nContentLength = atoi(billschedContentLength);
billschedBuffer = malloc(billschedContentLength+1); // allocate a buffer
memset(billschedBuffer, 0, billschedContentLength+1); // zero it out
fread(billschedBuffer,1,billschedContentLength,stdin); // get data
cout << "Content-type: text/html" << endl << endl
<< "<html>" << endl
<< "<body>" << endl
<< "<p>" << endl
<<billschedBuffer<<""<<endl
<< endl
<< "" << endl
<< "" << endl
<< "" ;
free(billschedBuffer);
return 0;
}
And the errors:
barth@bluegospel:~$ g++ ~/cppexperiments/billscheduler.cpp
/home/barth/cppexperiments/billscheduler.cpp: In function 'int main()':
/home/barth/cppexperiments/billscheduler.cpp:16: error: invalid conversion from 'char*' to 'size_t'
/home/barth/cppexperiments/billscheduler.cpp:16: error: initializing argument 1 of 'void* malloc(size_t)'
/home/barth/cppexperiments/billscheduler.cpp:16: error: invalid conversion from 'void*' to 'char*'
/home/barth/cppexperiments/billscheduler.cpp:17: error: 'memset' was not declared in this scope
/home/barth/cppexperiments/billscheduler.cpp:19: error: invalid conversion from 'char*' to 'size_t'
/home/barth/cppexperiments/billscheduler.cpp:19: error: initializing argument 3 of 'size_t fread(void*, size_t, size_t, FILE*)'
barth@bluegospel:~$
Copy-paste output of 'cat -n ~/cppexperiments/billscheduler.cpp' - it makes line numbers obvious.
int main()
{
char* billschedContentLength = getenv("CONTENT_LENGTH");
char* billschedBuffer;
int nContentLength = atoi(billschedContentLength);
billschedBuffer = malloc(billschedContentLength+1); // allocate a buffer
memset(billschedBuffer, 0, billschedContentLength+1); // zero it out
}
And the errors:
barth@bluegospel:~$ g++ ~/cppexperiments/billscheduler.cpp
/home/barth/cppexperiments/billscheduler.cpp: In function 'int main()':
/home/barth/cppexperiments/billscheduler.cpp:16: error: invalid conversion from 'char*' to 'size_t'
I am just caring about the first error. getenv(3) returns a pointer to a character. And you just add 1 to the address of the first character, but you want to most possibly to this:
Code:
billschedBuffer = malloc(nContentLength+1);
You're quite lucky that you used C++. In C, you'll hopefully get a warning :-)
Andi
Last edited by ForzaItalia2006; 07-03-2010 at 04:11 PM.
Sergei, I'm trying to profit from your honest attempt to simplify this without spoon feeding, but I'm just not seeing the answer. A little more help, if I'm not asking too much?
ForzaItalia, I wondered about that too, but the example I'm using as a template is as such:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
void main()
{
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
cout << "Content-type: text/html" << endl << endl
<< "<html>" << endl
<< "<body>" << endl
<< "<p>" << endl
<< "Hello! You sent " << lpszContentLength << " bytes of data which read: <br>" << endl
<< lpszBuffer << endl
<< "
” << endl
<< “” << endl
<< ““;
free(lpszBuffer);
}
Sergei, were you hinting that I should include <string.h> in my pre-processing directives? I did that and some of my error messages have changed. I also added a few others.
Here's my new code w/ errors:
bash-3.1# cat -n cppexperiments/billscheduler.cpp
1 #include <iostream>
2 #include <cstdlib>
3 #include <stdlib.h>
4 #include <cstdio>
5 #include <stdio.h>
6 #include <malloc.h>
7 #include <string.h>
8 #include <stddef.h>
9
10 using namespace std;
11
12 int main()
13 {
14 char* billschedContentLength = getenv("CONTENT_LENGTH");
15 char* billschedBuffer;
16 int nContentLength = atoi(billschedContentLength);
17
18 billschedBuffer = malloc(billschedContentLength+1); // allocate a buffer
19 memset(billschedBuffer, 0, billschedContentLength+1); // zero it out
20
21 fread(billschedBuffer,1,billschedContentLength,stdin); // get data
22
23 cout << "Content-type: text/html" << endl << endl
24 << "<html>" << endl
25 << "<body>" << endl
26 << "<p>" << endl
27
28 <<billschedBuffer<<""<<endl
29
30 << endl
31 << "" << endl
32 << "" << endl
33 << "" ;
34
35
36 free(billschedBuffer);
37
38 return 0;
39
40 }
bash-3.1# g++ cppexperiments/billscheduler.cpp
cppexperiments/billscheduler.cpp: In function 'int main()':
cppexperiments/billscheduler.cpp:18: error: invalid conversion from 'char*' to 'size_t'
cppexperiments/billscheduler.cpp:18: error: initializing argument 1 of 'void* malloc(size_t)'
cppexperiments/billscheduler.cpp:18: error: invalid conversion from 'void*' to 'char*'
cppexperiments/billscheduler.cpp:19: error: invalid conversion from 'char*' to 'size_t'
cppexperiments/billscheduler.cpp:19: error: initializing argument 3 of 'void* memset(void*, int, size_t)'
cppexperiments/billscheduler.cpp:21: error: invalid conversion from 'char*' to 'size_t'
cppexperiments/billscheduler.cpp:21: error: initializing argument 3 of 'size_t fread(void*, size_t, size_t, FILE*)'
bash-3.1#
Sergei, I'm trying to profit from your honest attempt to simplify this without spoon feeding, but I'm just not seeing the answer.
...
If we are talking about 'fread', the compiler tells you something about illegal type conversion, while the manpage among other things tells you explicitly about the function arguments types.
So, reread the manpage paying attention to the function arguments types, reread the compiler error messages as a hint and recheck in your program whether you are supplying to 'fread' arguments of correct type.
Sergei, I'm trying to profit from your honest attempt to simplify this without spoon feeding, but I'm just not seeing the answer. A little more help, if I'm not asking too much?
ForzaItalia, I wondered about that too, but the example I'm using as a template is as such:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
void main()
{
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
}
Is the example correct?
No, I don't think so! The compiler is still complaining about line 18 and even though this is some kind of template, the template must be incorrect. You would still need to replace the argument to malloc. It would possibly work to allocate that much memory, but it is not what you want to allocate.
You still need to use this one:
Code:
int nContentLength = atoi(lpszContentLength);
By the way, if you have multiple error messages in your compilation, I would then usually concentrate one the first error message, because the subsequent error messages _might_ be side-effects of the first one ...
EDIT:
Hint: The other two error messages have the same cause!
Andi
Last edited by ForzaItalia2006; 07-04-2010 at 05:26 AM.
Bluegospel,
there's a problem with your datatype, try read the manpage for malloc for the expected return data type, change the billschedContentLength from char * to size_t.
So, since "billschedContentLength+1" on line 18 designates the address location plus one, not it's size, I should rather dereference that pointer, to provide the size of "Content_Length" to malloc, so it can allocate that size of memory, assigning an address to pointer billschedBuffer.
I should also dereference "billschedContentLength" on line 19, rather than using "billschedContentLength+1". This way billschedBuffer is a pointer to an area of memory *billschedContentLength" bytes long, whose cells all contain zero.
Finally, do the same for the third argument in fread on line 21.
So, since "billschedContentLength+1" on line 18 designates the address location plus one, not it's size, I should rather dereference that pointer, to provide the size of "Content_Length" to malloc, so it can allocate that size of memory, assigning an address to pointer billschedBuffer.
I should also dereference "billschedContentLength" on line 19, rather than using "billschedContentLength+1". This way billschedBuffer is a pointer to an area of memory *billschedContentLength" bytes long, whose cells all contain zero.
Finally, do the same for the third argument in fread on line 21.
I believe the purpose of line 14 is to get the size of the data from an html form, so I can initialize a buffer for the data in lines 18 & 19.
I'm guessing the obvious error is that,
since "billschedContentLength+1" on line 18 designates the address location plus one, not it's size, I should rather dereference that pointer, to provide the size of "Content_Length" to malloc, so it can allocate that size of memory, assigning an address to pointer billschedBuffer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.