LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with errors: C++ (https://www.linuxquestions.org/questions/programming-9/help-with-errors-c-817873/)

bluegospel 07-03-2010 03:49 PM

Help with errors: C++
 
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:~$

Sergei Steshenko 07-03-2010 04:02 PM

Quote:

Originally Posted by bluegospel (Post 4023065)
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.

Sergei Steshenko 07-03-2010 04:04 PM

Start from

Quote:

/home/barth/cppexperiments/billscheduler.cpp:17: error: 'memset' was not declared in this scope
by reading

man 3 memset
.

ForzaItalia2006 07-03-2010 04:08 PM

Quote:

Originally Posted by bluegospel (Post 4023065)
Code:

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

bluegospel 07-03-2010 04:37 PM

Here's the code again w/ line numbers:

Code:


barth@bluegospel:~$ 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       
    8        using namespace std;
    9       
    10        int main()
    11        {
    12          char* billschedContentLength = getenv("CONTENT_LENGTH");
    13          char* billschedBuffer;
    14          int nContentLength = atoi(billschedContentLength);
    15       
    16          billschedBuffer = malloc(billschedContentLength+1);  // allocate a buffer
    17          memset(billschedBuffer, 0, billschedContentLength+1);  // zero it out
    18       
    19          fread(billschedBuffer,1,billschedContentLength,stdin);  // get data
    20       
    21          cout << "Content-type: text/html" << endl << endl
    22          << "<html>" << endl
    23          << "<body>" << endl
    24          << "<p>" << endl
    25         
    26          <<billschedBuffer<<""<<endl
    27       
    28          << endl
    29          << "" << endl
    30          << "" << endl
    31          << "" ;
    32       
    33       
    34          free(billschedBuffer);
    35       
    36          return 0;
    37       
    38        }
barth@bluegospel:~$

Will return in about an hour.

Sergei Steshenko 07-03-2010 04:48 PM

Quote:

Originally Posted by bluegospel (Post 4023094)
Here's the code again w/ line numbers:

Code:


barth@bluegospel:~$ 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       
    8        using namespace std;
    9       
    10        int main()
    11        {
    12          char* billschedContentLength = getenv("CONTENT_LENGTH");
    13          char* billschedBuffer;
    14          int nContentLength = atoi(billschedContentLength);
    15       
    16          billschedBuffer = malloc(billschedContentLength+1);  // allocate a buffer
    17          memset(billschedBuffer, 0, billschedContentLength+1);  // zero it out
    18       
    19          fread(billschedBuffer,1,billschedContentLength,stdin);  // get data
    20       
    21          cout << "Content-type: text/html" << endl << endl
    22          << "<html>" << endl
    23          << "<body>" << endl
    24          << "<p>" << endl
    25         
    26          <<billschedBuffer<<""<<endl
    27       
    28          << endl
    29          << "" << endl
    30          << "" << endl
    31          << "" ;
    32       
    33       
    34          free(billschedBuffer);
    35       
    36          return 0;
    37       
    38        }
barth@bluegospel:~$

Will return in about an hour.

And read

man 3 fread

- it contains info explaining the

Quote:

/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*)'
errors.

bluegospel 07-03-2010 07:25 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);
}

Is the example correct?

bluegospel 07-03-2010 08:00 PM

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 Steshenko 07-03-2010 11:37 PM

Quote:

Originally Posted by bluegospel (Post 4023172)
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 Steshenko 07-03-2010 11:51 PM

Quote:

Originally Posted by bluegospel (Post 4023179)
Sergei, were you hinting that I should include <string.h> in my pre-processing directives? ...

Yes - because the manpage says so. Now 'memset' is known to the compiler. Also read my previous post.

ForzaItalia2006 07-04-2010 05:25 AM

Quote:

Originally Posted by bluegospel (Post 4023172)
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

tablebubble 07-04-2010 05:26 PM

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.

cheers

bluegospel 07-05-2010 11:34 AM

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.

Is this correct?

Sergei Steshenko 07-05-2010 11:55 AM

Quote:

Originally Posted by bluegospel (Post 4024374)
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.

Is this correct?

Here are two interrelated lines of your code:

Code:

  char* lpszContentLength = getenv("CONTENT_LENGTH");
...
  lpszBuffer = malloc(lpszContentLength+1);  // allocate a buffer

What did you mean to do using them and do you understand there is an obvious error ? Answer at least the first part of the question about the intent.

bluegospel 07-05-2010 12:28 PM

Sergei, the excerpt you most recently posted is actually from the tutorial I'm using as a model.

Here are my lines, corresponding to the lines you reference from the model:

Code:

14 char* billschedContentLength = getenv("CONTENT_LENGTH");

18 billschedBuffer = malloc(billschedContentLength+1); // allocate a buffer

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.

Am I in the right ballpark?


All times are GMT -5. The time now is 02:50 PM.