|
ForNow wrote: The run time has it own heap I think the default heap
Modern OSes generally have at least two allocators. So in C++ the OS provides space to the app and then C++ itself manages that heap with its own allocator. Same is true for Java and C#.
ForNow wrote: When I create my heap I’m segragting the allocations
Not sure that what that means.
An heap already represents a segregation. The C++ heap (every one I have looked at going back decades) dynamically manages blocks that it requested from the OS. Then on top of that it lays down a simple (or even complex) traditional heap. Then when you use 'new' (presuming you do not otherwise provide an allocator) it uses that existing API to request an amount of space appropriate for usage. Generally the allocation is exact to the structure.
The structure might have padding but that is intrinsic to the structure and not the allocation.
|
|
|
|
|
New works well when you now Exactly howvmuch storage you want I.e a object or class
In my case I am capturing user input and not quite sure thus HeapReAlloc
|
|
|
|
|
ForNow wrote: New works well when you now Exactly howvmuch storage you want I.e a object or class
That is not true.
For an object in C++ you must ALWAYS allocate at least as much space is needed for the object. You can allocate more but it will be wasted.
One can force C++ to use space that is less but that would be nothing but bad programming.
ForNow wrote: In my case I am capturing user input and not quite sure thus HeapReAlloc
All that means is that you are dynamically allocating the space. Which is something that C++ allows directly. You do not need to use a system API method to do that.
As I previously pointed out HeapReAlloc has a specific purpose. And what you just described here is an inappropriate use of that. And one that could lead to resource (memory) starvation if you are not careful how you use it.
|
|
|
|
|
My Addstring works fine its in OinitDialog
except when I make it ownerdraw then I get an exception however even out it I mean Addstring
DrawItem never gets control
|
|
|
|
|
It would be helpful to know which kind of exception and where it occurs.
However, has your owner drawn list box also the LBS_HASSTRINGS style?
If not, that is probably the reason:
If the list box has an owner-drawn style but not the LBS_HASSTRINGS style, this parameter is stored as item data instead of a string. You can send the LB_GETITEMDATA and LB_SETITEMDATA messages to retrieve or modify the item data.
|
|
|
|
|
I have that "LBS_HASSSTRINGS" actually just stepping thru the code on ClistBox::AddString the VS debugger
comes up with a DialogBox "BreakPoint hit" a message indicating some sort of exception
I am wondering if I cannt do AddString in OnitDialog for a OwneDraw litbox ?
|
|
|
|
|
Calling AddString() itself should not raise an exception (provided that the list box window exists) because that just sends the LB_ADDSTRING message. I guess it is happening later when other list box operations are performed.
|
|
|
|
|
I made a custom DDX to create the list box
DDX_TextVAL(pDX, IDC_STORAGE_AREAS, storage_area);
HWND tempwin = ::GetDlgItem(pDX->m_pDlgWnd->m_hWnd, nIDC);
LPTSTR temptr = (LPTSTR)new char[10];
::GetClassName(tempwin, temptr, 10);
if (strcmp((char *)temptr, "Edit") == 0)
value.Attach(pDX->PrepareEditCtrl(nIDC));
else
value.Attach(pDX->PrepareCtrl(nIDC));
delete temptr;
CListBox storage_area
|
|
|
|
|
ForNow wrote: I made a custom DDX to create the list box
And what for?
IMHO, it is not a good idea...
|
|
|
|
|
|
When I step thru the debugger on ClistBox::AddString it (VS debugger) just says breakpoint hit seems like for OwnerDraw I cann't do addstring in OinitDialog
Notsure
|
|
|
|
|
With that little information it's anyone's guess what is going on. I have used AddString in the OnInitDialog method, in normal and owner drawn list boxes.
|
|
|
|
|
Ok thanks that’s helpful I’ll do some digging
|
|
|
|
|
Consider the following code
"main.cpp"
#include <stdio.h>
#include "../include/ESSolver.h"
int mu;
int lambda ;
int main(void)
{
mu = 5;
lambda = 10;
ESSolver es;
es.Init();
es.Optimize();
return 0;
}
"ESSolver.h"
extern int mu;
extern int lambda ;
class ESSolver
{
public:
const static int nVar = 4;
ESSolver();
~ESSolver(void);
void Init();
void Optimize();
int rand_r(int, int);
private:
double (*muPop)[nVar];
double *muSigma;
double (*lambdaPop)[nVar];
double *lambdaSigma;
double (*mulambdaPop)[nVar];
double *mulambdaSigma;
};
"ESSolver.cpp"
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include "../include/ESSolver.h"
using namespace std;
ESSolver::ESSolver()
{
double _muPop [mu][nVar];
muPop = _muPop;
muSigma = new double[mu];
double _lambdaPop [lambda][nVar];
lambdaPop = _lambdaPop;
lambdaSigma = new double[lambda];
double _mulambdaPop [mu+lambda][nVar];
mulambdaPop = _mulambdaPop;
mulambdaSigma = new double[mu+lambda];
return;
}
ESSolver::~ESSolver(void)
{
if (muSigma) delete muSigma;
if (lambdaSigma) delete lambdaSigma;
if (mulambdaSigma) delete mulambdaSigma;
return;
}
void ESSolver::Init()
{
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
muPop[i][j] = 50;
}
}
cout << "\n----- muPop -----\n";
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
cout << muPop[i][j] << " ";
}
cout << endl;
}
}
void ESSolver::Optimize()
{
cout << "\n----- muPop -----\n";
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
cout << muPop[i][j] << " ";
}
cout << endl;
}
}
On running the following code I am getting Output
[^]
As you can see from the image, Matrix is not getting initialized. Can anybody tell me why.
modified 29-Jan-21 21:01pm.
|
|
|
|
|
Quote: double _muPop [mu][nVar];
muPop = _muPop;
_mPop is a temporary array (a local variable), your muPop pointer is goingo to point to garbage.
|
|
|
|
|
Thanks
modified 29-Jan-21 21:01pm.
|
|
|
|
|
|
How can we find the index of smallest 3 elements in an array. Below code finds the index of largest 3 elements in an array.
#include <vector>
#include <iostream>
using namespace std;
int main()
{
double arr[] = {0.2, 1.0, 0.01, 3.0, 0.002, -1.0, -20};
priority_queue < pair<double, int> > pQueue;
for (int i = 0; i < 7; i++)
{
pQueue.push(pair<double, int>(arr[i], i));
}
int k = 3;
for (int i = 0; i < k; ++i)
{
int ki = pQueue.top().second;
cout << ki << " ";
pQueue.pop();
}
}
modified 29-Jan-21 21:01pm.
|
|
|
|
|
See std::priority_queue - cppreference.com[^]:
Quote: A user-provided Compare can be supplied to change the ordering, e.g. using std::greater<t> would cause the smallest element to appear as the top(). Another solution would be inverting the sign of the items pushed into the queue:
pQueue.push(pair<double, int>(-arr[i], i));
|
|
|
|
|
Where should I put
std::greater<t>
in the code.
modified 29-Jan-21 21:01pm.
|
|
|
|
|
See the link from my answer. It contains example code showing that it must be passed as 3rd template parameter. So you have to pass also the 2nd parameter.
As with any templates, T (uppercase) is a placeholder for the corresponding type which is std::pair<double, int> in your case.
So it must be (untested):
priority_queue < pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>> > pQueue;
|
|
|
|
|
Thanks it worked
modified 29-Jan-21 21:01pm.
|
|
|
|
|
Hi,
I have a value float a=1234.5578
I want to print output as 1234.55.
How to approach this ?
|
|
|
|
|
Read up on printf formatting. Google will find you lots of tutorials and references.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Are you sure you want 1234.55 as output? The number would be rounded to 1234.56.
|
|
|
|