|
So my buffer will be displayed LSB first in the memory window, if I print it out, it should show how I want it to be right?
But when I try to do
for (int i=0, i< 100; i++)
printf ("%X", msgBuffer[i]);
I keep getting memory access errors.
If I cant look in the memory window or print it out, how can I verify my buffer. Any ideas?
-C
|
|
|
|
|
msgBuffer = new char [100] ;
Sets msgBuffer to the address of a an array of 100 characters. Fine.
memset (&msgBuffer,0, 100);
Overwrites the value of msgBuffer from the address of the array to 0, and then overwrites another 96 bytes with 0. Very wrong.
After that almost everything will fail sooner or later.
If you display in 'bytes' then the memory window will show a value of 0x04030201 as
01 02 03 04
IOW LSB first.
Paul
|
|
|
|
|
why are you bothering to allocate a new msgBuffer ?
Why not use a stack variable for this ?
or a declared member variable ?
__________________________________________
a two cent stamp short of going postal.
|
|
|
|
|
Regardless if I use a stack variable or a member variable, I have to create it, initialize it, and copy my data into it. I'm at the beginning stages of writing a device driver sending msgs back/forth serially. So I'd like to verify now, early in the development, that I"m creating my buffers correctly.
I've realized that I need to byte swap my shorts and longs so that the buffer is properly sent.
Thanks for all the assistance and corrections.
-C
|
|
|
|
|
Hello. When I write something like:
---
class test{
public:
CBrush f;
};
int main()
{
test t;
vector<test> hej;
hej.push_back(t);
}
---
I get an error in <vector>: ...\include\vector(575): error C2440: 'initializing' : cannot convert from 'const test' to 'test'
But if I replace CBrush with one of my own classes, i everything works fine! What is it with those MFC classes?
|
|
|
|
|
I would say the copy constructor
anyway its better to store a pointer to your class
try:
vector<test*> hej;
hej.push_back(&t);
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
How smart is it to store a pointer if the object you added to the vector goes out of scope?
I know that isn't the case right here, but generally speaking...
|
|
|
|
|
if it will goes out of scope just new it first
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Why did you want to use a pointer in the first place? What is the advantage?
|
|
|
|
|
For various reasons:
Parameter passing to a function of objects is always better by pointer or by reference, the copy time is smaller and faster.
and the allocator of your vector is faster too because you wouldn't reallocate data or allocate chunks often.
and much more too like copy modifications ...
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
But i'm not looking for speed-improvements. I just want to know why the code i posted doesn't work. MUST I use pointers to make it work ?
|
|
|
|
|
Using pointer will settle this for you.
if you dont want to having a copy constructor will do
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
pie wrote:
How smart is it to store a pointer if the object you added to the vector goes out of scope?
If the object was allocated on the heap, it won't go out of scope per se. It will still exist until delete is called. In other words:
void MyClass::SomeFunction( void )
{
SomePtr *ptr = new SomePtr;
m_vector.Add(ptr);
} Even though ptr goes out of scope, the memory that it points to is still alive and well.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Do you have all the necessary #include s in place?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
I have:
#include <afxwin.h>
#include <vector>
using namespace std;
|
|
|
|
|
No compiler errors with this:
class test
{
public:
CBrush f;
test();
test(const test& t);
~test();
const test& operator=(const test& t);
};
void main( void )
{
test t;
vector<test> hej;
hej.push_back(t);
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
Yeah, that seems to work.. Thank you(and Navin)
|
|
|
|
|
You probably need to define a copy constructor for your class.
E.g.,
class test
{
public:
test(const test &src);
CBrush f;
};
test::test(const test &src)
:f(src.f)
{
}
Then try it and see what happens. (Note, just a guess, I didn't actually try to compile this.)
Sometimes I feel like I'm a USB printer in a parallel universe.
|
|
|
|
|
Objects that can be stored in std:: containers must be copyable, and copies must be equivalent. This isn't the case with CBrush. Even if you arrange a copy constructor such that the object being copied to takes ownership,
test ( const test& t)
{
f.Attach ( t.f.Detach ()) ;
}
you haven't met the 'equivalent' condition since the new object owns the Windows Brush and the old one no longer does. In your example if 't' was initialised with a valid brush after the push_back would no longer own it.
The copy constructor above won't compile because the argument 't' is const and we're trying to modify it with 'Detach', sound familiar?
In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually.
Paul
|
|
|
|
|
Thank you, Paul, that was a good answer.
|
|
|
|
|
Paul Ranson wrote:
In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually.
Or even better, to use some smart pointers, like boost::shared_ptr or Loki::SmartPtr
|
|
|
|
|
Hi all,
I'm having what I consider to be an inexplicable problem printing in my program. I've searched message boards, google, etc.. for the past week and no dice. I have two different "reports" that are displayed in the main window of my SDI app and should be printable. All of the drawing code is contained in OnDraw(), and the program draws the appropriate report based on flags set by button presses. Both reports display fine in Print Preview, but only one will actually print. When i print the other, the printer just spits out a blank page. I've replaced the drawing code in the function to simply display a rectangle in the center of the view for testing, but it still displays fine in print preview, but wont print. The report that is displayed by default is the one that prints okay, so i'm wondering if I need to re-initialize the CDC between prints or something along those lines. Thanks for reading this far, and thanks for any help you can provide.
|
|
|
|
|
Acetate wrote:
i'm wondering if I need to re-initialize the CDC between prints...
Printing the non-working report first will tell you if it's an initialization issue or not.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
True true. Just tried that, and sure enough, its still shooting out a blank page. Thanks for responding.
|
|
|
|
|
I'm sorry I could not be of more help. A lot of times, knowing what the problem is not is just as important as knowing what the problem is.
Thomas Edison said something to the effect of "I have not failed. I've just found 10,000 ways that won't work."
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|