How should I specify size?

W

William Payne

Hello, when using cin.getline() with a char array, how should I specify the
size (the second parameter of getline)?

const int buffer_size = 256;
char buffer[buffer_size];

std::cin.getline(buffer, std::streamsize(buffer_size));
or
std::cin.getline(buffer, buffer_size);
?

I know, I should use std::string and I usually do, but I still want to know
the proper way. I am writing a program that will be probably converted to C
so I ended up with char arrays instead of std::string, and the program uses
a third-party api which is based on C.

// William Payne
 
M

Mike Wahler

William Payne said:
Hello, when using cin.getline() with a char array, how should I specify the
size (the second parameter of getline)?

const int buffer_size = 256;
char buffer[buffer_size];

std::cin.getline(buffer, std::streamsize(buffer_size));
or
std::cin.getline(buffer, buffer_size);
?

I know, I should use std::string and I usually do, but I still want to know
the proper way. I am writing a program that will be probably converted to C
so I ended up with char arrays instead of std::string, and the program uses
a third-party api which is based on C.

The C++ standard shows these prototypes:

basic_istream<charT,traits>& getline(char_type* s, streamsize n);
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim);

so I'd use 'streamsize' type.

But with C, you don't have that type, use 'size_t' to specify
object sizes or counts. And when calling library functions,
simply use whatever type the prototype specifies.


-Mike
 
W

William Payne

Mike Wahler said:
William Payne said:
Hello, when using cin.getline() with a char array, how should I specify the
size (the second parameter of getline)?

const int buffer_size = 256;
char buffer[buffer_size];

std::cin.getline(buffer, std::streamsize(buffer_size));
or
std::cin.getline(buffer, buffer_size);
?

I know, I should use std::string and I usually do, but I still want to know
the proper way. I am writing a program that will be probably converted
to
C
so I ended up with char arrays instead of std::string, and the program uses
a third-party api which is based on C.

The C++ standard shows these prototypes:

basic_istream<charT,traits>& getline(char_type* s, streamsize n);
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim);

so I'd use 'streamsize' type.

But with C, you don't have that type, use 'size_t' to specify
object sizes or counts. And when calling library functions,
simply use whatever type the prototype specifies.


-Mike

Thanks alot for your reply, Mike. I will use std::streamsize.

/ William Payne
 
D

David Rubin

William said:
Hello, when using cin.getline() with a char array, how should I specify the
size (the second parameter of getline)?

const int buffer_size = 256;
char buffer[buffer_size];

std::cin.getline(buffer, std::streamsize(buffer_size));
or
std::cin.getline(buffer, buffer_size);
?

std::cin.getline(buffer, sizeof buffer);

/david
 
J

Jack Klein

William Payne said:
Hello, when using cin.getline() with a char array, how should I specify the
size (the second parameter of getline)?

const int buffer_size = 256;
char buffer[buffer_size];

std::cin.getline(buffer, std::streamsize(buffer_size));
or
std::cin.getline(buffer, buffer_size);
?

I know, I should use std::string and I usually do, but I still want to know
the proper way. I am writing a program that will be probably converted to C
so I ended up with char arrays instead of std::string, and the program uses
a third-party api which is based on C.

The C++ standard shows these prototypes:

basic_istream<charT,traits>& getline(char_type* s, streamsize n);
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim);

so I'd use 'streamsize' type.

But with C, you don't have that type, use 'size_t' to specify
object sizes or counts. And when calling library functions,
simply use whatever type the prototype specifies.


-Mike

I disagree here, Mike. Since C++ requires a proper prototype in
scope, the conversion is automatic. The functional-like cast just
adds unnecessary clutter in this case.

My rule is never provide an explicit cast for an implicit automatic
loss-less conversion unless the circumstances are complex enough that
someone reading the code might be mislead.
 
M

Mike Wahler

Jack Klein said:
William Payne said:
Hello, when using cin.getline() with a char array, how should I
specify
the
size (the second parameter of getline)?

const int buffer_size = 256;
char buffer[buffer_size];

std::cin.getline(buffer, std::streamsize(buffer_size));
or
std::cin.getline(buffer, buffer_size);
?

I know, I should use std::string and I usually do, but I still want to know
the proper way. I am writing a program that will be probably converted
to
C
so I ended up with char arrays instead of std::string, and the program uses
a third-party api which is based on C.

The C++ standard shows these prototypes:

basic_istream<charT,traits>& getline(char_type* s, streamsize n);
basic_istream<charT,traits>& getline(char_type* s, streamsize n,
char_type delim);

so I'd use 'streamsize' type.

But with C, you don't have that type, use 'size_t' to specify
object sizes or counts. And when calling library functions,
simply use whatever type the prototype specifies.


-Mike

I disagree here, Mike. Since C++ requires a proper prototype in
scope, the conversion is automatic. The functional-like cast just
adds unnecessary clutter in this case.

But do we have any guarantee that 'streamsize' is at least as large
as e.g. 'int'?
My rule is never provide an explicit cast for an implicit automatic
loss-less conversion unless the circumstances are complex enough that
someone reading the code might be mislead.

I Agree.

-Mike
 
M

Martijn Lievaart

But do we have any guarantee that 'streamsize' is at least as large
as e.g. 'int'?

Actually not, 27.4.1 clause 2:

"The type streamsize is a synonym for one of the signed basic integral
types. It is used to represent the number of characters transferred in an
I/O operation, or the size of I/O buffers.266)"

So the standard allows signed char. In practice it should be at least the
signed couterpart to size_t, but I think this is a defect.

M4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top