|
I have a function which return an std::vector<T>
it is quite big and moved around a lot and kept for a long time, so I put it into a
std::shared_ptr<std::vector<T>>
Now I need to access the vector as an array
with
std::vector<T> a;
I can write
T* pa = &a[0];
But this doesn't work with a std::shared_ptr<std::vector<T>>
What can I do?
Remark
Why don't I just pass it by reference around?
Well, it is created in a function, and then it is captured by a lambda that is spawn regularly into a task (i.e. in another thread) which would be the only reference left.
And I want to avoid copying the vector (it is big) and destructing when it goes out of scope, I want to create it only once and reuse it / keep it alive!...
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
Super Lloyd wrote: Now I need to access the vector as an array What's wrong with it?
e.g.
shared_prt<vector<int>> p(new vector<int>{1,2,3,4});
cout << (*p)[2] << endl;
Veni, vidi, vici.
|
|
|
|
|
Nothing, just me struggling with the syntax...
But thanks for your tip, it works indeed!!!
So simple! Could I add in hindsight! ^^
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
I use some ATL functions to create a Media control. But, it seems that I can only use Dynamic link to ATL option, when I switch to Static link to ATL. I just can't create the control successfully.
I'm so confused. Anyone can help me?
#include <wmp.h>
#include <atlbase.h>
#include <atlcom.h>
#include <atlhost.h>
#include <atlctl.h>
CAxWindow m_wndView; m_spWMPPlayer;
CComPtr<IAxWinHostWindow> spHost;
HRESULT hr;
m_wndView.Create(m_hWnd, rect, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);
hr = m_wndView.QueryHost(&spHost);
if(!SUCCEEDED(hr)) return FALSE;
hr = spHost->CreateControl(CComBSTR(_T("{6BF52A52-394A-11d3-B153-00C04F79FAA6}")), m_wndView, 0);
if(!SUCCEEDED(hr)) return FALSE;
hr = m_wndView.QueryControl(&m_spWMPPlayer);
if(!SUCCEEDED(hr)) return FALSE;
|
|
|
|
|
You need to look at the actual error code value when you get a FAIL result, in order to see why it has failed.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi,when I Write files ,I want a carton show to let user know a data is performing.
I want to use a work thread in background to writing file,when the file finish ,it may be send a message to main window destroy the carton,but I dont't know how to do it ,please help me
|
|
|
|
|
Use an event. With MFC, check out CEvent . I talk about this briefly in this article.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Use PostMessage()[^].... make sure you know how to catch the message on your receiving window as well. MSDN usually has good tutorials. It's easier if you're using MFC since the message map is easily defined.
|
|
|
|
|
|
Hi,
cherry729 wrote: I want to use a work thread in background to writing file,when the file finish ,it may be send a message to main window destroy the carton,but I dont't know how to do it ,please help me
If you want to save some time and avoid re-inventing the wheel then you might be interested in using the old Animation Window class[^]. If you are using the MFC library... this basic window class is represented in the CAnimateCtrl Class[^]. You can open, start, stop and pause the animation by simply sending window messages from any thread in your application.
Best Wishes,
-David Delaune
|
|
|
|
|
I am running into annoying problem using capDriverConnect API in an ancient VFW library.
I have a simple two step code using capCreateCaptureWindow and capDriverConnect.
The "problem " is that capDriverConnect randomly copies the captured data into the CView derived class.
It appears that when the code is run in debug the data is blank ( black ) about 50 % of the time and when run (.exe - still in debug mode) it fills the data ( single frame from USB camera ) about 80% of the time.
<b>I cannot find anywhere ( I did Google for it) if capDriverConnect suppose to do that and / or how to stop it. </b>
I need to capture frames for further processing and that code is working correctly.
Please - no commentary about how obsolete or crappy VFW is. It is used in OpenCV and that library does ton of video processing which I need.
I am not trying to be a moron, but if you do not have a clue why capDriverConnect
behave this way save your and mine time and do not reply. OK?
Cheers Vaclav
|
|
|
|
|
One embedded program has a MemCpy(), like:
void MemCpy(unsigned byte *pTg, unsigned byte *pSc, size_t slen)
{
volatile unsigned byte *ps, *pt;
for (pt=pTg, ps=pSc; ps<(pSc+slen); pt++,ps++)
*pt = *ps;
}
I wonder why the coder didnot use standard C memcpy function, what is the merit to rewrite a memcpy function like the above?
modified 4-Jan-13 14:46pm.
|
|
|
|
|
If this is connected to your question below, it is possible that the system available on the embedded device did not include a full standard C library. Alternatively this could just save using the library and thus reduce memory requirements.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Thanks, but according to the compiler manual:
//the compiler include the standard set of C library functions.//
I think which means the standard C function like memcpy should be accepted by the MCU, and the compiler is specific fot the MCU family.
|
|
|
|
|
Perhaps the library implementation is less efficient, you could look at the disassembly of the library memcpy() to find out. Or as Richard pointed out, maybe the person didn't want to deal with memory requirements of using the library.
|
|
|
|
|
Well, as I said before you are showing us a few lines of code and asking why it is done this way, but we do not have any information on the implementation of this product. You need to talk to the people who wrote this and ask them why they did it this way.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
econy wrote: I wonder why the coder didnot use standard C memcpy functio
1. They didn't know it existed.
2. The 'volatile' is important.
3. They thought 'volatile' was important. Perhaps due to the source of the pointers being passed.
4. They are doing a lot of very small copies and the compiler will optimize those with a user method but will not do so with a library method (it will always call the library method rather than attempting to unroll it.)
5. memcpy returns a value, the method doesn't. Perhaps that was important in terms of stack space.
6. The code was originally written with a very early version of the compiler and memcpy was buggy. Or the coder thought it was buggy. No one has challenged that since then.
Could also be a combination of the above.
|
|
|
|
|
For very small copies, this may be faster since it doesn't bother looking at alignment. However, if intrinsics are enabled, many compilers will identify the code as a copy and use an intrinsic instead (an inline memcpy.)
(See previous comment. Also note that years ago, especially for very small memory usage, many embedded developers wouldn't use the CRT; they wouldn't use libraries of any kind. This wasn't arrogance, but that they had to control every aspect of the code. Even today, I deal with many serious problems where third party libraries are used, including STL and Boost.)
modified 7-Jan-13 16:32pm.
|
|
|
|
|
Hi, I am reading a program, found a code block like the following:
for (i=0; i<16; i++) { Bstruct.aa[i] = Btable.aa[i];}
for (i=0; i<16; i++) { Bstruct.bb[i] = Btable.bb[i];}
...
for (i=0; i<16; i++) { Bstruct.kk[i] = Btable.kk[i];}
This is a embedded program running on a MCU.
I wonder what is the benefit for the usage of the for loop like above.
|
|
|
|
|
In this particular case I don't see a real benefit for seperating the for loops, perhaps the author thought it to be easier to read? However, if there comes a time when Bstruct.aa[]/Btable.aa[] has a different number of entries than Bstruct.bb[]/Btable.bb[] then this would be easier IMO to maintain.
|
|
|
|
|
All Bstruct.aa[],bb[] ... kk[] have the same size 16.
Is there some cons for using seperate for loops? the compiler maybe optimized them in one loop in assembly?
|
|
|
|
|
econy wrote: All Bstruct.aa[],bb[] ... kk[] have the same size 16
Yes, today they do, tomorrow, who knows?
econy wrote: Is there some cons for using seperate for loops?
Using more CPU cycles because of the multiple initializations and testings (some MCUs clear thier cache when branches are executed) of i. Other than that I can't think of any.
econy wrote: the compiler maybe optimized them in one loop in assembly?
Don't know offhand, my gut tells me they wouldn't be optimized out.
|
|
|
|
|
|
This would depend to some extent on the target hardware and the program design. The only way to be certain would be to ask the original writer.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
No benefit, in my opinion.
Veni, vidi, vici.
|
|
|
|