473,511 Members | 16,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memset vs fill and iterators vs pointers

I'm a hobbiest, and made the forray into c++ from non-c type languages about
a year ago. I was "cleaning up" some code I wrote to make it more "c++
like" and have a few questions. I'm comfortable using new/delete when
dealing with arrays, and, so-far haven't used the STL (eg vectors) very much
when dealing with POD. I'm using a class to dump files into. The class
puts the file data into a 32-bit array, then offers both 32-bit and char* to
the file data. The reason I've done this blasphomy is that I need to access
the file-header information from a byte-oriented viewpoint, but the actual
data is 32-bit and *will* be word-aligned with respect to the start of the
file.

first...is there a better way using streams to do the same?

second...I've been using memcopy and memset...for POD, are there compelling
reasons to use copy() and fill/fill_n() instead (when dealing with POD
arrays)?

Third...if I load a file into a 32-bit vector and the file byte length is
not an even multiple of 4-bytes...what happens to the last (incomplete) word
of the file?

I hope my questions are clear.

Joe


Jul 22 '05 #1
5 6181
"Joe C" <jk*****@bellsouth.net> wrote in message
news:kq*******************@bignews6.bellsouth.net. ..
I'm a hobbiest, and made the forray into c++ from non-c type languages about a year ago. I was "cleaning up" some code I wrote to make it more "c++
like" and have a few questions. I'm comfortable using new/delete when
dealing with arrays, and, so-far haven't used the STL (eg vectors) very much when dealing with POD. I'm using a class to dump files into. The class
puts the file data into a 32-bit array, then offers both 32-bit and char* to the file data. The reason I've done this blasphomy is that I need to access the file-header information from a byte-oriented viewpoint, but the actual
data is 32-bit and *will* be word-aligned with respect to the start of the
file.

first...is there a better way using streams to do the same?
I don't understand your question. I'm sure you could use C++ stream classes
instead of C I/O functions, if that's what you're talking about. I'm not
sure if that's necessarily better, in your situation.
second...I've been using memcopy and memset...for POD, are there compelling reasons to use copy() and fill/fill_n() instead (when dealing with POD
arrays)?
While std::memcpy is not guaranteed to work for overlapping regions of
memory, std::copy works for overlapping sequences. Also, std::memset sets a
region of memory to all bits zero, and that's not guaranteed to be the
representation of zero for certain types (e.g. pointers may not use all bits
zero to represent null). C++'s std::fill does not inherently write "all
bits zero" to a region of memory. Using the C++ equivalents may help you
escape some of the gotchas that the C functions bring to the table.
Third...if I load a file into a 32-bit vector and the file byte length is
not an even multiple of 4-bytes...what happens to the last (incomplete) word of the file?


The std::vector is not so different from the array. In fact, the
std::vector uses an array internally. See the FAQ
(http://www.parashift.com/c++-faq-lite/), section 34 ("Container classes and
templates"), question 3 ("Is the storage for a std::vector<T> guaranteed to
be contiguous?").

--
David Hilsee
Jul 22 '05 #2
"David Hilsee" <da*************@yahoo.com> wrote in message
news:yd********************@comcast.com...
<snip>
While std::memcpy is not guaranteed to work for overlapping regions of
memory, std::copy works for overlapping sequences. Also, std::memset sets a region of memory to all bits zero, and that's not guaranteed to be the
representation of zero for certain types (e.g. pointers may not use all bits zero to represent null). C++'s std::fill does not inherently write "all
bits zero" to a region of memory. Using the C++ equivalents may help you
escape some of the gotchas that the C functions bring to the table.
Here I assumed that zero was being passed to memset, which is the common
usage. I also failed to mention that std::fill and std::copy are more
type-safe than their C equivalents and do not require the programmer to
consider the size of the elements (using code like numElems *
sizeof(Element)). In general, they are easier to use.

<snip> The std::vector is not so different from the array. In fact, the
std::vector uses an array internally. See the FAQ
(http://www.parashift.com/c++-faq-lite/), section 34 ("Container classes and templates"), question 3 ("Is the storage for a std::vector<T> guaranteed to be contiguous?").


Here, I should have instead said "In fact, the std::vector uses contiguous
storage internally".

--
David Hilsee
Jul 22 '05 #3
On Mon, 23 Aug 2004 00:31:30 -0400, "Joe C" <jk*****@bellsouth.net>
wrote:
I'm a hobbiest, and made the forray into c++ from non-c type languages about
a year ago. I was "cleaning up" some code I wrote to make it more "c++
like" and have a few questions. I'm comfortable using new/delete when
dealing with arrays, and, so-far haven't used the STL (eg vectors) very much
when dealing with POD. I'm using a class to dump files into. The class
puts the file data into a 32-bit array, then offers both 32-bit and char* to
the file data. The reason I've done this blasphomy is that I need to access
the file-header information from a byte-oriented viewpoint, but the actual
data is 32-bit and *will* be word-aligned with respect to the start of the
file.

first...is there a better way using streams to do the same?
Not really, at least not if your code is already working. You would be
better off having the class do the reading of the header, so that this
detail is encapsulated from users of the class. Then it would return a
pointer to the start of the *real* "32-bit array" (by which I assume
you mean an array of unsigned int or similar).
second...I've been using memcopy and memset...for POD, are there compelling
reasons to use copy() and fill/fill_n() instead (when dealing with POD
arrays)?
For POD, copy is like memmove in that it works with overlapping
ranges.

fill is different from memset. memset only allows you to set every
byte to the same value, whereas fill allows you to set every element
(which may be, e.g., an unsigned int) to the same value.
Third...if I load a file into a 32-bit vector and the file byte length is
not an even multiple of 4-bytes...what happens to the last (incomplete) word
of the file?


Assuming the vector is 0-initialized, it depends on the byte order
your platform uses. Either the high order or low order bytes of the
last value will be 0, which will give the word a particular value.

Tom
Jul 22 '05 #4

"tom_usenet" <to********@hotmail.com> wrote in message
news:17********************************@4ax.com...
On Mon, 23 Aug 2004 00:31:30 -0400, "Joe C" <jk*****@bellsouth.net>
wrote:
I'm a hobbiest, and made the forray into c++ from non-c type languages abouta year ago. I was "cleaning up" some code I wrote to make it more "c++
like" and have a few questions. I'm comfortable using new/delete when
dealing with arrays, and, so-far haven't used the STL (eg vectors) very muchwhen dealing with POD. I'm using a class to dump files into. The class
puts the file data into a 32-bit array, then offers both 32-bit and char* tothe file data. The reason I've done this blasphomy is that I need to accessthe file-header information from a byte-oriented viewpoint, but the actualdata is 32-bit and *will* be word-aligned with respect to the start of thefile.

first...is there a better way using streams to do the same?


Not really, at least not if your code is already working. You would be
better off having the class do the reading of the header, so that this
detail is encapsulated from users of the class. Then it would return a
pointer to the start of the *real* "32-bit array" (by which I assume
you mean an array of unsigned int or similar).
second...I've been using memcopy and memset...for POD, are there compellingreasons to use copy() and fill/fill_n() instead (when dealing with POD
arrays)?


For POD, copy is like memmove in that it works with overlapping
ranges.

fill is different from memset. memset only allows you to set every
byte to the same value, whereas fill allows you to set every element
(which may be, e.g., an unsigned int) to the same value.
Third...if I load a file into a 32-bit vector and the file byte length is
not an even multiple of 4-bytes...what happens to the last (incomplete) wordof the file?


Assuming the vector is 0-initialized, it depends on the byte order
your platform uses. Either the high order or low order bytes of the
last value will be 0, which will give the word a particular value.

Tom


Thanks, Tom. Your reply is really helpful. I think that I will leave
things as they are, since the prog is working and useful for me, and has
already been fairly thoroughly streamlined. One more question...suppose I
have large amounts of memory that I want to clear. Do you know if there is
a speed advantage if it's done using fill with the native integer data-type
vs using byte-oriented memset? It's a little hard to measure, since the
operation is really fast in either case...as such I suppose it makes no
practical difference, huh?

Thanks again for the reply.

Joe
Jul 22 '05 #5
On Mon, 23 Aug 2004 13:09:01 -0400, "Joe C" <jk*****@bellsouth.net>
wrote:
Thanks, Tom. Your reply is really helpful. I think that I will leave
things as they are, since the prog is working and useful for me, and has
already been fairly thoroughly streamlined. One more question...suppose I
have large amounts of memory that I want to clear. Do you know if there is
a speed advantage if it's done using fill with the native integer data-type
vs using byte-oriented memset? It's a little hard to measure, since the
operation is really fast in either case...as such I suppose it makes no
practical difference, huh?


You'll love this article:

http://www.cuj.com/documents/s=7990/...r/alexandr.htm

Note that for zeroing large amounts of memory, all reasonable
techniques work out much the same, since the bottleneck is memory
bandwidth.

Tom
Jul 22 '05 #6

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

Similar topics

4
21293
by: Leon | last post by:
Hi all. I'm a bit confused about the use of STL iterators and pointers, and I was wondering if maybe you could give me some pointers ;) Is it possible to convert between iterators and pointers...
6
8180
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
22
3117
by: srivatsan_b | last post by:
Hi, Can somebody explain whether an explicit typecast is mandatory while calling memset function for a structure? like in the following code snapshot..... struct some_structure x;...
21
8695
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
16
10492
by: hack_tick | last post by:
hi Guys! I am having a class, having around 300+ pointers ONLY, I need to set them all NULL, when the object is created. The best way I think is memset(this, NULL, sizeof(classname)); ...
27
5178
by: volunteers | last post by:
I met a question about memset and have no idea right now. Could anybody give a clue? Thanks memset is sometimes used to initialize data in a constructor like the example below. What is the...
4
5024
by: pauldepstein | last post by:
This is copy-pasted from cplusplus.com: BEGINNING OF QUOTE void * memset ( void * buffer, int c, size_t num ); Fill buffer with specified character. Sets the first num bytes pointed by...
19
2099
by: fungus | last post by:
I mentioned earlier to day that I was moving some code from VC++6 to VC++2005 and having trouble with the new iterators. There's all sorts of problems cropping up in the code thanks to this...
1
2740
by: Gennaro Prota | last post by:
Hi, I have the following function template template< typename T, std::size_t n > void secure_fill( volatile T ( &arr ), const T & value = T() ) { for( std::size_t i( 0 ); i < n; ++i ) {...
0
7242
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,...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
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...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3222
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...
0
1572
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
446
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...

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.