|
Hi Mark,
Did you ever get this written and working?
|
|
|
|
|
OMG I never even looked at it again!
Wow, that was really a year ago? Man, did I get sidetracked
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'm doing binary file input and I want to read 4 32-bit values in binary from a file with the read() command as elegantly as possible. Read gives me back a pointer to a char array of 16 chars (16 bytes of binary data representing the 4 32-bit values, called headerValPtr)
So I have a simple struct:
myStruct
{
int
int
float
float
}
My data types in the struct are 32-bit unsigned integers and floating point vals. So I create a pointer to myStruct and allocate memory for it, call it sillyStruct. Will it work to then say sillyStruct = (myStruct*)headerValPtr? I just want to typecast the binary data into the 4 values in the struct.
I don't have a good textbook handy to see how struct allocates memory. If the struct pointer simply points to a set of consecutive memory locations that hold all of the data, then I'm good. But I'm not 100% sure that is the case.
If you want real code snippets I can throw something together, but this is more of a theoretical question so maybe you can live without?
Thanks,
--Seth
|
|
|
|
|
|
It's like trying to herd a pack of fish!
|
|
|
|
|
Maybe we need a purse seine[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
No problem! A structure is basically a record of a specific size, and therefore you can save an exact copy in binary form and read it back (using sizeof(struct what_ever)). You must ensure that the program writing it and the program reading it is using the same byte alignment or the reader will be reading the wrong number of bytes. That is all there is to it, unless the structure contains a pointer to other data, in which case life get much more complicated.
Note that the above applies mainly to the C language, because C++ can be bit trickery. If you are only using a structure in the manner of your example, then no problem.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
search for structure packing, pack your structure for 1 byte.
then
any way you have all the data in an char array,
so try this,
struct MyStruct
{
char a;//1 byte
int b;//4 byte
int c;//4 byte
};
int main()
{
MyStruct MyObj;
MyStruct* l_Stemp = &MyObj;
//You have an array of 9 bytes
//let's say as l_Carr[9];
char* l_cPtr = reinterprete_cast<char*>(l_Stemp);
//Here address of l_Stemp and l_cPtr is same so u can write values to l_cPtr using and read it from l_Stemp
for(int i=0;i<9;i++)
{
*l_cPtr++=l_Carr[i];
}
//Here u'd get ur struct back in the form of l_Stemp pointer, While using this u have to be very careful because,
// pointer is poweful but power comes with responsibility
return 0;
}
}
// I did not compile this program if any compilation error is thr pls let me know
|
|
|
|
|
abhijit bhopale wrote: search for structure packing
You may want to do that yourself
By default....
struct MyStruct
{
char a;//1 byte
int b;//4 byte
int c;//4 byte
};
...is not a 9 byte structure. It's more like
struct MyStruct
{
char a;//1 byte + 3 bytes padding
int b;//4 byte
int c;//4 byte
};
Size is 12 bytes.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi.
whenever you right a line of code which ask for an input you make sure that if the input doesn't match the variable's type the program prompt user ( " please enter a digit,..... " or ....) .
so righting 1000 lines of code asking for an input and writing 2000 controll functions to make the program stay running is not a shortcut if there is any.
So , is there any shortcut. for instance, can vi write a line of code in the beginning of program and make sure thatthe program prompt oss whenever the input's not matching?
Thanks
int life()
{
in a land with no bird, no spring. My first journey was a
return 0;
}
|
|
|
|
|
You don't have to duplicate the validation code everywhere you use it.
You can write a function which is called to verify input. Or several functions to validate
different types of input (numeric, alpha-numeric, etc.)
With Windows edit controls, you can subclass the control and perform
validation as characters are entered by the user.
With console input, you can write function(s) which read characters and
perform validation as they're entered by the user.
etc...
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks.
I have a long way to go
int life()
{
in a land with no bird, no spring. My first journey was a
return 0;
}
|
|
|
|
|
Yes! But you are asking the right questions, so you are well on your way. Any time you have to write the same piece of code 2 or more times, you must consider making it a separate method/function call. There are other things you will figure out over time, and I have no doubt that you will.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
I am building an application that uses external DLL files.
I got unexpected error.
Windows has triggered a breakpoint in abc.exe
This may be due to a corruption of the heap, and indicates a bug in abc.exe or any of the DLLs it has loaded.
The output window may have more diagnostic information.
This runtime doesnot occur always. Only Sometimes it comes but most of time the program works fine.
I couldnot find out what the problem is. If there was a problem in program the error should always occur.
can you please help me
saroj sigdel
-- modified at 14:23 Thursday 8th November, 2007
|
|
|
|
|
sarojsigdel wrote: If there was a problem in program the error should always occur.
That's not true at all. I'm always amazed at how long an application can run with a memory
access bug before one little change somewhere finally causes it to visibly crash.
You may need to run the program over and over again until it crashes.
You can do this in the debugger. Or, you could run the program outside the debugger,
and attach to the process with the debugger when it crashes.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
To Which process I should attach
saroj
|
|
|
|
|
Generally, whatever the name of your executable (.exe) is.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
After attaching a process what should we do
|
|
|
|
|
Hopefully you'll be able to use the stack trace and threads debug windows
to navigate to the offending line of code.
I said hopefully...
Preferrably, you want to catch it while running in the debugger so it will break
on the first-chance exception.
Or you can use the 50% rule - if it runs without crashing at least 50% of the time.
then ship it.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi again
In the event of a dialog being minimized, I wish to execute a function. I have been able to pick up on the SC_MINIMIZE command (if the minimize button is clicked on), but how would you do this if the dialog is minimized by another app being clicked on (either on the taskbar, or by the app being open underneath my 1st dialog)?
Thanks for your valuable help!
69 Bay
|
|
|
|
|
Have you tried the WM_SIZE event?
|
|
|
|
|
69 Bay wrote: but how would you do this if the dialog is minimized by another app being clicked on (either on the taskbar, or by the app being open underneath my 1st dialog)?
It is not clear to me...
May be WM_SHOWWINDOW or WM_WINDOWPOSCHANGED your solution.
- NS -
|
|
|
|
|
Do you want to send WM_MINIZED to other programs?
|
|
|
|
|
Hi, I'm using VS2005Pro developing in C.
I remember being able to begin a comment section and VS would put in a template depicting function name, return value and a parameter list. Of course, the developer would have to fill in the blanks (as it were).
I thought it was something akin to '///' and on the third '/', the template would appear that I could fill in.
Does someone else know what the correct key sequence is?
Is this something that only happens for C# && C++?
Thanks!
|
|
|
|
|
Hi CP,
I have a deployment project on VC2005 SP1 and it creates a Setup.exe and a .msi file, and other prerequisite files. Actually prerequisites are Windows Installer setup files, what's really necessary for application installation is only .msi but Setup.exe is needed for me since it can get a sufficient privilege for UAC in Vista to work correctly. Directly clicking on .msi starts installation but it stops by an error in a custom action in my deployment project that requires a elevated privilege.
Basically what I want to know is how to prevent a user from clicking .msi instead of Setup.exe, I have seen other applications do it, it says "don't click .msi directly, click Setup.exe" but couldn't find how to do it without InstallShield and other products. Editing .msi with Orca is probably what I should do but can't find a relevant part.
Can anybody help me to figure it out? Thanks in advance.
|
|
|
|