Use variable length array in a struct? - gdb

With llvm-ir (text) I know how to use llvm.dbg.declare, !DISubrange, etc to create a variable length array that gdb can understand
However, I do not know how to do this in a struct. The problem is DISubrange. If the struct is a variable on the stack I can declare the length as a variable and give it to DISubrange. How would I do this in a struct? I have no idea how to use a member as a variable. I suspect DIExpression is an answer but I have no idea how to use it. LLVM seems to suggest its using DWARF 5

Related

Is length of variable name can create any performance or efficiency related issue?

I'm working on an iOS project with Objective-C and I found following line of code written by the someone else previously
NSString * const number_of_time_user_open_app_after_last_time_invitation_screen_shown_to_user = #"number_of_time_user_open_app_after_last_time_invitation_screen_shown_to_user";
My question is not specific to Objective-C or above line of code, I just want to know length of variable can create any performance or efficiency related issue to program or not.
Length of variable name doesn't make any difference in performance.
variable occupies some byte of memory base on language compiler. that memory is only refer object which assign to it or data value if it refer to datatype value.

Run time penalty in Variable Length arrays?

Reference to the Stackoverflow question title "variable length arrays in C++" we know that following is valid
void foo(int n) {
int values[n]; //Declare a variable length array
}
I have read that there exists a small run time penalty(in variable length arrays) here. Can somebody nicely explain what that is in this context?
I would also try my level best to locate the link where I read it.I apologize i could not mention it here.
IIRC, with GCC, functions with VLA's:
Will not be inlined
Need to use an extra register for the frame pointer (that is, -fomit-frame-pointer works on non-VLA functions), so there is one less register available for the other work the function needs to do.

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

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.

C++ declaring variable inside if statement

I have a really basic problem in c++, I'm reading a tab separated file and I want to declare an array with the dimension if the number of fields the file has (work with different files with different widths) so I need to read the first line and count the number of fields, I tried this:
while(getline(t, line));{
...
if(!flag)
{int array[size][5];
flag=1}
...
}
But then I get the error:
error: 'array' was not declared in this scope
I understand it is because the scope of the variable is in the if loop, is there any way to declare a null array and resize it? Or will I have to use pointers?
The size of an array must be a compile-time constant. Use a std::vector if you want a dynamically-sized array.
Other issues with your code:
Remove the semicolon after the while, or your loop body will only be executed once after the whole file is read.
Add a semicolon after flag=1.
You are writing C++, so why not use a std::vector<std::vector<int> >? If possible (here it is) try not to use raw pointers.
Pointers will be the way to go...
int array[size] possible just in C99.
In C++ plain-C array sizes should be compile-time constant.

How to inspect std::string in GDB with no source code?

I'm trying to debug a program that has no source code available, and I need to look at what it has stored in a std::string. I've been Googling and looking on here, and I've found some information about outputting STL containers, but all of it refers to variables, with no source or debug information all I have is a memory offset of the class data. Is there any way to do this?
Every std::string implementation has a pointer to the raw characters in it somewhere. For g++ 4.x, that pointer is at offset 0 into the string.
If you know that the string resides at e.g. 0x7fffffffda88, then
print *(char**)0x7fffffffda88
is what you need.
Perhaps the easiest option is to use the c_str method, as in:
print myStr.c_str()