|
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
|
|
|
|
|
|
|
thanks for reply
but i want to ask one thing these .Net dll can use in my mfc VC++ application.
|
|
|
|
|
Whoops! Sorry, I thought this was a C# question.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
its ok plz provide me any help for my mfc vc++ application
|
|
|
|
|
In the past for just basic files I have used
GitHub - brechtsanders/xlsxio: XLSX I/O - C library for reading and writing .xlsx files[^]
Note the 3 limitations
1.) assumes the first row contains header names
2.) assumes the next rows contain values in the same columns as where the header names are supplied
3.) only values are processed, anything else is ignored (formulas, layout, graphics, charts, ...)
CSV has easily become the more normal interchange and as its as simple as "save as" in excel it's rare anyone bothers with raw excel file anymore. They can do all there fancy graphs etc in excel itself and just export the data
In vino veritas
|
|
|
|
|
|
Le@rner wrote: ...but XLSX file read by this driver not found What exactly is this supposed to mean?
"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
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
It means what it says
He has a current version of Excel but the XLSX file is from and older version and that earlier driver isn't installed on the machine.
Microsoft ODBC drivers simply call prior version drivers to load older version XLSX files they don't of themselves read older versions.
The message is correct even if a bit humorous
Now ask me the real funny question can you tell what version an xlsx file was created from
Well yeah if you have HexEditor the BIFF in the beginning of file header will have a tag XL9, XL10, XL12 etc.
Find that old Driver, Download that driver, install and you are away .. simple for an end user hey
In vino veritas
|
|
|
|