Get length of any function? [duplicate] - c

This question already has answers here:
Getting The Size of a C++ Function
(17 answers)
Closed 1 year ago.
Assuming there's some function in my program or any linked library to it, is there a way to get the length of the function in bytes? By the length I mean the size of the whole code inside the function, from the start to the very end (functions branch out, so the end would be e.g. last 'ret' instruction, or something like that). Is there a simple way to do that, maybe some API functions or is it too much non cost-effective to create such a function?

There are probably tools to dump the contents of an object file, and then you can parse the output of this dump, and there is a chance that you can find out more or less reliably what the length of s function is. Adding data that is used by the function is more tricky. Or adding things like lambdas / closures that should probably be counted towards the length of the function.

Related

Linux Redirect Messages: __strlen_avx2, __mempcpy_avx_unaligned_erms, __strchrnul_avx2

I have questions regarding the three redirect messages I got. Just to give some context to my program...
I am using C programming and Linux. I read numbers from a file containing all the words in a dictionary. I extract the words from the file and save them in a string object which consists of 3 things: a dynamically allocated array of characters for the words (the string objects are designed so that I do not need null terminators at the end of the arrays), an integer for the size of the word currently being stored, and an integer for the capacity of the current dynamically allocated array of characters. Each string object itself is stored in a vector, and I have an array of 30 vectors with the index of each vector corresponding to the size of the words. For example, index 2 in my array of vectors has 94 words which means the vector at index 2 contains 94 string objects with each object holding a word of size 2. After storing them in the vectors, I then print them to stdout (you can see the tail end of this), and then print out the size of each vector as well as the total words extracted.
Up until now (I'm a fairly new CS student so bear with me), I ran my code always with valgrind and --leak-check=full. As you can see, I have no memory leaks or errors. However, I became aware of the "-v" I could run my program with as well, and when I do that, I get the three redirect messages:
libc.so.6:__strlen_avx2
libc.so.6:__mempcpy_avx_unaligned_erms
libc.so.6:__strchrnul_avx2
I have no idea what these mean. I tried trying to figure it out for myself by looking it up online and I couldn't figure it out so I'm wondering what those errors mean. The last thing I'll say is, the first redirect message involves "strlen" which is a string library function in C's string.h library. However, I never use string.h. The whole point of this project was for me to create my own customized string objects and my own library of string functions to work with these string objects. So, assuming that the redirect message is referring to the string.h library function, I have no idea why it's doing that.
I didn't post any code since it's a long project. I'm just looking for answers as to in general what those messages could be referring to.
Valgrind will check the use of library functions too - such as strlen, memcpy, strchrnul.
When you compile with some optimizations enabled or otherwise, then these function calls might be replaced with an optimized version, i.e. here ones optimized for AVX and AVX2. But those functions are doing some dirty tricks that would otherwise be illegal in C, and also hard for valgrind to check. So valgrind reditrects the call to __strlen_avx2 back to strlen, so that valgrind can more easily track the specifics. Now, -v means only --verbose and it shows more information... and for some it might be crucial to know that the actual function __strlen_avx2 is not called at all, unlike when not run with valgrind, but any calls to that would jump to the strlen controlled by valgrind instead.

C: How can I cast a pointer (or am I going about this all wrong)? [duplicate]

This question already has answers here:
Creating an atoi function
(5 answers)
atoi implementation in C
(6 answers)
Closed 4 years ago.
I should first start out by saying that I come from a Java background. That being said, I'm just starting to learn C and I really struggle with the use of pointers. The concepts are simple enough, but actually using them proves to be a rather difficult and frustrating experience for me.
At any rate, I'm trying to create a function that replicates atoi without using the stdlib.h library. I'm thinking it's a simple matter of casting, but when I test it I get some really strange results. What I have is as follows:
int myatoi(const char* str){
return (int)*str;
}
Given that I don't really know what I'm doing when it comes to pointers, I'm most certainly doing something wrong, but I have absolutely no idea what.
That will not work. Casting will result in the "reinterpretation" of the first character (at location *str) into its ASCII value as an integer.
You will need to process the string by iterating through its characters and parsing them.

generating new variables in c [duplicate]

This question already has an answer here:
Creating variable names from parameters
(1 answer)
Closed 8 years ago.
Is is possible for your code to generate new variables in c? For example, if I made "example_variable = 15", is there any way to automatically generate 15 new variables such as: "generated_variable_1", "generated_variable_2", "generated_variable_3", all the way to "generated_variable_15"?
I'm very new to c, and I haven't had a proper introduction to it, so I only know the basics, especially when it comes to variables. I am pretty sure this is really high-level stuff, so I'm sorry if the question doesn't make sense. I am open to any suggestions for alternate ways of generating the variables.
I know there are probably answers already out there, but I've had trouble finding them and would like answers specific to what I'm looking for, as opposed to piecing together what I need from what I can find.
What you are talking about - generating variables at runtime - is not possible in C. The reason is that C is a low-level language and does not expose an API for runtime manipulation. In fact, once compiled, C programs don't use variables - are values are stored directly in memory using memory addresses.
The closet equivalent to what you're looking for that's available in C is an "array". To declare an array, you can do:
int var[15];
int var2[n]; // in C99+, n is a variable saying how many elements you want in the array
You can also do this with malloc, but this is a bit more complicated and then you must free the values.
A running C program doesn't use your variable names at all. Those names were useful for the compiler to build the program, but are discarded before you run it. This means that in C (but not in interpreted languages like python):
If you rename your variables, you get the exact same program
If you do strings <your program> you won't see any variable names (unless you retained debugging symbols)
Hence, runtime is too late to create new variables. In C, variables are compile-time only. Of course, you can use arrays, or dictionaries, to simulate run-time variable creation, like the other answer, and a few commenters, suggest.

How give array name from string variable in C [duplicate]

This question already has answers here:
Variable with char[] name
(3 answers)
Closed 8 years ago.
I am stuck with a situation where I need to give array name from string variable.
Basically I want to create an array with same name as value in another string variable "name":
char *name="arr_name";
In my case the string being hold by variable name may change. Hence advice accroding.
Thanks!
I think you're looking for some mechanism, similar to the ones found in the higher level languages as in python (introspection), or C# (reflections). C doesn't provide this kind of insight from the runtime, not even the variable names are existing in the bytecode - so basically it's not possible and doesn't make any sense in the terms of the way how C works.
I don't know if that helps, but one thing you could do is to statically (so in the compilation time, not while it's running!) populate char* and create variable with the same name, given that the value of the string is a proper name for the variable (Naming convention for C/C++). You can achieve that by defining a proper macro (#define your_macro(...) code_to_populate_char_and_declare_variable), but I cannot see any point in doing so.

What is the way to find the String literal size limit [duplicate]

This question already has answers here:
Is there any limitation on string length defined in variable argument list
(3 answers)
Closed 8 years ago.
I wanted to know what are the ways to find out the String literal size limit.I guess different compilers do provide the max size limit of string literal but how do I find it programmatically or there is some standard header files which maintain this size limit as some macro??
I checked the C99 draft, all it says is that at least 4,095 characters should be supported in a string literal; there doesn't seem to be a maximum length. This makes sense to me; why impose such a limit?
I really don't think you can "detect" this at run-time. Of course you should be able to detect it, crudely speaking, at compile-time by checking if the compilation succeeds. Write a program that generates a program containing a string literal of a given length, then try to build that output and iterate until building fails. Of course you will only have learnt something about your particular compiler, not a general lesson.
Perhaps you should try to state your actual problem, it seems you're kind of hinting at it instead.

Resources