Non-copyable struct in C? [duplicate] - c

This question already has answers here:
How do you implement a class in C? [closed]
(16 answers)
Closed 3 years ago.
After reading this page, I already know how to implement non-copyable
classes in C++.
(How do I make this C++ object non-copyable?)
Now I want implement non-copyable in C,
But I don't find similar code in C.
So I want to ask how to implement in C.

You can do this using opaque pointers. The idea is:
You define a struct somewhere and you define all of its operations in terms of a pointer to that struct. That would probably be a standalone compilation unit.
The consumers of your struct only get a declaration but not the full definition of that struct, which means that they don't know the layout or even the size of the struct. It follows that they are able to receive, store, and pass around any pointers to that struct, but not values of it.

Related

When should I use pass by reference instead of pass by value? [duplicate]

This question already has answers here:
When should I pass or return a struct by value?
(9 answers)
Closed 7 years ago.
I know the difference between pass by value and pass by reference. I use them and understand how they work in the codes that I've dealt so far. However, I'm looking for a general rule. What is generally the best time to use pointers and what is the best to use actual values? Examples are much appreciated.
As a general rule, pass-by-value for basic types (int, char, etc.), and pass-by-pointer (or better, pass-by-reference) for big data as struct.
Thinking of a struct with 1000 data members, and the cost to copy that gigantic data to a function. It'd be much quicker to pass-by-pointer or pass-by-reference in that case.

Use C++ possibilities in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It might be a silly question, but I'm interested in it very much. Is it possible to implement operator new, dynamically expanding arrays, classes in pure C?
Any links or code examples will be appreciated.
new: #define new(type) malloc(sizeof(type)) (have to call it using function syntax, like struct stat *st = new(struct stat))
dynamically expanding arrays: realloc plus some custom array-manipulation functions (like push_back, etc.) - this is commonly implemented by third-party C utility libraries (and, as #Mgetz points out, some compilers have built-in extensions for it)
classes: structs with function pointer members (this is very common in several projects, such as the Linux kernel)
You might want to look at GObject, which is a C library providing some object-oriented features to C. Also see the dozens of hits you get for googling "Object-Oriented C".
A quick google search revealed this:
http://ooc-coding.sourceforge.net/
Haven't read it through but it sounds like what you're after.
Yes, it is possible (common?) to implement object orientedness in C - or at least the bits that are especially needed.
An example is a once created a garbage collector by storing the pointers to malloced memory and the free function in linked lists.
The best thing about C is that it just works and there is almost zero overhead. The more work a language does for you automatically can mean there is a lot more overhead - though this is not always the case.
It depends if it is OK for you to reimplement the compiler.
If it's ok - you can do whatever you wish, otherwise:
new - as an operator - no, but you can define a function + macros that will simulate it.
classes - yep, you can. you may simulate it pretty closely with static functions and an array of pointers to functions. But there will be no overloading.
expanding arrays - yes, with the classes simulation above.

How does struct works under the hood? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am curious about struct that we declare in C. What does it actually do ?
I know we can make nodes & pointers to those using struct but how does it function ? Like a while loop checks the condition & branches accordingly if equal or not equal. What does struct do under the hood ?
A struct type is a user-defined composite type. It is composed of fields or members that can have different types.
From struct-wiki:
A struct in the C programming language is a declaration that defines a list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer.
For memory allocation of a struct: check out How are C struct members allocated?
For why to use struct, check out Why should we typedef a struct so often in C?
Its nothing more than multiple variables treated as one entity. There is not much magic behind this, the values simply appear behind each other in the order of declaration in the memory.
A C struct simply represents data in a specified way and doesn't do anything at all. It is used to represent a more complex data-type, such as a linked-list node.
Using struct a user can define its own required data type to handle complex data.
just like array where all elements in array are of same type but in struct each element can be defined in user's desired way.
so struct is used to define ""user-defined data types".

Why do you have to access struct members with an '->'? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Normally you access structure-members by a dot (Struct.Member), but when Struct is a pointer, you have to use Struct->Member
Is there a special reason for this? Because in both instances you're just referring to a piece of memory. And the compiler should be able to handle both operators, so is it just an enforced codestyle ?
It's because the language is defined that way.
There are languages that allow the prefix of the . operator to be either a structure or a pointer to a structure. No ambiguity is introduced because, as you say, the compiler knows what type the prefix is.
C just happens not to do that. I don't think there's any deep reason for it, it's just the way Dennis Ritchie decided to do it.
And using . for structures and -> for pointers also makes it clear to the reader whether the prefix is a pointer or not. For a relatively low-level language like C, that kind of detail can be important.
You can access the struct elements in . notation if the struct is a pointer. You just have to dereference the struct elements
So :
(*struct).element should work fine

Concept of function pointers in C? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the point of function pointers?
hi all,
I want to get the basic and concrete idea of function pointers in C language.
ie 1) its usage in C
2) main applications it is currently using
3) unique features
4) its scope in embedded applciations etc
Hoping your co operation in this too.
__Kanu
Function Pointers are pointers, that is variables, which point to the address of a function.
Nice example here. Also this answer is a must read.

Resources