I've allocated 4K memory and I'd like to use std::fill to fill each 1K
with a different value. Note: I could easily use a vector/deque but
I'm interested in a C style array.
int main()
{
int const max = 0x1000;
int *ptr_mem = new int [ max ];
int initial(1);
for ( int idx(0); idx < 4; ++idx )
{
std::fill ( ptr_mem, ptr_mem + 0x400, initial );
ptr_mem += 0x400; // move to the next 1K
initial += 1; // change the value
}
// call a display function to send output to a text - for assessment
delete [] ptr_mem;
ptr_mem = 0; // just in case.
}
I'm coming up short. I'm sure part of the problem is my limited
understanding of std::fill (back to the text in a minute on this). In
the meantime, how would I achieve this? 12 6357
ma740988 wrote: I've allocated 4K memory and I'd like to use std::fill to fill each 1K with a different value. Note: I could easily use a vector/deque but I'm interested in a C style array.
int main() { int const max = 0x1000; int *ptr_mem = new int [ max ]; int initial(1); for ( int idx(0); idx < 4; ++idx ) { std::fill ( ptr_mem, ptr_mem + 0x400, initial ); ptr_mem += 0x400; // move to the next 1K initial += 1; // change the value }
// call a display function to send output to a text - for assessment
delete [] ptr_mem; ptr_mem = 0; // just in case. }
Looks OK. What's the problem?
Michiel.Salt...@tomtom.com wrote: ma740988 wrote: I've allocated 4K memory and I'd like to use std::fill to fill each 1K with a different value. Note: I could easily use a vector/deque but I'm interested in a C style array.
int main() { int const max = 0x1000; int *ptr_mem = new int [ max ]; int initial(1); for ( int idx(0); idx < 4; ++idx ) { std::fill ( ptr_mem, ptr_mem + 0x400, initial ); ptr_mem += 0x400; // move to the next 1K initial += 1; // change the value }
// call a display function to send output to a text - for assessment
delete [] ptr_mem; ptr_mem = 0; // just in case. }
Looks OK. What's the problem?
Except that ptr_mem was changed from its original location. Unless it
is changed back before the delete[], there will be problems! Dare I ask
why the OP can't use a vector (or perhaps boost::array or a statically
allocated array)?
Cheers! --M
mlimber wrote: Dare I ask why the OP can't use a vector (or perhaps boost::array or a statically allocated array)?
I don't know why he can't use vector. boost::array isn't actually
standard and I don't know if it's ever going to be. Is it more portable
than vector across libraries? That's the big downside of vector.
He will be exceeding his 4K of memory unless sizeof(int) is 1 on his
system.
Earl Purple wrote:
[snip] boost::array isn't actually standard and I don't know if it's ever going to be. Is it more portable than vector across libraries? That's the big downside of vector.
Well, boost::array is at least part of TR1, and it provides an iterator
interface that would reduce pointer errors like the one you caught
below.
He will be exceeding his 4K of memory unless sizeof(int) is 1 on his system.
Good catch.
Cheers! --M
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... He will be exceeding his 4K of memory unless sizeof(int) is 1 on his system.
Good catch.
How so? The OP didn't say 4K of what. The array has 4K elements and all
those elements get filled.
> > Looks OK. What's the problem? Except that ptr_mem was changed from its original location. Unless it is changed back before the delete[], there will be problems!
Yikes!! Forgot to change it back!! So much for these source code
analysis tools. What a joke!!
Dare I ask why the OP can't use a vector (or perhaps boost::array or a statically allocated array)?
Limitation of the vendor hardware. If I want to move data and move it
fast, I take advantage of the vendor hardware. ie. their DMA engine.
Having said that it's a call to a vendor API, which is a C API.
I've experimented with containers usage and while transfers from source
to destination appeared OK with the vendor API. The contents at the
destination was all 'garbage'.
One thing, I'm tempted to do is compare the performance of
memcopy/std::copy versus the vendor API. Something tells me the vendor
API is memcopy under the hood. Even so the vendor API has a dma
engine (hardware spewing 128 byte bursts) under the hood so it should
blast memcopy/std::copy out the window.
We'll see!!
Andrew Koenig wrote: "mlimber" <ml*****@gmail.com> wrote in message news:11**********************@g14g2000cwa.googlegr oups.com...
He will be exceeding his 4K of memory unless sizeof(int) is 1 on his system.
Good catch.
How so? The OP didn't say 4K of what. The array has 4K elements and all those elements get filled.
Uhh, good catch. I didn't do the math; I just assumed Earl Purple had
done it correctly. Mea culpa.
Cheers! --M
ma740988 wrote: Dare I ask why the OP can't use a vector (or perhaps boost::array or a statically allocated array)? Limitation of the vendor hardware. If I want to move data and move it fast, I take advantage of the vendor hardware. ie. their DMA engine. Having said that it's a call to a vendor API, which is a C API. I've experimented with containers usage and while transfers from source to destination appeared OK with the vendor API. The contents at the destination was all 'garbage'.
std::vector's memory is guaranteed to be contiguous, so something like
this should work:
vector<int> data( 4096 );
GetDataFromDMA( &data[0], data.size() );
If it doesn't, it's not likely std::vector's fault. That code should
behave the same as if you allocated the memory yourself:
const unsigned int size = 4096;
int *const data = new int[ size ];
GetDataFromDMA( &data[0], size );
except that the usual advantages (and usually minor disadvantages) of
std::vector apply.
One thing, I'm tempted to do is compare the performance of memcopy/std::copy versus the vendor API. Something tells me the vendor API is memcopy under the hood. Even so the vendor API has a dma engine (hardware spewing 128 byte bursts) under the hood so it should blast memcopy/std::copy out the window.
<OT>
The performance (and perhaps even method) likely depends on where
you're DMAing from and to.
</OT>
Cheers! --M
ma740988 wrote: Limitation of the vendor hardware. If I want to move data and move it fast, I take advantage of the vendor hardware. ie. their DMA engine. Having said that it's a call to a vendor API, which is a C API. I've experimented with containers usage and while transfers from source to destination appeared OK with the vendor API. The contents at the destination was all 'garbage'. One thing, I'm tempted to do is compare the performance of memcopy/std::copy versus the vendor API. Something tells me the vendor API is memcopy under the hood. Even so the vendor API has a dma engine (hardware spewing 128 byte bursts) under the hood so it should blast memcopy/std::copy out the window. We'll see!!
If you want to be able to take advantage of things like memcpy then you
might use
std::basic_string<int, myIntTraits >
where you write myIntTraits in the style of char_traits to optimise
copies.
The problem is that if your API wants an int* buffer then you cannot
get one from &intstr[0] like you can with &vec[0]. You can get a
continguous const int* buffer by calling c_str() or data().
(And when you said 4K of memory I assumed you meant 4K bytes. The code
was correct though in that you allocated 4K ints, just you deleted the
wrong pointer as was pointed out). vector<int> data( 4096 ); GetDataFromDMA( &data[0], data.size() );
If it doesn't, it's not likely std::vector's fault. That code should behave the same as if you allocated the memory yourself:
Uhmmn, I'd have to check again but if memory serves here's a test
'case' I ran that failed
int *ptr_source = new int[ 4096 ];
std::fill ( ptr_source, ptr_source + 4096, 0xA5 );
int *ptr_dest = new int [ 4096 ];
<non standard >
gtDmaTransfer( /*some DMA engine*/, ptr_source, ptr_dest, 0x4096);
</non standard >
Works!!
Now
vector<int> int_vec1(4096);
// same story - std::fill
vector<int> int_vec2(4096);
<non standard >
gtDmaTransfer( /*some DMA engine*/, &int_vec1[0], &int_vec2[0],
0x4096);
</non standard >
Didn't.
A little surprising to me but I thought the fact that the vector comes
with copy constructor etc, might have hosed things up. Ironically, (in
my mind - I thought) if I'm transferring raw bits, the API shouldn't
care about the fact that it's a 'container'.. Oh well, I just need to
play with it some more I guess.
ma740988 wrote:
Uhmmn, I'd have to check again but if memory serves here's a test 'case' I ran that failed int *ptr_source = new int[ 4096 ]; std::fill ( ptr_source, ptr_source + 4096, 0xA5 ); int *ptr_dest = new int [ 4096 ];
<non standard > gtDmaTransfer( /*some DMA engine*/, ptr_source, ptr_dest, 0x4096); </non standard >
Works!!
Well, maybe, but it's impossible to say, since this is a code fragment,
not a test case. It looks suspicious, though. The code allocates space
for 4096 (0x1000) integers, then copies some unexplained number of bytes
that's apparently related to 16,534 (0x4096).
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com) If you want to be able to take advantage of things like memcpy then you might use
std::basic_string<int, myIntTraits >
where you write myIntTraits in the style of char_traits to optimise copies.
"myIntraits in the style of char_traits to optimise copies". I think
I'm following you here but could you elaborate on this a little? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ma740988 |
last post by:
Consider:
# include <iostream>
using std::cout;
using std::cin;
using std::endl;
# include <list>
using std::list;
|
by: red floyd |
last post by:
I got an error by using std::fill to set an array of pointers to 0.
e.g.:
class XXX;
XXX* v;
std::fill(v, v+30, 0); // <-- ERROR -- cant' match template type
I have to either explicitly...
|
by: Joe C |
last post by:
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...
|
by: ma740988 |
last post by:
The hierachy for standard exception lists:
bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failure,
runtime_error and bad_exception
runtime_error and logic_error has subsets.
The...
|
by: ma740988 |
last post by:
Trying to get more acclimated with the use of function objects. As
part of my test, consider:
# include <vector>
# include <iostream>
# include <algorithm>
#include <stdexcept>
#include...
|
by: Michele Moccia |
last post by:
Simple newbie question:
I want to set an int array to value 5
int i;
int val=5;
std::uninitialized_fill(&i, &i, val);
OR
std::uninitialized_fill(i, i+3, val);
|
by: ma740988 |
last post by:
I've been perusing for some time now - I suppose some of the algorithms
in Josuttis. 'Today', I do alot of funny math on data resident within
memory. As part of this. I find myself using...
|
by: cpisz |
last post by:
I saw that using std::fill was the way to go for setting all elements
of an array to some value in one foul swoop. However when I tryed it I
am getting an error.
Can I only use this for vectors...
|
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 ) {...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
| |