|
Your code has another problem: if you build the array inside the function, as local storage, you cannot return it.
If you're stuck with the above (IMHO bad) design, you may try with a template function, for instance
#include <stdio.h>
float alfa[10]={0,1,2,3,4,5,6,7,8,9};
template <int N>
float * diff(float a[N])
{
static float b[N-1];
for (int i=0; i<N-1; i++)
{
b[i] = a[i+1]- a[i];
}
return b;
}
void main()
{
float * beta = diff<10>(alfa);
for (int i=0; i<9; i++)
{
printf("beta[%d]=%f\n", i, beta[i]);
}
}
Please note: the static allocation of b is just a workaround, there are better ways to do it (for instance, the caller may provide storage for b ).
Moreover, personally, I prefer dynamic memory allocation for such a task.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
LostPitch wrote: I know that allocation for arrays is static
Only when they're allocated on the stack.
What's wrong with allocating the array on the heap:
float* diff(float* inArr, size_t inArrSize)
{
float* outArr = new float[inArrSize-1];
for(size_t i=0;i<inarrsize-1;++i)>
{
outArr[i] = inArr[i+1]-inArr[i];
}
return outArr;
}
Yes, you need to deallocate it after it's been used with delete[] , but hey - it works!
Also - what's with the use of float ? Doubles have more bits and are also faster, as floating point hardware is double or bigger internally and has performance penalties for converting to and from single precision floating point data.
LostPitch wrote: I would like to avoid to use vectors
Again - why? vectors (certainly after the TR1 update to the C++ standard) are guaranteed to be as efficient as arrays. They also do all the memory management for you. What's not to like?
|
|
|
|
|
Stuart Dootson wrote: What's wrong with allocating the array on the heap
That you should at least check for the allocation result.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Why? I use a modern C++ system that throws when new fails
|
|
|
|
|
OMG another ot those terrible over-the-top-youngster-developers!!!!!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: youngster
I wish...
I first programmed in C on my ZX Spectrum[^] around 1984. Can you imagine compiling in C (think headers...) when your only storage is a tape deck?
|
|
|
|
|
Stuart Dootson wrote: I first programmed in C on my ZX Spectrum[^] around 1984.
That time i was only few months old
BTW: THHB in action
Regards,
Sandip.
|
|
|
|
|
Stuart Dootson wrote: I first programmed in C on my ZX Spectrum[^] around 1984.
I did the same.
Stuart Dootson wrote: Can you imagine compiling in C (think headers...) when your only storage is a tape deck?
C ? I started with BASIC , but soon passed to Z80 assembly !
BTW: as told in advance by the (I would say 'good-old' but he's really youngster...) SandipG, welcome in the THHB [^], [^]!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks Pallini and Stuart,
For the first solution I already knew the static trick but not the template one
For the second..actually I had already used it developing a vst long time ago but I forgot
I used the float since in vst 2.4 the processReplacing function manages float data and it's mandatory to implement it..anyway in the end I followed your advise and used vectors!
Thanks again!!
|
|
|
|
|
Hello all,
I am new to MFC. I know how to create dialogbox by adding the new resource into the resource file. But i want to create DialogBox at runtime without adding it into the Resource.rc file. After creating dialogbox, i have to add Edit control, List Box over that dialog box.
How can i do this?
Thanks in advance.
|
|
|
|
|
Check this[^]
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
|
|
|
|
|
I would also recommend reading this post:
http://blogs.msdn.com/oldnewthing/archive/2005/04/29/412577.aspx[^]
and some previous ones.
Heck, I recommend reading every single one of his blog posts - you'll learn about lots of things you should and should not be doing.
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Thanks for ur reply and suggesting me the link.
I read this post, but it is hard to understand for me, because i am new to MFC.
Please suggest the simplest way with example for creating dialog box without adding dialog resource into the resource.rc file.
|
|
|
|
|
Abhijit D. Babar wrote: because i am new to MFC
This is allllll windows stuff - MFC hardly comes in at all.
Abhijit D. Babar wrote: Please suggest the simplest way
There is no simple way - that's one of the reasons we use templates.
Abhijit D. Babar wrote: with example
Read a few months of the blog before the article I point you at - he builds up from very small examples to large complex ones. I could not write a better one - especially here on the fly.
Good luck,
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
|
hi all,
I am trying to convert from System::String to char *. After searching some forums and the msdn, i got these lines of code
const char * convertStrToChar(System::String ^ str)
{
CString tmpStr (str);
char* ConvToChar = new char[tmpStr.GetLength() + 2];
size_t Converted = 0;
wcstombs_s(&Converted, ConvToChar, tmpStr.GetLength()+1, tmpStr, _TRUNCATE);
return ConvToChar;
}
However, when i compilie it, these error messages appear :
Error 1 error LNK2028: unresolved token (0A00023F) "public: char * __clrcall Process::convertStrToChar(class System::String ^)" (?convertStrToChar@Process@@$$FQAMPADP$AAVString@System@@@Z) referenced in function "public: void __clrcall Process::showFaceDetect(class System::String ^,class System::String ^)" (?showFaceDetect@Process@@$$FQAMXP$AAVString@System@@0@Z) Process.obj
Error 2 error LNK2019: unresolved external symbol "public: char * __clrcall Process::convertStrToChar(class System::String ^)" (?convertStrToChar@Process@@$$FQAMPADP$AAVString@System@@@Z) referenced in function "public: void __clrcall Process::showFaceDetect(class System::String ^,class System::String ^)" (?showFaceDetect@Process@@$$FQAMXP$AAVString@System@@0@Z) Process.obj
I know this is a simple problem, but i cannot figure out why this bug happens. Can you give me some suggestion? Thanks
|
|
|
|
|
It looks to me (from the linker error) like the function signature you declared was:
char * convertStrToChar(System::String ^)
but you've actually implemented this function signature:
<big>const</big> char * convertStrToChar(System::String ^)
|
|
|
|
|
this is not C++, it is C++/CLI.
so it belongs to the Managed C++ forum
|
|
|
|
|
|
thank all. i just find out the solution. It 's only a stupid mistake
to toxcct: sorry. Will post in the right place next time
|
|
|
|
|
Hi,
I'm working in VS2005, C++. I'm pretty much a C++ newbie. I tend to write a lot of little console programs while i'm learning and testing things out. But of course the console disappears once the program is finished. it would be handy to be able to see the console output after the fact in the ide, and it seems to me there must be a way to do that - i just don't know it yet. any ideas?
Thanks,
Warre
Warren Sirota
www.warrensirota.com
|
|
|
|
|
Try any of the following suggestions
1. Put a getch() at the end of the program so that console application will wait for a key press before it exists.
2. Redirect the output to a text file. For eg in the project settings->debug->Program arguments, if you enter c:\output.txt, all the outputs from the program will go to the text file.
|
|
|
|
|
3: Get a Windows version of the "tee" utility so that the OP can get it both in a file and in the output. Helps with interactive apps or while debugging.
Peace!
-=- James Please rate this message - let me know if I helped or not!<hr></hr> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
I have a big problem porting one of my apps to VS 2005:
on one of the templates I get
"error C2906: 'const AFX_MSGMAP *CModWin<dialog>::GetThisMessageMap(void)': Explizite Spezialisierung erfordert 'Vorlage <>'" in the line BEGIN_MESSAGE_MAP(CModWin<cdialog>, CDialog)</cdialog>
the*.cpp looks like this
BEGIN_MESSAGE_MAP(CModWin<cdialog>, CDialog)
ON_COMMAND(IDOK, OnOK)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CModWin<cresizabledialog>, CResizableDialog)
ON_COMMAND(IDOK, OnOK)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CModSht<cpropertysheet>, CPropertySheet)
ON_COMMAND(IDOK, OnOK)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CModSht<cresizablesheet>, CResizableSheet)
ON_COMMAND(IDOK, OnOK)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CModSht<clistviewwalkerpreferencesheet>, CListViewWalkerPreferenceSheet)
ON_COMMAND(IDOK, OnOK)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CModSht<clistviewwalkerpropertysheet>, CListViewWalkerPropertySheet)
ON_COMMAND(IDOK, OnOK)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CModWlkSht<clistviewwalkerpreferencesheet>, CListViewWalkerPreferenceSheet)
ON_COMMAND(IDOK, OnOK)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CModWlkSht<clistviewwalkerpropertysheet>, CListViewWalkerPropertySheet)
ON_COMMAND(IDOK, OnOK)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
</clistviewwalkerpropertysheet></clistviewwalkerpreferencesheet></clistviewwalkerpropertysheet></clistviewwalkerpreferencesheet></cresizablesheet></cpropertysheet></cresizabledialog></cdialog>
the *.h like this
template<class window="">
class CModWnd : public WINDOW {
public:
CModWnd() {
m_bDeleteOnClose = TRUE;
m_bActive = FALSE;
}
void OpenDialog(UINT bDeleteOnClose = TRUE) {
m_bDeleteOnClose = bDeleteOnClose;
if (!m_bActive){
m_bActive = TRUE;
CallCreate();
}
ShowWindow(SW_SHOW);
SetFocus();
}
virtual void CloseDialog() {
if (m_bActive)
DestroyWindow();
}
bool IsDialogOpen() {
return (m_bActive == TRUE);
}
protected:
virtual void CallCreate() = 0;
virtual BOOL OnInitDialog(){
UINT old_nFlags = m_nFlags;
m_nFlags |= WF_CONTINUEMODAL;
BOOL bResult = WINDOW::OnInitDialog();
m_nFlags = old_nFlags;
return bResult;
}
afx_msg void OnOK() {
UpdateData();
DestroyWindow();
}
afx_msg void OnCancel() {
DestroyWindow();
}
afx_msg void PostNcDestroy() {
m_bActive = FALSE;
if (m_bDeleteOnClose)
delete this;
}
BOOL m_bActive;
BOOL m_bDeleteOnClose;
};
template<class dialog="">
class CModWin : public CModWnd<dialog> {
public:
CModWin(UINT nIDTemplate, CWnd* = NULL):CModWnd<dialog>(){
m_nIDTemplate = nIDTemplate;
}
protected:
virtual void CallCreate() {
Create(m_nIDTemplate, GetEmuleDlg());
}
DECLARE_MESSAGE_MAP()
UINT m_nIDTemplate;
};
template<class sheet="">
class CModSht : public CModWnd<sheet> {
public:
CModSht():CModWnd<sheet>() {}
protected:
virtual void CallCreate() {
Create(GetEmuleDlg(), WS_SYSMENU | WS_POPUP | WS_CAPTION | DS_MODALFRAME | DS_CONTEXTHELP | WS_VISIBLE | WS_MINIMIZEBOX);
}
virtual BOOL OnInitDialog(){
BOOL old_bModeless = m_bModeless;
m_bModeless = FALSE;
BOOL bResult = CModWnd<sheet>::OnInitDialog();
m_bModeless = old_bModeless;
return bResult;
}
afx_msg void OnOK() {
SendMessage(WM_COMMAND, ID_APPLY_NOW);
CModWnd<sheet>::OnOK();
}
DECLARE_MESSAGE_MAP()
};
template<class wlk_sheet="">
class CModWlkSht : public CModSht<wlk_sheet> {
public:
CModWlkSht(CListCtrlItemWalk* pListCtrl):CModSht<wlk_sheet>() {
m_pListCtrl = pListCtrl;
}
void DropControl() {
m_pListCtrl = NULL;
m_bDeleteOnClose = TRUE;
if(!IsDialogOpen())
delete this;
}
protected:
DECLARE_MESSAGE_MAP()
};
typedef CModWin<cdialog> CModDialog;
typedef CModWin<cresizabledialog> CModResizableDialog;
typedef CModSht<cpropertysheet> CModPropertySheet;
typedef CModSht<cresizablesheet> CModResizableSheet;
typedef CModWlkSht<clistviewwalkerpreferencesheet> CModListViewWalkerPreferenceSheet;
typedef CModWlkSht<clistviewwalkerpropertysheet> CModListViewWalkerPropertySheet;
</clistviewwalkerpropertysheet></clistviewwalkerpreferencesheet></cresizablesheet></cpropertysheet></cresizabledialog></cdialog></wlk_sheet></wlk_sheet></class></sheet></sheet></sheet></sheet></class></dialog></dialog></class></class>
the whole project is available on NeoMule.sf.net
Any ideas how I can fix this anoying problem?
|
|
|
|
|
It would help if you'd escaped your code so we could see the template parameters...
|
|
|
|