Search results
Top results related to what are the function of dictionary in c compiler
Top Answer
Answered Jul 19, 2017 · 3 votes
- Same as regular typdefs, incredibly useful for 2 and 4. Instead of typing the whole thing, you can now use myFuncDef
- A function declaration for a function that recieves a function pointer. A function that takes a function pointer of 1. would thus look int add2to3(myFuncDef functionPtr);
- -
- A function that takes an int and (probably based on that) returns you a function pointer. Using the typedef it would have looked like myFuncDef functionFactory(int n);
Without typedefs these chains of pointers to pointers could get incredibly long as can be seen in this question: C syntax for functions returning function pointers
1/5
Top Answer
Answered Mar 25, 2018 · 2 votes
N1570 6.11.6:
The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.
This same wording appears in the 1990, 1999, and 2011 editions of the ISO C standard. There has been no change. The word obsolescent says that the feature may be removed in a future edition of the Standard, but so far the committee has not done so. (Function pointer declarations are just one of several contexts where function declarators can appear.)
The Introduction section of the C standard explains what obsolescent means:
Certain features are obsolescent, which means that they may be considered for withdrawal in future revisions of this International Standard. They are retained because of their widespread use, but their use in new implementations (for implementation features) or new programs (for language [6.11] or library features [7.31]) is discouraged.
A call to a function declared with an old-style declarator is still required to pass the correct number and type(s) of arguments (after promotion) as defined by the function's actual definition. A call with incorrect arguments has undefined behavior, which means that the compiler is not required to diagnose the error; the burden is entirely on the programmer.
This is why prototypes were introduced, so that the compiler could check correctness of arguments.
On an i386 system with GCC, “extra” arguments passed in a call to an empty-parentheses-type’d function pointer are ignored, because of how stack frames work ...
Yes, that's well within the bounds of undefined behavior. The worst symptom of undefined behavior is having the program work exactly as you expect it to. It means that you have a bug that hasn't exhibited itself yet, and it will be difficult to track it down.
You should not depend on that unless you have a very good reason to do so.
If you change
int (*fp)() = foo;-
to
int (*fp)(int) = foo;-
the compiler will diagnose the incorrect call.
2/5
Top Answer
Answered Jan 28, 2024 · 0 votes
Calling a constructor and calling a function pointer of a constructor in C++ are very similar in the sense that they are both quite impossible:
- You cannot call a constructor in C++.
- You cannot obtain a pointer to a constructor in C++.
According to the C++ spec about constructors:
Constructors have no names and cannot be called directly.
So, in C++, you do not call constructors. The language does that for you when you use the new keyword, when you declare an object on the stack, etc. The expression new MyClass() might look like a constructor invocation, and it does in fact contain a constructor invocation, and so it might be mistaken for a constructor invocation, but that's not what it is; it is a "new expression", which includes, among other things, an invocation of a constructor. But the other things are crucial: you cannot just skip them and directly invoke the constructor.
Function pointers of constructors do not exist because normally you cannot take the address of a constructor. If you examine the names exported by the DLL, and somehow figure out which name corresponds to the constructor, and use that name to obtain the entry point of that constructor from the DLL, you are cheating: you are doing something which is not normally supported by the language. As a result, you should not attempt to invoke that entry point. A constructor is only supposed to be invoked by the language.
Also according to the C++ spec:
A constructor is a special non-static member function of a class.
(Emphasis mine.)
A constructor assumes that memory has been allocated, and proceeds to initialize that memory. The language takes care of the memory allocation, but invoking the constructor directly does not. Since a constructor is non-static, it expects an uninitialized this to be passed to it, but you are not allocating any memory and you are not passing anything to the constructor. The result is exactly as expected: a crash.
And besides, think about it: the constructor could not be internally allocating the memory, because how would it work then if the object was allocated on the stack? And how would it work if the constructor was being invoked by the constructor of a derived class which presumably occupies more memory?
You can get your DLL to work by using a factory method. A factory method is a public static method which executes new ClassA() and returns the result, or declares ClassA() and returns it by value. Then, obtain the address of the factory method from the DLL, and invoke it.
3/5
Top Answer
Answered Sep 06, 2010 · 34 votes
There are a lot of minor nitpicks. One that strikes me as being the most obvious is that in C++, you have to cast the return value of malloc. Also structs are automatically typedefed in C++.
Always use a C compiler for C code, not C++. C++ isn't perfectly compatible with C.
A few others:
- In C, declaring void func(); declares a function that hasn't specifed what its arguments are, whereas in C++, void func(); is equivalent to the C void func(void)', taking no arguments;
- Prototypes are required in C++, whereas it's generally just a warning in C;
- The type of character constants (like 'a') is int in C and char in C++;
- The type of string literals is char [] in C and const char [] in C++;
- Some legitimate variable names in C, like class, are reserved keywords in C++.
For all those who don't believe me and are downvoting, check out this C code:
#include <stdlib.h>-int main(int argc, char **argv) { int *i = malloc(sizeof(int));- return 0;}-
Compilation under gcc is fine, but compilation under g++ gives the following errors:
test.c: In function `int main(int, char**)':test.c:4: error: invalid conversion from `void*' to `int*'-
4/5
Top Answer
Answered Jun 26, 2017 · 5 votes
Answering questions in reverse order:
C does not have array variables....but this is really just working with pointers with an alternative syntax.
This is incorrect, and you need to toss that bookmark in the trash. It's a common misconception that arrays and pointers are the same thing, but they are not. An array expression will be converted to a pointer expression under most circumstances, and array subscripting is accomplished through pointer arithmetic, but an array object is an actual array, not a pointer.
If pointer also keeps the variable's address in it and at the same time we can get the variable's address using & sign, can't we do the same task pointer does by deriving the address of any variable? I mean if I don't declare it as pointer and use it as int ptr = &i; in 2nd code snippet and use it as normal variable, what would be the differences?
That code doesn't illustrate why pointers exist, or why they are useful.
C actually requires us to use pointers in the following cases:
- To write to a function's parameters;
- To track dynamically allocated memory;
Pointers also make dynamic data structures like trees and lists easy to implement, but they aren't required for it (unless you're using dynamic memory allocation in those structures).
Writing to a function's parameters
C passes all function arguments by value; the formal parameter in the function definition is a separate object in memory from the actual parameter in the function call, so any change to the formal parameter is not reflected in the actual parameter. For example, assume the following swap function:
void swap( int a, int b ) { int t = a; a = b; b = t; }-
This function exchanges the values in a and b. However, when we call the function as
int x = 4, y = 5;swap( x, y );-
the values of x and y won't be updated, because they are different objects than a and b. If we want to update x and y, we have to pass pointers to them:
swap( &x, &y );-
and update the function definition as follows:
void swap( int *a, int *b ) { int t = *a; *a = *b; *b = t; }-
Instead of swapping the contents of a and b, we swap the contents of the objects that a and b point to. This crops up all the time - think about the scanf function, and how you have to use the & operator on scalar arguments.
Tracking dynamically allocated memory
The dynamic memory allocation functions malloc, calloc, and realloc all return pointers to dynamic memory buffers; there's no variable associated with that memory as such.
char *buffer = malloc( sizeof *buffer * some_length );-
A pointer is the only way to track that memory.
5/5
stackoverflow.com › questions › 78959310Declaring variable based on itself in C++ somehow works, but ...
stackoverflow.com › questions › 7895931011 hours ago · works (compiles and runs successfully, with different values for x being printed and y always being equal to zero (which is the same behaviour when compared to just putting int y; and int x; instead of int y = y; and int x=x;, respectively)) on onlinegdb when I select C++, C++14, C++17, C++20 or C++23 (but fails (does not compile ...
stackoverflow.com › questions › 78959114Can I use AddressSanitizer in C++Builder CLANG compiler?
stackoverflow.com › questions › 7895911411 hours ago · 0. I found that AddressSanitizer exists in the Clang compiler. And C++Builder uses the clang compiler. Can I mix these two things together? It doesn't work for me. I tried to put the -fsanitize=address compiler flag into a simple 64bit Windows project in C++Builder 10.2. I got compiler error:
expertbeacon.com › understand-data-structures-in-cUnderstand Data Structures In C And C++ - ExpertBeacon
expertbeacon.com › understand-data-structures-in-c11 hours ago · Types of Data Structures. Some common data structures and their core usage patterns: Arrays – Index based fast access and traversal of linear data. Linked Lists – Efficient insertion/deletion and sequential access. Stacks – LIFO access for temporarily storing data. Queues – FIFO access for buffering/queueing data.
link.springer.com › chapter › 10Understanding andWorking with theDevicetree in General and ...
link.springer.com › chapter › 1011 hours ago · The corresponding function pointer values will be used in the initialization of the collection of function pointers data structure for the API. In the case of the code snippet shown previously, suppose that the actual driver functions were defined as follows:
Searches related to what are the function of dictionary in c compiler