How to copy a struct from a pointer-to-void? - c++

Say I have a DLL contains a struct, but I don't know the details of this struct. But I have a void pointer which points to address of the struct.
Can anybody tell me how can I get the details of the struct? Such as output the struct to a text file.
Thank you!

You cannot know the details of the struct without the type definition. Copying a region starting with the void pointer without a type definition will give you the raw binary data, but you wont know where it ends, or which pieces represent which variables. Some of the values could be integer values or they could be pointer addresses. There are all sorts of possibilities.
You should try to obtain the header file.
You might be able to glean some information from the debug / symbol file if you have it (example .pdb files on Windows), or debugging the program with GDB on Linux, this will only work if you have a debug build of the program. Refer to the "whatis" and "ptype" commands in GDB.

You never know this without structure definition. Also there can be "holes" between the user's variables in the real memory placement because of the alignment and padding.
Say if you have,
struct mystr {
char x;
int y;
};
by default such structure most likely will have size 8, and after one byte of char x there will be three bytes of padding (in theory random values), and then 4 bytes of int y, but it depends on compiler and its directives.

Related

Hierarchical overview of static memory usage

I'm developing some software for microcontrollers, and I would like to be able to easily see which parts of the software are using how much memory. The software does not use dynamic memory allocation, I am only interested in static memory allocations (the bss and data sections).
All of this static memory is actually part of a single struct, which contains (most of the) memory the program works with. This is a hierarchy of structs, corresponding to the components of the program. E.g.:
struct WholeProgram {
int x;
struct ComponentA a;
struct ComponentB b;
};
struct ComponentA {
int y;
struct ComponentC c;
struct ComponentD d;
};
...
struct WholeProgram whole_program;
Ideally, I would like to see the memory usage represented with a multi-level pie chart.
I could not find anything that can descend into structures like this, only programs which print the size of global variables (nm). This isn't too useful for me because it would only tell me the size of the WholeProgram struct, without any details about its parts.
Note that the solution must not be in the form of a program that parses the code. This would be unacceptable for me because I use a lot of C++ template metaprogramming, and the program would surely not be able to handle that.
If such a tool is not available, I would be interested in ways to retrieve this memory usage information (from the binary or the compiler).
Rather than using nm, you could get the same information (and possibly more) by getting the linker to output a map file directly. However this may not solve your problem - the internal offsets of a structure can be resolved by the compiler and the symbols discarded and therefore need not be visible in the final link map - only the external references are preserved for the purposes of linking.
However, the information necessary to achieve your aim must be available to the debugger (since it is able to expand a structure), so some tool that can parse your compiler's specific debug information - perhaps even the debugger itself - but that is a long shot, I imagine that you would have to write such a tool yourself.
The answers to GDB debug info parser/description may help.
If you declare instances of the component structs at global scope instead of inside the whole_program struct, your map file should give you the sizes of each component struct.
Packing all the components into one single structure naturally results in only whole_program being listed in the map file.

Bizarre CDStructures.h created by class-dump

today I downloaded class-dump for getting information about the Mach-O files.
After class-dump created the header files of a sample application, I recognized a strange CDStructures.h created by the tool.
There are lots of definitions of struct-objects, as expected. But some of them have no members, like this:
struct AlbumInfo;
....
struct ArtistInfo;
....
But other struct-objects use these empty objects like this:
struct RefPtr<sp::AlbumInfo> {
struct AlbumInfo *_p;
};
Is that right? Or is there missing something? I find it strange that their are so many objects that have no members.
Another thing is that this namespace or whatever sp is not declared anywhere.
But the most strange thing I saw is that the elements inside of the angle brackets are on the one hand types (sp::GuiControl*) and on the other hand values (64, true).
struct Array<sp::GuiControl*, sp::GuiControl* const&, 64, true> {
struct GuiControl **_field1;
unsigned int _field2;
unsigned int _field3;
};
Perhaps, you have already seen something like this and can help me. I don't know where I should start.
Is this just my fault or are there missing informations in the Mach-O files?
class-dump can only show you the structures for which the info is present in the executable's Objective-C metadata generated by the compiler. This usually includes any Objective-C classes and any structures they include as members (the Objective-C runtime needs to know their layout for the reflection/dynamic access to work). In the case of struct AlbumInfo or struct GuiControl, only pointers to them are present, and the size of the pointer is always the same regardless of what it points to, so there was no need to include the layout of the pointed-to structure.
class-dump included the forward declaration so that the code is syntactically correct, but it did not generate the actual struct layout since it was not present in the binary.
As for the angle brackets syntax, it's just C++ template instantiation. Again, the template declaration is not stored in the binary, but only the concrete instance of a template with all parameters specified.

c++ loading large amount of data at compile time

I have a C++ object which needs a huge amount of data to instantiate. For example:
class object {
public object() {
double a[] = { array with 1 million double element };
/* rest of code here*/};
private:
/* code here*/
}
Now the data (i.e 1 million double numbers) is in a separate text file. The question: How can I put it after "double a[]" in an efficient way and eventually compile the code? I do not want to read the data at run time from a file. I want it compiled with the object. What can be a solution? Ideally I would like the data to sit in the separate text file as it presently resides and somehow also have an assignment like double a[] =..... above.
Is this possible? Thanks in advance!
Something like:
class object
{
public
object(){ double a[] = {
#include "file.h"
};
/* rest of code here*/};
private:
/* code here*/
}
The file has to be formatted correctly though - i.e. contain something like:
//file.h
23, 24, 40,
5, 1.1,
In general, you can use #include directives to paste content into files. I've seen virtual methods being pasted like that, if they were common for most derived classes. I personally don't really like this technique.
One large problem with this design is that 1 million ints on the stack will probably blow the stack. What you probably want is to put the data on the data segment, or in some kind of resource that is stores in your binary file and can be loaded at run time. If you need more than one copy of the data, duplicate it into a std::vector at run time, so you know the data is on the free store (heap). Mayhap even use a shared_ptr to a std::array to reduce the chance of needless accidental duplication(or unique_ptr to reduce the chance of reference duplication).
4mb of data is not going to play all that well is all I am saying. And locality of reference on a 4mb array to your other variables is not going to be your biggest concern.
Depending in your compiled target platform and framework, there will be ways to stuff this kind of data into a binary resource. I've never done it for a multi-meg file, but here is the visual studio help on resource files: http://msdn.microsoft.com/en-us/library/7zxb70x7%28v=vs.80%29.aspx
Note that "the data being in the code" does not make it fundamentally faster to load (other than traversing the filesystem once to find it maybe). The OS still has to load the binary, and larger binaries take more time to load, and a big array of values will take up as much room in a binary as it does in a distinct file. The real advantage is that it isn't a file that can be "misplaced" relative to your executable, but resource fork/resource file/etc methods can deal with that.
As noted in the comments below, static const data (and global data) tends to be loaded into the data segment, which is distinct from both the heap (aka free store) and stack (aka automatic store). I forget what the standard calls it. I do know that a static local variable in a function will behave differently than a static or global non-local variable with regards to initialization order (global (static or not) data gets initialized fully prior to main starting, while static local is initialized the first time the function is called, if I remember correctly).
The answer of Luchian Grigore is quite correct. But compiler can have some limit on length of source code line. See for example https://stackoverflow.com/questions/10519738/source-line-length-limit
So try on your compiler. But I am afraid, more simple solution of your problem will be reading of huge data from file.

C and C++ Code Interoperability - Data Passing Issues

The following is the situation. There is a system/software which is completely written in C. This C program spawns a new thread to start some kind of a data processing engine written in C++. Hence, the system which I have, runs 2 threads (the main thread and the data processing engine thread). Now, I have written some function in C which takes in a C struct and passes it to the data processing thread so that a C++ function can access the C struct. While doing so, I am observing that the values of certain fields (like unsigned int) in the C struct changes when being accessed in the C++ side and I am not sure why. At the same time, if I pass around a primitive data type like an int, the value does not change. It would be great if someone can explain me why it behaves like this. The following is the code that i wrote.
`
/* C++ Function */
void DataProcessor::HandleDataRecv(custom_struct* cs)
{
/*Accesses the fields in the structure cs - an unsigned int field. The value of
field here is different from the value when accessed through the C function below.
*/
}
/*C Function */
void forwardData(custom_struct* cs)
{
dataProcessor->HandleDataRecv(cs); //Here dataProcessor is a reference to the object
//of the C++ class.
}
`
Also, both these functions are in different source files(one with .c ext and other with .cc ext)
I'd check that both sides layout the struct in the same
print sizeof(custom_struct) in both languages
Create an instance of custom_struct in both languages and print the offset of
each member variable.
My wild guess would be Michael Andresson is right, structure aligment might be the issue.
Try to compile both c and c++ files with
-fpack-struct=4
(or some other number for 4). This way, the struct is aligned the same in every case.
If we could see the struct declaration, it would probably clearer. The struct does not contain any #ifdef with c++-specific code like a constructor, does it? Also, check for #pragma pack directives which manipulate data alignment.
Maybe on one side the struct has 'empty bytes' added to make the variables align on 32 bit boundaries for speed (so a CPU register can point to the variable directly).
And on the other side the struct may be packed to conserve space.
(CORRECTION) With minor exceptions, C++ is a superset of C (meaning C89), So i'm confused about what is going on. I can only assume it has something to do with how you are passing or typing your variables, and/or the systems they are running on. It should, technically speaking, unless I am very mistaken, have nothing to do with c/c++ interoperability.
Some more details would help.

Alignment of structure members and static variables

We have some legacy code with classes that have members that are used in Interlocked* functions calls.
I want to be sure that some member variables I have are aligned on 4 byte boundaries (for use with InterlockedIncrement, see http://blogs.msdn.com/b/oldnewthing/archive/2004/08/30/222631.aspx).
I can't find anything definitive that specifies the default alignment of structure members for VS 2010. Experimentally, I haven't been able to make a struct violate 4 byte alignment without changing the default packing. All I have been able to find out is that the default packing is 8 bytes and that we're using that everywhere.
http://blogs.msdn.com/b/oldnewthing/archive/2004/08/30/222631.aspx
What I want to know is do we need to add __declspec(align(4)) to every variable that is used in the Interlocked* calls?
Edit: I know about packing and how to do it. Apologies for not being specific enough. Will the CRT also allocate all of my structs so that, given the default packing of 8 bytes, all of my struct members will be, by default aligned on 4 byte boundaries?
Will 32 bit int static variables be aligned by default? I'm looking for some docs on VS, but I'm having a hard time finding docs to explain the defaults.
You can specify the packing for an entire structure by using the #pragma pack directive.
#pragma pack(4)
struct MyStruct
{
...
};
#pragma pack() // this reset the packing to default
You can change the structure member alignment directly in your project settings. The option is called "Struct member alignment". You have just to set it to 4 bytes.
http://msdn.microsoft.com/en-us/library/xh3e3fd0.aspx
Open the project's Property Pages dialog box. For details, see How to: Open Project > Property Pages.
Click the C/C++ folder.
Click the Code Generation property page.
Modify the Struct Member Alignment property.
You can use __declspec(align()) as per http://msdn.microsoft.com/en-us/library/83ythb65.aspx. You can use this with individual members of a struct. See the last example in the link above.