|
waldermort wrote: IsKindOf() is MFC only, which unfortunatly I am not using.
What about typeid() ?
waldermort wrote: I usually always assign NULL to a pointer both on creation and deletion, but in this case it's not possible. I must test the pointer somehow before trying to use it.
Can you modify the CImage class?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
type_info and typeof both return char pointer to the type name associated with the pointer, they do not perform any pointer validation. Unfortunatly it looks like I am going to have to rethink this. Yes, I can modify the CImage class.
CImage is a base class which holds and draws a series of DIB sections. My basic idea is to give the class the ability to draw the image contained within another instance of the class (a little like swapping the this pointer). Since the drawing functions will be called often, I would prefer to store the pointer, that way I only need to change the image once rather than passing a pointer for each bitblt call.
|
|
|
|
|
The pointer is valid, the object it is pointing to is not. What you can do is add a static method to cImage to check the status of a pointer you pass to it to see if it is a valid object of that type.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
waldermort wrote: Which results in the value of tmp being 0xFFFFFFFF...
Of course it does. Why would it be anything else? It should retain that value until it is assigned something different.
waldermort wrote: ...whereas I want it to bu NULL.
Then you can't call SwapImage() with a non-NULL value.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: Of course it does. Why would it be anything else? It should retain that value until it is assigned something different.
The whole point of my question is to find out how to correctly validate that a pointer is poining to a certain type, NOT to find out how large your ego is! If I knew it would give me those results I would not have used it in the first place now would I? I am testing for NULL because the docs say that if dynamic_cast fails it returns NULL and throws an error, it seems logical to me that if the return value is not NULL then the provided pointer is valid.
|
|
|
|
|
I think you are over complexifying things.
unless don't know where m_ImagePtr some from or where the input to SwapImage come from, you don't need to test the validity of the pointer except for NULL.
waldermort wrote: CImage::SwapImage((CImage*)0xffffffff);
This is weird coding; I assume 0xffffffff is here only to show what your are trying to explain ?
me think that if you don't know where and how your data come from, you'd better review your whole system.
bool CImage::SwapImage( CImage *cImage )
This should only receive a CImage object and it should only check for NULL.
CImage *tmp = dynamic_cast<CImage*>(m_ImagePtr);
if tmp is ever NULL when m_ImagePtr is not then you better check your code; because it shows that you are assigning untyped data to something that needs to be types, either by using a C cast or by directly casting an adress.
|
|
|
|
|
the 0xFFFFFFFF is there just to represent an arbitary pointer that is not of the CImage type. I understand what you are saying. The CImage class is part of a library that I may use again and again, I want to put this test in place for two reasons
1. To detect hanging pointers. In the case a CImage class was instantiated using new , passed in then delete d.
2. To prevent typecasting of other data types being passed in, which would again throw an error.
I think I am going to have to do this through smart pointers and reference counting.
|
|
|
|
|
There is no way to what you are trying to do. dynamic_cast is used to upcast or downcast through base classes and derived classes, and makes sure that the cast is valid, not checking whether the pointer is a valid address. Look it up in MSDN, there is a very good list of examples there.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Yeah, I kinda learnt that the hard way tonight
But your post just gave me an idea. I may be able to derive CImage from an empty base class and type cast to that. I will give it a try now.
-- modified at 15:31 Thursday 2nd November, 2006
That didn't work either, the address returned is simply an offset of the origional. Oh well, back to reference counting.
|
|
|
|
|
Garbage in, garbage out. You have to start with a valid C++ object for dynamic_cast to work.
|
|
|
|
|
I agree. But there is no way to detect garbage. It wouldn't be so bad if I could throw an error when trying to dereference the pointer, but I have found no way to detect it before the OS does.
|
|
|
|
|
waldermort wrote: But there is no way to detect garbage.
you are the one who do the coding, you have to make sure there is no garbage that is introduced to the system.
If there are places in your code that can generate garbage, fix them.
in your example, the CImage class must be instantiated somewhere, if you cannot validate the construction of your own object, you have a problem with your design.
remember that C++ is quite "type" safe; and with the compiler warnings set to level 4 you should be able to resolve all of these without having to do dark magick incantations on your pointers.
and try to never do C type casting on C++ objects, use static_cast<CImage*>(pointer) instead of (CImage*)pointer
|
|
|
|
|
Maximilien wrote: you are the one who do the coding, you have to make sure there is no garbage that is introduced to the system.
I am the one coding this library, that is true, but I am not the onw who will coding the final product. This project is a re-usable library which I and other developers may use, therefore I want to prevent/detect garbage from being entered into my code.
|
|
|
|
|
You can wrap the dereference in a SEH block, that will let you handle the access violation if it occurs.
|
|
|
|
|
waldermort wrote: I'm passing a pointer to an object into a function where I would like to validate it.
If by "validate it" you mean make sure it is pointing to a valid object, there is no way you can do it with C++.
Can you use a reference-counted smart pointer instead of a "raw" one?
|
|
|
|
|
HI,
I want to use listview control using win32 API in my dialog box. I tried to fill this listview during WM_INITDIALOG message but I can’t see anything in the listview. Can I know, what is the problem with my code?
case WM_INITDIALOG:
{
InitCommonControls();
HWND hList = NULL;
hList = ::GetDlgItem(hDlg,IDC_EVENT_LIST);
LVCOLUMN lvCol;
LVITEM lvItem;
memset(&lvCol,0,sizeof(lvCol));
lvCol.mask=LVCF_TEXT|LVCF_SUBITEM;
lvCol.pszText="Item";
SendMessage(hList,LVM_INSERTCOLUMN,0,(LPARAM)&lvCol);
memset(&lvItem, 0, sizeof(lvItem));
lvItem.mask=LVIF_TEXT;
lvItem.cchTextMax = 256;
lvItem.iItem=0;
lvItem.iSubItem=0;
lvItem.pszText="Item 0";
SendMessage(hList,LVM_INSERTITEM,0,(LPARAM)&lvItem);
return TRUE;
}
Thanks in advance
|
|
|
|
|
priyank_ldce wrote: ...but I can’t see anything in the listview.
Does this mean you can't see columns, items, or both?
The code you've shown looks fine.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I can't see anything - no column, no item
|
|
|
|
|
The only other thing I can suggest is to check the return values from GetDlgItem() and SendMessage() . Also make sure that the IDC_EVENT_LIST control has the LVS_REPORT style.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
DavidCrow wrote: Also make sure that the IDC_EVENT_LIST control has the LVS_REPORT style.
Well if the Insert works you should still see the item regardless of the View Style
led mike
|
|
|
|
|
led mike wrote: Well if the Insert works you should still see the item regardless of the View Style
True, I was just dotting all the Is and crossing all the Ts. Sometimes it helps just to get the obvious things out of the way, rather than chasing rabbits unnecessarily.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Are any of those SendMessage() calls failing? Check your return values.
You probably just need to set a non-zero width for that column.
|
|
|
|
|
This worked in VS 2003, but I cant seem to understand what is going wrong here. I am trying to specify all the fields in a message, 1 bit 14 bits etc. But the size is wrong
This structure should have a size of 20 bytes, but during debugging its 22 bytes with I ceck its length, Why?
#pragma pack(1)
typedef struct{
unsigned long a1_t04_word_0_reserved_133_0 : 1;
unsigned long a1_t04_word_0_spare_134_1 : 1;
unsigned long a1_t04_word_0_mrk_typ_status : 1;
unsigned long a1_t04_word_0_sq_g_status : 1;
unsigned long a1_t04_word_0_sq_lev_g_status : 4;
unsigned long a1_t04_word_0_vol_grx_status : 4;
unsigned long a1_t04_word_0_marker_vol_status : 4;
unsigned long a1_t04_word_1_vol_max_status : 4;
unsigned long a1_t04_word_1_sidvol_status : 4;
unsigned long a1_t04_word_1_clp_status : 2;
unsigned long a1_t04_word_1_saturn_relay_enable_status : 1;
unsigned long a1_t04_word_1_agc_status : 1;
unsigned long a1_t04_word_1_sq_lev_m_status : 4;
unsigned long a1_t04_word_2_mod_depth_status : 4;
unsigned long a1_t04_word_2_srq_cfg_status : 2;
unsigned long a1_t04_word_2_reserved_144_5 : 10;
unsigned short a1_t04_word_3_reserved_148_0;
unsigned long a1_t04_word_4_reserved_152_0 : 6;
unsigned long a1_t04_word_4_af_in_lv_status : 6;
unsigned long a1_t04_word_4_pset_sel_status : 4;
unsigned long a1_t04_word_5_err_vol_status : 4;
unsigned long a1_t04_word_5_spare_156_4 : 1;
unsigned long a1_t04_word_5_err_beep_status : 3;
unsigned long a1_t04_word_5_reserved_154_7 : 8;
unsigned long a1_t04_word_6_reserved_159_0 : 8;
unsigned long a1_t04_word_6_reserved_160_8 : 8;
unsigned long a1_t04_word_7_hold_time_status : 4;
unsigned long a1_t04_word_7_scan_time_status : 4;
unsigned long a1_t04_word_7_reserved_163_8 : 1;
unsigned long a1_t04_word_7_7_bit_pad_162_9 : 7;
unsigned long a1_t04_word_8_spare_170_0 : 10;
unsigned long a1_t04_word_8_reserved_169_10 : 4;
unsigned long a1_t04_word_8_time_master_slave_status : 2;
unsigned long a1_t04_word_9_spare_176_0 : 12;
unsigned long a1_t04_word_9_reserved_175_12 : 1;
unsigned long a1_t04_word_9_sat_ext_pre_status : 1;
unsigned long a1_t04_word_9_sat_prem_ptt_status : 1;
unsigned long a1_t04_word_9_sat_atec_trsp_status : 1;
}a1_VUHF1_T04;
#pragma pack(0)
|
|
|
|
|
I think you are confusing bit and byte
|
|
|
|
|
I am trying to define a structure such that its not padded out to the nearest byte.
I did think that the directive #pragma pack(1) meant that across a 32 bit (4 byte) boundry all the bits are used.
e.g.
#pragma pack(1)
typedef struct{
unsigned long a1_t12_word_0_block_label : 12;
unsigned long a1_t12_word_0_spare_825_12 : 2;
unsigned long a1_t12_word_0_hset_ld_succ : 2;
unsigned long a1_t12_word_1_hqi_a_net : 8;
unsigned long a1_t12_word_1_hqi_b_net : 8;
unsigned long a1_t12_word_2_hqii_no : 8;
unsigned long a1_t12_word_2_hqii_non_no : 8;
unsigned long a1_t12_word_3_hqii_fmt1 : 8;
unsigned long a1_t12_word_3_hqii_fmt2 : 8;
unsigned long a1_t12_word_4_hqii_fmt3 : 8;
unsigned long a1_t12_word_4_hqii_fmt4 : 8;
unsigned long a1_t12_word_5_hqii_fmt5 : 8;
unsigned long a1_t12_word_5_hqii_fmt6 : 8;
unsigned long a1_t12_word_6_urn_no : 8;
unsigned long a1_t12_word_6_urn_non_no : 8;
unsigned long a1_t12_word_7_urn_t40_no : 8;
unsigned long a1_t12_word_7_urn_t40_non_no : 8;
unsigned long a1_t12_word_8_hqii_fmt7 : 8;
unsigned long a1_t12_word_8_hqii_fmt8 : 8;
}a1_VUHF1_T12;
The size of this structure turns out to be 20 bytes. It should be 18 bytes, how can I get it to be 18 bytes?
|
|
|
|