Hi,
I am not clear with use of __attribute__ keyword in C.I had read the relevant docs of gcc but still I am not able to understand this.Can some one help to understand.
13 replies to this topic
#1
Posted 19 November 2010 - 12:47 AM
|
|
|
#2
Posted 19 November 2010 - 01:00 AM
onus said:
Hi,
I am not clear with use of __attribute__ keyword in C.I had read the relevant docs of gcc but still I am not able to understand this.Can some one help to understand.
I am not clear with use of __attribute__ keyword in C.I had read the relevant docs of gcc but still I am not able to understand this.Can some one help to understand.
You can tell the compiler to apply attributes to functions and optimize it as so, for example defining a function that is not to return anything i.e. a fatal() or exit() function:
void fatal () __attribute__ ((noreturn));
void fatal (char* msg) {
fprintf(stderr, "%s", msg);
exit(1);[SIZE=-1]
}[/SIZE]The documentation within GCC for __attribute__ is here:Using and Porting the GNU Compiler Collection (GCC): C Extensions
I find it fairly straightforward, what parts do you not get? You may find some simple examples of the different attributes of use to your learning:
Using GNU C __attribute__
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 20 November 2010 - 05:15 AM
Nullw0rm said:
I find it fairly straightforward, what parts do you not get? You may find some simple examples of the different attributes of use to your learning:
Using GNU C __attribute__
Using GNU C __attribute__
Let us deal one by one excerpts from the above page says
This __attribute__ allows assigning printf-like or scanf-like characteristics to the declared function, and this enables the compiler to check the format string against the parameters provided throughout the code. This is exceptionally helpful in tracking down hard-to-find bugs. There are two flavors:
So what does the above paragraph means?
#4
Posted 20 November 2010 - 11:40 AM
Quote
This __attribute__ allows assigning printf-like or scanf-like characteristics to the declared function, and this enables the compiler to check the format string against the parameters provided throughout the code. This is exceptionally helpful in tracking down hard-to-find bugs.
There are two flavors:
__attribute__((format(printf,m,n)))
__attribute__((format(scanf,m,n)))
but in practice we use the first one much more often.
The (m) is the number of the "format string" parameter, and (n) is the number of the first variadic parameter. To see some examples:
There are two flavors:
__attribute__((format(printf,m,n)))
__attribute__((format(scanf,m,n)))
but in practice we use the first one much more often.
The (m) is the number of the "format string" parameter, and (n) is the number of the first variadic parameter. To see some examples:
Are you familiar with the standard C functions printf and scanf? Unlike Microsoft compilers, GCC makes sure that your format string matches the arguments you pass in. Using the attributes outlined above, you can write your own function whose format string will get checked just like printf and scanf are.
sudo rm -rf /
#5
Posted 21 November 2010 - 08:34 AM
I have used scanf and printf in C programs but still from your reply I could not understand
What do you mean by format string and the matching arguments?
dargueta said:
sure that your format string matches the arguments you pass in.
#6
Posted 21 November 2010 - 01:58 PM
#7
Posted 22 November 2010 - 02:28 AM
Here is a C program I wrote but I am not able to understand how will I use attribute in it
#include<stdio.h>
int myfunction (int ,int);
int main ()
{
int a,b,c;
c=myfunction(a,b);
printf("the result is %d ",c);
}
int myfunction (int a,int b)
{
int c;
return c=a+b;
}
#8
Posted 22 November 2010 - 02:29 AM
What do you want to use the attribute for? There's a lot of things it's capable of doing.
sudo rm -rf /
#9
Posted 22 November 2010 - 02:48 AM
I first want to understand how __attribute__ works so any simple thing would be a good start for me.I looked at the format arguments where on your link it was mentioned scanf or printf like arguments but what is the difference between scanf and print for attribute?
#10
Posted 22 November 2010 - 02:52 AM
printf lets you specify field widths and other things that scanf has no need for. For example:
This code
produces the output 0x0032. If you pass this format string to scanf, it'll choke, because you have no need for forcing the leading zeros and such.
This code
printf("%#04x", 50)
produces the output 0x0032. If you pass this format string to scanf, it'll choke, because you have no need for forcing the leading zeros and such.
sudo rm -rf /
#11
Posted 22 November 2010 - 02:53 AM
Why does the above give out put 0x0032?
#12
Posted 22 November 2010 - 02:58 AM
# means print out the base prefix for the number you're printing out. For hexadecimal this is a leading 0x, for octal it's a leading 0, and decimal has none.
04 means that the number must take up at least four digits, and be padded with zeros if it's shorter. If I had just put 4 then printf would've padded it with spaces.
x means print out the number in lowercase hexadecimal. X uses uppercase letters.
04 means that the number must take up at least four digits, and be padded with zeros if it's shorter. If I had just put 4 then printf would've padded it with spaces.
x means print out the number in lowercase hexadecimal. X uses uppercase letters.
sudo rm -rf /
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









