|
Maybe there is a setting for this or a utility or what not. I've been tracking down a buffer overrun and loss of data for a few days, and I have discovered that a data structure pretty clearly defined is blowing past it's size. I think it's due to silly datatype selection and not understanding padding.
I'm working in VS2008 (no choice), and I've yet to find something to help me understand the exact byte layout of a structure. I really need to see the padding being added. The fact that I have a mix of unsigned ints, bools and bytebools can be attributed to ignorance and just poor programming.
This seems to me something any IDE should be able to show me, but if you recall, Microsoft just broke help in VS2008. I was thinking the map file, but I'm not seeing it. What I would like to be able to do is to add a watch to the data structure. In the watch window, I want to see the addresses of each item in the structure by expanding it. But I think I'm going to have to do a lot of typing.
Appreciate any pointers.
Long weekend ahead.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
I'm not sure why you'd be having data overruns, assuming you're sizing any I/O or copying correctly. Even if you have an "anonymous" buffer (e.g. char *), that you're copying your struct from, things should still work correctly with the appropriate sizeof operator in use.
I don't know of any tool within VS that will give you the information you need, but you might get some mileage out of [pack pragma | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/preprocessor/pack?view=msvc-160), and or [offsetof Macro | Microsoft Docs](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/offsetof-macro?view=msvc-160). Either or both won't save you from some typing though.
I think you need to be careful with #pragma pack. X86-64 doesn't seem to have a problem accessing unaligned data, but I assume its slower to do so than correctly aligned data. Other architectures though are not as accommodating. For example, the following code works fine on X86-64 in both 64 bit and 32 bit modes (at least using linux), but gets a bus error when running under arm-32
#include <stdio.h>
#include <stdlib.h>
int main()
{
void *ptr = malloc(1024);
char *tmp = ptr;
for(size_t i = 0; i < 24; i++, ++tmp) {
long double *d = (long double *)tmp;
*d = i * 2.0L;
printf("tmp = %p, d = %p *d = %Lf\n", tmp, d, *d);
}
free(ptr);
return 0;
}
Keep Calm and Carry On
|
|
|
|
|
k5054 wrote: I think you need to be careful with #pragma pack. X86-64 doesn't seem to have a problem accessing unaligned data, but I assume its slower to do so than correctly aligned data. A bit, on average, but not due to mere misalignment itself. There is a small penalty for crossing cache line boundaries and a different one (~85 cycles on Haswell, even more on Core2, not so much on AMD processors or Skylake and newer) for crossing 4K boundaries. That makes random unaligned loads and stores a bit slower on average, since some of them will cross those boundaries, but it doesn't make all of them slower and the impact is averaged out. Usually the addresses wouldn't really be random of course, so you could run into some situation where it keeps hitting a bad case, which would amplify the problem.
There are various other caveats..
Of course, avoiding unaligned operations avoids the whole issue.
|
|
|
|
|
Sorry, I got too specific in my comments and did not provide enough detail. The structure in question is unioned with a communications buffer. The code assumes the structure will fit in the buffer, but there are no checks. It just so happens that the structure extends beyond the communications buffer (from the alignment issue I think - hence my question.
In any event, the result is that the structure's data is truncated with values lost at the end. Luckily, the last 200 bytes or so are spare, so no harm or foul but lousy coding all the same.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
If you have a fast data source, eg. a file or RAM, from which you know that the data element will always be available in a single operation, you should probably read the struct directly e.g.
struct S {
int i;
double d;
char str[25];
};
S s;
std:ifstream data("data.bin", std::ios:binary);
data.read(reinterpret_cast<char *>(&s), sizeof s); If you have a slow data source, from which multiple reads may be needed to read a single data item, you should probably read and update a buffer and then copy that to the struct when you have enough bytes.
Using a union for type punning is not really the best solution. There's a good explanation of that on Stack Overflow: c++ - Accessing inactive union member and undefined behavior? - Stack Overflow[^]
Keep Calm and Carry On
|
|
|
|
|
k,
see my other comments. it's not an overrun in the traditional sense. Just two unioned structures that are assumed to be size compatible, so the aligned structure extends past the other. sorry for the confusion.
thanks
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
A union of structs will take as much space as is required by the largest member of the union. So the mere fact that it is a union should not cause problems.
|
|
|
|
|
truth. I know how these things work. In my case, the data was longer than the communications buffer. Since the code one uses the communications buffer to talk to the hardware, the last 30 bytes or so were being dropped.
thanks
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
Well, the Packing pragma is very old. A Pragma is an adidional instruction to the compiler,stated in te file to be compiled. In the Packing case, it is about how to align data on Preferred Data Boundaries. A 64 bit CPU is most efficient if all items are stored at addresses, a multiple of 64. Failing to do so, the code will not break down. it will just take (very minutely) longer to execute, but will never break your code.
Now, you can Easily enter "Packing Hell"! That occurs when different packing pragma's are used in different components. Unless if something is critical, the best idea is to leave all packing pragments out, unless you have reasons to use them, e.g. Interfacing a new 64 structure with say old 16 or 32 bit data.
Bram van Kampen
|
|
|
|
|
In general all struct items are aligned to word boundaries, so you should be able to draw the layout just by reading the source. However, if you use the #pragma pack directive anywhere then it tends to move things closer together. But without seeing any of your code it's impossible to say more.
|
|
|
|
|
good comments all. I've been around the block, so I'm certain that due to the compiler's desire to arrange things in a preferential way. I've just started digging into this code for another issue and blundered into this one. The structure is stored as a blob in the system, so I must be very, very careful in how I touch this structure. Changing any of the alignment options will surely frag stored data.
This leads me to realize that all one needs to do is change the packing on the compiler line, and the data will be trashed. Oh, but wait, it gets worse. This code base is built with two different IDEs. It's vulnerable there as well. It's 8am on a Sunday, and I have a long day ahead of me.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
I have a C++ project that was created in qt creator. (but doesn't use any of its features) Is there a way to painlessly migrate it to visual studio? There is Qt tools for visual studio but it requires downloading a bunch of different stuff to make it work and assumes you are making a qt project.
I've tried manually adding the files to visual studio but for some bizarre reason there is no easily visible option to add existing folders forcing me to manually create folders one by one and add the files into it. https://shareit.onl/
modified 13-May-21 5:18am.
|
|
|
|
|
Do you have make / cmake files there ? These ones could help you.
|
|
|
|
|
The make/cmake is probably full of qt references.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
1. Create an empty project in Visual Studio.
2. Copy the files into the project directory.
3. In Visual Studio go to the Solutions window where you can add the files to their proper (virtual) folder (Header, Resource or Source).
4. You may then need to adjust the Project settings to build the project correctly.
|
|
|
|
|
Hello everyone I was required to build my first Arduino game and I still do not control well will be happy for your help
Light a random position in both the directions and diagonals represented by two bulbs
That is, if for example: both the right and the top light are on - move diagonally to the right and up and turn both off
And after 20 times of correct moves turn off the lights and let the game end.
|
|
|
|
|
I see no code in your post.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
|
Hi
In the process of re-writing my CAsyncSocket to the more controllable WSA flavor I got stuck on WaitForMultipleObjects.
For Each Event on the socket that I do a bind by that I mean FD_READ, FD_WRITE I CreateEvent I associate the events with WSAEventSelect.
I pass all the info (HWND windows message etc when I want notification to be sent in a structure) pointed to by a LPVOID the parameter in The CreateThread
I let the worker thread wait on the events and do the appropriate SendMessage
Problems is I am abending or rather getting an Access violation on WaitForMultipleobjects
Running under the visual studio debugger I can see the values of the 5 events are correct
The first thing i thought of was that my worker thread didnt have security rights to the events I created in the Mother thread ( all threads are in the same process)
I just figured the quickest way to resolve this was to name the events and do a OpenEvent If I get a null from any events I would have a problem
All of the events named from OpenEvent return a value however they are not the same event values that I created
Real mystery to me
Hope some one can help
Thanks
|
|
|
|
|
ForNow wrote:
I let the worker thread wait on the events and do the appropriate SendMessage Should you be using PostMessage() instead?
"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
|
|
|
|
|
You mean like this the worker thread doesn’t have to wait for a reply from send message
Ok thanks
|
|
|
|
|
Do you pass any MFC object to the worker thread?
|
|
|
|
|
No not sure how WSAWaitForMultipleEvents is implemented in Windows MFC app
But it was always my understanding based on feedback from people such as yourself that any kernel object wait I.E WaitForSingle/Multiple should be done in a worker thread correct ?
|
|
|
|
|
ForNow wrote:
No not sure how WSAWaitForMultipleEvents is implemented in Windows MFC app
But it was always my understanding based on feedback from people such as yourself that any kernel object wait I.E WaitForSingle/Multiple should be done in a worker thread correct ? |
WSAWaitForMultipleEvents has nothing to do with MFC (note that it is defined in Winsock2.h header!).
And what does it have to do with your using WaitForMultipleObjects function?
|
|
|
|
|
I am using WaitForMultipleObjects rather thanWSAWaitForMultipleEvents for socket notification regardless don’t think the wait should be in the main thread
|
|
|
|