473,772 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream buffer size conversion from size_t to std::streamsize --> Is this OK?

Hello,

I'm an intermediate noob reading-in data from ascii-file using an
ifstream object.

I have specified a c-style string buffer with size of type size_t and I
am specifying to use this buffer size as the number of characters to
read in using the function read(). The issue I am having is read()
expects that the value for the number of characters to read-in will be
of type std::streamsize , which is apparently signed int. My buffer
size, being of type size_t, is unsigned int.

I am getting the follwing compile-time warning in MSVC++ 2005 EE:

warning C4267: 'argument' : conversion from 'size_t' to
'std::streamsiz e', possible loss of data

1. What are the implications of this down-cast in this case? My guess
is that I will process the buffer thinking I have read-in my specified
size when in fact I have read-in upto only the maximum number allowable
by signed int.

2. If I want to read-in as much data as possible in one-shot, is my
only solution in this case to define the length of the _buffer array
using a signed int?

CODE SNIPPET FOLLOWS:

#include <fstream>
#include <string>
.. . .

// somewhere ...
size_t _nSizeBuf = (int) ( 1024 / sizeof(char) ); // Probably not big
enough to cause a problem.
char* _buffer = new char[_nSizeBuf];
std::string _sPathFileName = "C:\temp.tx t";

.. . .

void myFunction() {

std::ifstream inStream;
inStream.open( _sPathFileName. c_str() );
if( inStream )
{
// read() expects the 2nd argument to be signed int;
// however, _nSizeBuf is unsigned int.
inStream.read( _buffer, _nSizeBuf );
}

}
Thanks for any insight!

- direction40

Jan 21 '07 #1
9 8800

"Notebooker " <di*********@gm ail.comwrote in message
news:11******** **************@ q2g2000cwa.goog legroups.com...
Hello,

I'm an intermediate noob reading-in data from ascii-file using an
ifstream object.

I have specified a c-style string buffer with size of type size_t and I
am specifying to use this buffer size as the number of characters to
read in using the function read(). The issue I am having is read()
expects that the value for the number of characters to read-in will be
of type std::streamsize , which is apparently signed int. My buffer
size, being of type size_t, is unsigned int.

I am getting the follwing compile-time warning in MSVC++ 2005 EE:

warning C4267: 'argument' : conversion from 'size_t' to
'std::streamsiz e', possible loss of data

1. What are the implications of this down-cast in this case? My guess
is that I will process the buffer thinking I have read-in my specified
size when in fact I have read-in upto only the maximum number allowable
by signed int.
The maximum number of chars to read should be quite large for a signed int
dependong on your implementation. For a 4 byte, 8 bit signed int this would
be 2,147,483,648 characters. Now, as long as your specified size isn't over
2 billion (on a system with 4 byte/8 bit ints) there won't be a problem.
2. If I want to read-in as much data as possible in one-shot, is my
only solution in this case to define the length of the _buffer array
using a signed int?
Actually, an unsigned int can store a number larger than a signed int. For
our 4 byte/8 bit systems, an unsigned int can store a value up to
4,294,967,296. Just make sure you don't specify a number larger than the
unsigned int can hold, otherwise it will overflow the sign bit and become a
negative number, which would cause problems (unknown what .read() would do
with a negative value).

In your code, there won't be a problem, 1024 is quite a bit smaller by a
number of magnitudes than 2 billion. You can, if you desire, make this
warning go away:

inStream.read( _buffer, static_cast<sig ned int>( _nSizeBuf ) );
or probably more prefered:
inStream.read( _buffer, static_cast<std ::streamsize>(
_nSizeBuf ) );

in your trivial code this won't be a problem. You can check for overflow if
you want however, and should if you will be reading large files or am unsure
of the value of _nSizeBuf something like:

if ( static_cast<std ::streamsize>( _nSizeBuf ) < 0 )
throw "Buffer Size overflowing std::streamsize !";
else
inStream.read( _buffer, static_cast<std ::streamsize>( _nSizeBuf ) );

It depends on how the code will be used, if you have control of the
buffersize or it's a user defined value, etc...

In practice, however, you can usually just do the static_cast without
worrying about overflow unless you define a very large buffer.
CODE SNIPPET FOLLOWS:

#include <fstream>
#include <string>
. . .

// somewhere ...
size_t _nSizeBuf = (int) ( 1024 / sizeof(char) ); // Probably not big
enough to cause a problem.
char* _buffer = new char[_nSizeBuf];
std::string _sPathFileName = "C:\temp.tx t";

. . .

void myFunction() {

std::ifstream inStream;
inStream.open( _sPathFileName. c_str() );
if( inStream )
{
// read() expects the 2nd argument to be signed int;
// however, _nSizeBuf is unsigned int.
inStream.read( _buffer, _nSizeBuf );
}

}
Thanks for any insight!

- direction40

Jan 22 '07 #2

Notebooker wrote:
Hello,

I'm an intermediate noob reading-in data from ascii-file using an
ifstream object.

I have specified a c-style string buffer with size of type size_t and I
am specifying to use this buffer size as the number of characters to
read in using the function read(). The issue I am having is read()
expects that the value for the number of characters to read-in will be
of type std::streamsize , which is apparently signed int. My buffer
size, being of type size_t, is unsigned int.

I am getting the follwing compile-time warning in MSVC++ 2005 EE:

warning C4267: 'argument' : conversion from 'size_t' to
'std::streamsiz e', possible loss of data

1. What are the implications of this down-cast in this case? My guess
is that I will process the buffer thinking I have read-in my specified
size when in fact I have read-in upto only the maximum number allowable
by signed int.

2. If I want to read-in as much data as possible in one-shot, is my
only solution in this case to define the length of the _buffer array
using a signed int?

CODE SNIPPET FOLLOWS:

#include <fstream>
#include <string>
. . .

// somewhere ...
size_t _nSizeBuf = (int) ( 1024 / sizeof(char) ); // Probably not big
enough to cause a problem.
char* _buffer = new char[_nSizeBuf];
std::string _sPathFileName = "C:\temp.tx t";

. . .

void myFunction() {

std::ifstream inStream;
inStream.open( _sPathFileName. c_str() );
if( inStream )
{
// read() expects the 2nd argument to be signed int;
// however, _nSizeBuf is unsigned int.
inStream.read( _buffer, _nSizeBuf );
}

}
Thanks for any insight!

I've been coming across this a lot in going through COM interfaces
where 32 bit integers are common, whereas many of the C++ types I use
are 64 bit.

The trick of doing the cast and seeing if the result is negative seems
a bit scary to me. I wouldn't go anywhere near that. Luckily there is a
better solution.

Try something like this (not done with help of compiler - expect the
usual muppetry):

if ( nSizeBuf std::numeric_li mits< signed int >::max() )
// Won't work - we will have an overflow
else
inStream.read( _buffer, signed int( nSizeBuf );
K

Jan 22 '07 #3
On Jan 21, 10:36 pm, "Notebooker " <directio...@gm ail.comwrote:
Hello,

I'm an intermediate noob reading-in data from ascii-file using an
ifstream object.

I have specified a c-style string buffer with size of type size_t and I
am specifying to use this buffer size as the number of characters to
read in using the function read(). The issue I am having is read()
expects that the value for the number of characters to read-in will be
of type std::streamsize , which is apparently signed int. My buffer
size, being of type size_t, is unsigned int.
Is there a good reason not to use std::streamsize instead of size_t? By
using the same type as the library you get two things, first you don't
get the warnings and second you can be sure never to get values out of
range.
size_t _nSizeBuf = (int) ( 1024 / sizeof(char) );
Just like to point out that sizeof(char) == 1, always.

--
Erik Wikström

Jan 22 '07 #4
Jim Langston wrote:
[...]
in your trivial code this won't be a problem. You can check for overflow if
you want however, and should if you will be reading large files or am unsure
of the value of _nSizeBuf something like:

if ( static_cast<std ::streamsize>( _nSizeBuf ) < 0 )
throw "Buffer Size overflowing std::streamsize !";
else
inStream.read( _buffer, static_cast<std ::streamsize>( _nSizeBuf ) );

It depends on how the code will be used, if you have control of the
buffersize or it's a user defined value, etc...
I don't mean to be picky, but doesn't this only detect
half of the overflows, i.e. when a variable overflows,
it doesn't necessarily wrap to a negative value, right?

cheers,
- J.
Jan 23 '07 #5
Jim Langston wrote:

[snip]
Actually, an unsigned int can store a number larger than a signed int.
For our 4 byte/8 bit systems, an unsigned int can store a value up to
4,294,967,296. Just make sure you don't specify a number larger than the
unsigned int can hold, otherwise it will overflow the sign bit and become
a negative number, which would cause problems (unknown what .read() would
do with a negative value).

In your code, there won't be a problem, 1024 is quite a bit smaller by a
number of magnitudes than 2 billion. You can, if you desire, make this
warning go away:

inStream.read( _buffer, static_cast<sig ned int>( _nSizeBuf ) );
or probably more prefered:
inStream.read( _buffer, static_cast<std ::streamsize>(
_nSizeBuf ) );

in your trivial code this won't be a problem. You can check for overflow
if you want however, and should if you will be reading large files or am
unsure of the value of _nSizeBuf something like:

if ( static_cast<std ::streamsize>( _nSizeBuf ) < 0 )
throw "Buffer Size overflowing std::streamsize !";
else
inStream.read( _buffer, static_cast<std ::streamsize>( _nSizeBuf ) );
Hm: if _nSizeBuf is too large, the conversion cast has either undefined
behavior or at least implementation defined behavior. So when the test is
supposed to kick in it could theoretically fail. I would try to get by
without the cast:

if ( std::numeric_li mits<std::strea msize>::max() < _nSizeBuf ) {
...

Now, the issue might be complicated by arithmetic conversions doing
something. Does anybody know how to get the blessings of the standard for
this kind of check? (I hate signed integer types.)
Best

Kai-Uwe Bux
Jan 23 '07 #6
Thanks all for the great feedback.
>Just like to point out that sizeof(char) == 1, always.
Is the result of sizeof not platform / OS dependent? Eg: 64-bit OS char
will be 2 bytes ?

Originally I had the size of the buffer defined by a size_t because I
was using a non-dynamic array (no use of "new") and I had read that the
maximum size of an array was defined by a value of size_t. I guess I
interpreted that wrong.
>For a 4 byte, 8 bit signed int this would be 2,147,483,648 characters.
What is a 4 byte / 8-bit integer as 4bytes on a 32-bit OS = 32 bits.

I like the ideas for checking for overflow.

- direction40

Jan 24 '07 #7
In article <11************ **********@a75g 2000cwd.googleg roups.com>,
di*********@gma il.com says...
Thanks all for the great feedback.
Just like to point out that sizeof(char) == 1, always.

Is the result of sizeof not platform / OS dependent? Eg: 64-bit OS char
will be 2 bytes ?
No. "sizeof(cha r), sizeof(signed char) and sizeof(unsigned char) are 1;
the result of sizeof applied to any other fundamental type (3.9.1) is
implementation-defined." ($5.3.3/1).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 24 '07 #8
On Jan 24, 3:03 am, "Notebooker " <directio...@gm ail.comwrote:
For a 4 byte, 8 bit signed int this would be 2,147,483,648 characters.

What is a 4 byte / 8-bit integer as 4bytes on a 32-bit OS = 32 bits.
Jim used that notation to point out that there is no guarantee in C++
that a byte is 8 bits. While this is true for most modern machines it
does not have to be, I seem to recall that there have been some with 13
bits per byte (or was it 11?). One could imagine a computer with 16
bits per byte in which case 4 bytes would be 64 bits.

--
Erik Wikström

Jan 24 '07 #9
Ok, thanks for the extra knowledge. I'll be keeping those in mind.

- direction40

Jan 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3182
by: Darby Wong | last post by:
The documentation for file() says that the buffer size will be: system default if bufsize == -1, unbuffered if bufsize == 0, line buffered if bufsize == 1, or else it is just bufsize. I can't get this last one to work... if I write something like: >>> test = file("testfile","w",1024)
0
2722
by: shea martin | last post by:
I have a very IO instensive program. I need to know if there is a way to set the buffer size the ifstream and ofstream use. Or is it easier to just create a wrapper class and maintain my own buffer. Thanks, ~Shea M.
4
9327
by: Skyhorse | last post by:
In fact, this is not a programming problem, but I think I can seek help here Sometimes I saw terms about multimedia such as 'prebuffering', 'buffer size' , but I don't know their actual meaning. Recently I downloaded a MP3 player for Pocket PC, there is an option about streaming for user to enter Buffer Size (default is 64 kB) and Prebuffering Size (default is 32 kB). Can anyone suggest (or just guess) what is the difference between...
12
7886
by: Raja | last post by:
How to know the buffer size and increase buffer size in c++.
3
9742
by: Amy L. | last post by:
Is there a Buffer size that is optimial based on the framework or OS that is optimal when working with chunks of data? Also, is there a point where the buffer size might not be optimal (too large)? I am considering an 8K or 16K Buffer. The files sizes are random but range between 8K - 100K with the occasional files being several megs. Example: int _READBUFFER_ = 1024 ; fi = new FileInfo( args ) ;
13
31891
by: Sharon | last post by:
I'm using TcpClient and getting the Stream by: TcpClient tcpclnt = new TcpClient(); . . . Stream stm = tcpclnt.GetStream(); Now, when I'm trying to send a big buffer via stm.Write(...) I get an exception saying that the buffer is too big. I tried to set the tcpclnt.ReceiveBufferSize to the size of the buffer I"m trying to send, but the exception is still thrown, there is no transmit
2
4662
by: Macca | last post by:
Hi, My application uses an asynchronous socket server. The question I have is what i should set my socket server buffer size to. I will know the size of each data packet sent across the network and was considering setting the buffer size to this. In the examples I have seen on the net the buffer size is usually set to 1024 bytes and the socket is continually read until all data is received.
11
2409
by: Steven | last post by:
Hi, I need to write an application using sockets. I have a server and about 10 clients "speaking" at the same time with the server, so i guess i need to use asynchronous sockets. But the server will receive from the clients an ascii string with a viariable length. And in all the example i found, the size of the buffer is always fixed (private byte theBuffer = new byte). I probably misunderstood the use of the buffer !
3
3491
by: Chris | last post by:
Hi, I'm using CGI with MS IIS and Visual Studio 2003. My problem is when I'm trying to read post data, I use cin.get(buffer, size), where buffer is a new char and size is the atoi of getenv("CONTENT_LENGTH"). My problem is, it cuts off one character at the end. And if I use cin.get(buffer, size + 1), it just sits there waiting for more data. Do I have to roll my own solution, or use fread? Is there some way to
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.