LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-03-2010, 03:49 PM   #1
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Rep: Reputation: 53
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:~$
 
Old 07-03-2010, 04:02 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by bluegospel View Post
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.
 
Old 07-03-2010, 04:04 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Start from

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

man 3 memset
.

Last edited by Sergei Steshenko; 07-03-2010 at 04:07 PM.
 
Old 07-03-2010, 04:08 PM   #4
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by bluegospel View Post
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

Last edited by ForzaItalia2006; 07-03-2010 at 04:11 PM.
 
Old 07-03-2010, 04:37 PM   #5
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
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.
 
Old 07-03-2010, 04:48 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by bluegospel View Post
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.
 
Old 07-03-2010, 07:25 PM   #7
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
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?
 
Old 07-03-2010, 08:00 PM   #8
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
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#
 
Old 07-03-2010, 11:37 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by bluegospel View Post
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.
 
Old 07-03-2010, 11:51 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by bluegospel View Post
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.
 
Old 07-04-2010, 05:25 AM   #11
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by bluegospel View Post
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.
 
Old 07-04-2010, 05:26 PM   #12
tablebubble
LQ Newbie
 
Registered: Feb 2006
Location: Lagos, Nigeria
Distribution: Mandriva 2009 Spring
Posts: 12

Rep: Reputation: 0
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
 
Old 07-05-2010, 11:34 AM   #13
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
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?
 
Old 07-05-2010, 11:55 AM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by bluegospel View Post
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.

Last edited by Sergei Steshenko; 07-05-2010 at 12:17 PM.
 
Old 07-05-2010, 12:28 PM   #15
bluegospel
Member
 
Registered: Jan 2010
Distribution: centOS
Posts: 404

Original Poster
Rep: Reputation: 53
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?
 
  


Reply

Tags
c++, executable, execute, form, program


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Errors, Errors, and more Errors (KDE 3.4.x GUI Errors) Dralnu Linux - Software 2 05-13-2006 08:30 AM
Qt errors devit Programming 1 02-09-2004 03:45 PM
Errors during filesystem check with one kernel while no errors with other kernel Potentials Linux - General 11 12-30-2003 04:24 AM
QMAIL errors errors.. YourForum Linux - Software 0 11-27-2003 12:30 PM
2 many errors wesley Linux - Newbie 1 08-02-2001 11:42 PM


All times are GMT -5. The time now is 04:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration