|
I recommend you use the std::vector class for this.
You can do the following -
std::vector<T> myArray;
myArray.reserve(stop - start + 1);
|
|
|
|
|
I think you meant vector::resize() .
---
IMO using the std::vector does not add any value in this case. In fact it's the other way around.
* It will allocate memory from the heap, whether you have an automatic instance or not. Possibly affecting performance and fragmentation. (Not a problem for most application, but still)
* There is no added bounds checking. It is just as good (or bad) as the native array.
* Memory management is provided for you in both cases.
Use of std::vector might be useful if:
* You work with years spanning over millions of years. Otherwise it might consume your stack in no-time.
* Maybe the compiler/linker can optimize your class due to the fact that all instances (with the same type parameter) have the same size. (sizeof(std::vector<int>) is not affected by the number of elements while sizeof(int[N]) is.) Unfortunately, my knowledge of linker optimization techniques is not what it should be so I really don't know. But I wouldn't mind knowing.
|
|
|
|
|
template <class T, int start, int stop>
class MyArray {
T test [stop-start+1];
public:
MyArray()
{
for (int i = 0; i < (stop - start + 1); ++i)
test[i] = start + i;
}
};
MyArray<int, 1999, 2010> arr;
|
|
|
|
|
Here's a quick counter question... Do you actually need an array?
The reason this springs to mind is that you've made the array private so no one outside of your interface can use it. Well, apart from using some disgusting tricks that you should burn in a personal hell for.
So if your final class interface is going to be something like:
template <class T, int first, int last>
class year_range
{
public:
T nth_year_in_range( int n );
};
you could actually eliminate the array wholesale:
T nth_year_in_range( int n )
{
return T( start + n );
}
Of course if you need the array for something else then this won't work.
Cheers,
Ash
|
|
|
|
|
matrix operator=(const matrix &that) {
matrix ret;
ret.resize(this->data.size(), this->data[0].size());
for (int i = 0; i < that.data.size(); i++)
for (int j; j > that.data[0].size(); j++)
ret[i][j] = that[i][j];
return ret;
};
Seems to be no good, what am I doing wrong
http://www.contract-developer.tk
|
|
|
|
|
are you getting an error message of any kind ?
|
|
|
|
|
Claims there are no operators available, to support the abstract vector algebra.
Error 2 error C2679: binary '-' : no operator found which takes a right-hand operand of type 'aa::vector<base>' (or there is no acceptable conversion) matrix.h 114 1 fibonacci
http://www.contract-developer.tk
|
|
|
|
|
vector operator-(const vector *that) { // parallelogram law
vector ret;
assert(this->data.size() == that.data.size());
ret.resize(this->data.size());
#pragma omp parallel for
for (int i = 0; i < this->data.size(); i++)
ret.data[i] = this->data[i] - that.data[i];
return ret;
};
http://www.contract-developer.tk
|
|
|
|
|
Without knowing what your representation of matrix is it's a bit hard to work out if you're copying it correctly.
Cheers,
Ash
PS: Having said that what you've implemented is going to be more of a factory or clone method. While it might be legal C++ it wouldn't half surprise anyone trying to use the class. Would you expect that writing a = b; wouldn't change the value of a?
|
|
|
|
|
Should be:
matrix operator=(const matrix &that) {
for (int i = 0; i < this->rows; i++)
for (int j=0; j < this->cols; j++)
(*this)[i][j] = that[i][j];
return *this;
}
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]
|
|
|
|
|
I am trying to load a picture from memory
from inspection can you see why this dosen't work?
here is part of the class I am using:
BOOL CPicture::Load(IStream* pstm)
{
Free();
HRESULT hr = OleLoadPicture(pstm, 0, FALSE,
IID_IPicture, (void**)&m_spIPicture);
return hr == S_OK;
}
here is some code I have found to implement this:
ifstream file ("c:\\1.jpg", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
IStream *pStrm = NULL;
HGLOBAL hgbl = GlobalAlloc (0, 852);
CreateStreamOnHGlobal (hgbl, true, &pStrm);
pStrm->Write(memblock, size, 0);
delete [] memblock;
m_pict.Load(pStrm);
}
Thabk you your help will be much appreciated Simon
|
|
|
|
|
simon alec smith wrote: from inspection can you see why this dosen't work?
Not really, unless you explain what you mean by "doesn't work"; such a statement does not help in understanding what your problem is. Does your program crash, throw an exception, paint the image upside down, etc.?
It's time for a new signature.
|
|
|
|
|
This is what was reported,
File c:\program files\microsoft visual studio\vc98\atl\include\atlbase.h
line 622
expression: p!= 0
|
|
|
|
|
simon alec smith wrote: expression: p!= 0
And how does this relate to your code, where does the stack history point to in your program?
It's time for a new signature.
|
|
|
|
|
This does not work as expected, for matrix * matrix
matrix operator*(const matrix that) { // matrix product AB != BA
matrix ret;
ret.resize(this->data.size(), that.data.size());
for (int i = 0; i < this->data.size(); i++) {
for (int j = 0; j < this.data[i].size(); j++) {
for (int k = 0; k < this->data.size(); k++) {
#pragma omp parallel for
for (int l = 0; l < this.data[i].size(); l++) {
ret[i][k] += this->data[i][k] * that.data[j][k];
}
}
}
}
return ret;
http://www.contract-developer.tk
|
|
|
|
|
So what exactly is the question? Have you used the debugger to step through the code? Have you removed the #pragma directive? Have you worked out on paper how to do the multiplication?
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
The result from a test != book
http://www.contract-developer.tk
|
|
|
|
|
VeganFanatic wrote: for (int i = 0; i < this->data.size(); i++) {
for (int j = 0; j < this.data[i].size(); j++) {
for (int k = 0; k < this->data.size(); k++) {
#pragma omp parallel for
for (int l = 0; l < this.data[i].size(); l++) {
ret[i][k] += this->data[i][k] * that.data[j][k];
}
}
}
}
AFAIK it should be
for (int i = 0; i < this->rows; i++)
for (int j = 0; j < that->cols; j++)
{
ret[i][j] = .0;
for (int k = 0; k < this->cols; k++)
ret[i][j] += this->data[i][k] * that.data[k][j];
}
Why do you need the further loop?
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]
|
|
|
|
|
I need to parse a .dat file which contains hexadecimal data. I need to parse the contents and store it as a text file.
I have the .dat file and the header file. Can anyone tell me how should I start with it or what are the steps involved.
|
|
|
|
|
Depends on what the hex represents... is it ASCII? is it archived? Is there a certain algorithm encoding it?
Josh Davis
Always looking for blackjack. Or maybe White Frank. One of the two.
|
|
|
|
|
the pseudo-code would be:
1. open the input file
2. create the output file
3. for each line in input file: read it, process it, write results to output file
4. close output file
5. close input file
6. terminate
However your description sucks a little, there is no such thing as hexadecimal data: all files contain either text or they don't; and every byte can be displayed as ASCII, Unicode, or be formatted in binary, octal, decimal, hexadecimal.
So maybe what you mean is: I have a text file containing a hexadecimal representation of some data, but then what would be the conversion you need, as it already is text?
So try to clarify; and to confirm your description, add a little example (input line, matching output line).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
The .dat file contains few information as
path of a file,filename,date of creation of file etc. These information are stored as hex in the .dat file.
Depending upon the header file I need to extract these data and store it in a text file as normal text.
|
|
|
|
|
To further expand on Luc's answer from point 3 of "process it", you can use strtok_s[^] to extract hex characters from each line.
|
|
|
|
|
I have a skinned dialog that I am attempting to place transparent buttons on; based on CButton. However, when I do a create I find that a blank space is created where my button should be. Therefore the background that shows through my transparent image is white rather than the skinned bitmap that should appear. I imagine that the reason this area is not painted is because that area is to be painted by the button.
Is there a way to get that area of the background painted despite the fact that the button is to be painted there?
Thanks in advance.
|
|
|
|
|
(I assume that the buttons you are creating are subclassed CButton .) The white spaces is created by someone somewhere. Did you check your handling of WM_ERASEBKGND ?
|
|
|
|