|
Since you are "porting C code to C++", why don't you replace malloc/free with the new/delete?
Just use C++ allocation operators/methods and you won't need every time cast the returned pointer to the correct type.
|
|
|
|
|
I was going to suggest the use of new, but since he is allocating a single area for two separate structures it would be more complicated.
|
|
|
|
|
I don't think it's two structs, it looks like it's N structs plus a 16 bit number, which maybe could contain the size. If so, it should be converted to a std::vector.
On a side note, sizeof(uint16_t) deserves a place in the coding horror forum
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
You are correct. However, given many of OP's previous questions I think it is a bit too soon to introduce him to the new operator.
|
|
|
|
|
In C++ it is more appropriate to use reinterpret_cast .
|
|
|
|
|
Please consider rewriting it in proper C++ .
I don't blame the usage of goto in C code, but C++ provides better alternatives.
|
|
|
|
|
It's not proper C code to start with, and I'm not even complaining about goto. Given the weird malloc statements it's pretty obvious there's some severe memory clusterfuck taking place. The structs and pointers being defined do not represent what is being stored. dl is being allocated twice, and dr not at all.
Given the the quality of the visible code, I strongly doubt that the code that uses these pointers is accessing the underlying memory correctly.
There's not a chance in hell to turn that into clean C++ if the original C code isn't cleaned up first.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
While I agree on your observations, I don't completely agree on your last sentence. If you are able to capture the actual requirements, you don't need to clean-up the original C , in order to produce neat C++ one.
|
|
|
|
|
This code looks very, very wrong. It shouldn't be possible to convert this into clean C++ code, because it's not clean C code to start with!
First of all, you have two pointers that require some allocation, but you have two allocations assigned to the same variable, dl. Why do you have two allocations for dl? Where is the allocation for dr?
Second, both dl and dr are declared as pointers to some kind of struct. In clean C code, the implicit meaning of this is that these pointers are pointing to one or more structs of their respective types.
Apparently, you are allocating data structures that contain arrays of data structures along with additional information, without ever declaring that memory structure as data structure. That is completely inaccaptable, unclean code.
You should start by defining your actual data structures in C and clean up your original code. Then you can define arrays as std::vector or std::array, to safe yourself the effort of managing containers, and use new/delete to allocate your actual structs if you do need to allocate them dynamically - chances are, that once you get the container memory auto-managed by C++ classes, you can simply declare those structs on the stack.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
First, I apologize that I erroneously posted TWO malloc calls.
It woudl have helped if I did not, since it generated unnecessary responses.
I was asking for help on malloc , not on whole snippet.
My apology.
I have "inherited" / using the original C code and have no control over how clean or stupid it is.
Since the original code is "very well documented" my intent is to get rid of the current "library" so I can figure out what the code does step by step.
So far I have part of hciconfig working thanks to the group help with the malloc issue.
Now I have small issue with "invalid pointer" which should go away after I clean up the "struct" assignments.
The "invalid pointer" is reported when the code is actually running, which by itself is "new" one on me. I may ask for help on that one, if I get stuck.
I got no issues learning to use "new" or any other C++ specific features.
Any comments on the contrary are not appreciated.
Cheers
|
|
|
|
|
Thanks for responding. It's good to know a little more context.
As for the "invalid pointer" error, I disagree with the notion of either using a simple C-style type cast, or reinterpret_cast . Both are only workarounds to a severe problem in your code. You're only telling your compiler to shut up, but that doesn't solve the problem that is causing this! All you'd achieve is that the program compiles - and then crashes at run-time!
To properly implement this in C++, the first step is to analyze how that pointer is being used, and what data fields are supposed to be accessed via this pointer. There are two possibilities:
1. all fields being accessed are data members of struct hci_dev_list_req . If this is the case, the malloc() statement from the original code is garbage (both of them). In C++, you should declare dl as follows:
std::array<hci_dev_list, HCI_MAX_DEV> dl;
There's no need for type casts, and no need for allocation or cleaning up - std::array takes care of that.
2. Some fields being accessed are not members of struct hci_dev_list_req . In this case the declaration of dl is garbage! dl is not a pointer to some struct or array of structs. Instead it's a pointer to some undefined struct which, among other things, contains hci_dev_list_req elements. And that is exactly how it should be declared. So, if the original C program does not provide the appropriate struct definition, you should create one yourself.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Bad news.
I tried this
array<hci_dev_list_req, HCI_MAX_DEV> dl_array;
and it broke GPIO class after enabling C11 in crosscompiler - as requested by complier error.
I guess I need to go back, at least temporary, and just hack it with
reinterpret_cast. .
Just FYI - the struct are used to access ioctl and that is an area I do no want to mess with since it works for other I/O access.
struct hci_dev_req {
uint16_t dev_id;
uint32_t dev_opt;
};
struct hci_dev_list_req {
uint16_t dev_num;
struct hci_dev_req dev_req[0];
};
I have not given it much thought, but could I put the "malloc" replacement as "class variable" ( instead allocating / freeing the memory in function itself as it was done in C ) or into a class constructor ?
|
|
|
|
|
I'm not sure I understand. If the variables you called dl and dr are part of your code, then surely it is in your hands how to define and use them? If any code breaks, it is an indication that code was bad to start with. I'm not surprised though: there must have been a reason for the odd malloc statements, and my suspicion, as stated before, is that someone uses these pointers not as pointer to some struct array, but as a pointer to some meta-struct: see my other posting with a suggestion how to declare such a struct.
If you don't fix that broken code, it won't be clean. And reinterpret_cast does not fix anything. It only makes your code compile, but it doesn't make it work, much less clean.
I do not suggest to fix or change the structs hci_dev_req and hci_dev_list_req themselves, only the code using them. That code defines variables called dl and dr, and the way these are allocated currently indicates that code is broken and needs to be fixed. And that includes any code that uses dl and dr.
If for some reason you can't get std::array to work even though it does not require C++11, you could try std::vector instead: the main difference is that std::vector does not require the size to be known at compile time, and it can be resized dynamically. If HCI_MAX_DEV is a constant, std::array should work just fine; otherwise you need std::vector.
Of course, you can't pass a std::array into ioctl where it requires a hci_dev_list_req - you first need to convert it. That is not unusual for modern C++ code using old C APIs: in such cases you need some helper or wrapper functions that perform these conversions under the hood.
If you want to avoid these conversions, then the task you're asking for is an entirely different one - you should have stated that much from the beginning. Then we could have told you that it's not a good idea to transport a hackish C struct into C++ code and expect the resulting code to be 'clean'.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
P.S.: Based on this code segment:
struct hci_dev_list_req *dl;
struct hci_dev_req *dr;
int dev_id = -1;
int i, sk, err = 0;
dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
I suggest the following struct definition:
struct hci_meta {
std::array<hci_dev_req,HCI_MAX_DEV> my_hci_list;
hci_dev_req& operator[](int i) {
return my_hci_list[i];
}
hci_dev_list_req my_hci_req;
};
void foo() {
hci_meta dl; hci_dev_list* dr = dl.my_hci_req;
...
hci_dev_req current_dev_req = dl[i]; ...
}
No need for allocation, deallocation, or typedefs unless you have additional requirements you haven't mentioned yet.
P.S.: since the array in this case is embedded inside a struct, you could use a standard C array instead:
struct hci_meta {
hci_dev_req my_hci_list[HCI_MAX_DEV];
hci_dev_req& operator[](int i) {
return my_hci_list[i];
}
hci_dev_list_req my_hci_req;
};
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
modified 10-Dec-19 2:57am.
|
|
|
|
|
Richard
I knew it had something to do with storage when I looked at cb it was only 4096 bytes 0XFFE
Had a global counter count the number of bytes I uploaded it was 0X16F67A I set LimiText to 0x16f770 and was able to type in the Rich Edit Control
Thanks
|
|
|
|
|
If this is meant as a response to a problem reeported somewhere on this site, please use the appropriate links to post a comment/reply/solution right there, not in some random forum!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
It’s the thread immediately below.
|
|
|
|
|
I streamed in text into a rich edit control To make it align I set SetDefaultCharFormat to Courier New the ES_READONLY flag is not in resource definition file for this control is there an way to modify the text
Thanks
|
|
|
|
|
Your post is somewhat unclear. Please explain in more detail what the problem is with modifying the text of an edit control.
|
|
|
|
|
Let me be specific I am writing a debugger and stream in the program listing. On the left hand side I have a one byte area for the user to in put a command like ‘B’ for breakpoint
Thanks
|
|
|
|
|
Very interesting, in a non-specific way. I am sure you do have a specific question, but I have no idea what it is.
|
|
|
|
|
I stream in the program TEXT from the Z/OS Assembler SYSADATA I then did SEL(0,-1) and changes it to RTF Courier New before displaying it, I would like to enter text
for example here is a line "_ 204 00004A LA R4,QDINIT " Where the Hyhpen is I would like to let the user enter a letter "B' but the entire text is protected
I have been doing research and it seems with the setevnetmask with a notification for a change I would get an en_chnage message and the doc says it I return 0 another message gets sent I am wondering if I
process this message would be able get to set the text modify (for example the B) it. Don't know if I am on the right track with this. All I really want to do is let the user enter text in this now RTF
richedit this should be possible after all Word is an RTF editor and you can modify the text even if its Rich Text (formatted with fonts)
Thanks
|
|
|
|
|
No idea what is happening there. I have used RichEdit controls and never had such a problem. Try a straightforward RichEdit control and don’t do anything to modify its settings to see what happens.
|
|
|
|
|
thanks that's all I was looking for from the way you see it I should be able to modify the text, I did a SetWindowText to the Richedit e.g. this is a test message and was able to type in it the rich edit control
thanks for your help if from the way you see it I should be able to modify the text Ill research the problem
|
|
|
|
|
Hi ,
can u please help me to read XLSX file without office automation
i read XLS file using ODBC Driver.
but XLSX file read by this driver not found please help me how can do this.
thanks for any help and guidance in advance
|
|
|
|
|