Garbage collection in a C compiled language [closed] - c

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 6 years ago.
Improve this question
Let's say I have a garbage collected language that is compiled to C and through that to assembly. Then, how garbage collection works when it is compiled down to C? Does it become fully deterministic? Or is it contained in the resulting program as another program that runs periodically and collects garbage? This is probably a very easy, if not silly, question but I wanted some clarifications.

Even though it's compiling to C, such implementations typically link in a runtime library for the original language. That library contains the garbage collector for the higher-level language data. And the data structures used to represent the original language's data in C includes additional fields needed by the garbage collector.
Another technique they may use is conservative garbage collection.

One way to do something similar in a compiled language is done in iOS with ARC reference counting. It's technically not garbage collection but something similar. You would need to periodically search your programs memory for addresses that had been allocated pointing to the heap to see if it was ok to free the memory or not.

Bohem gc exists; however if you have an integer in the right range to be a pointer to a dead object entire graphs can leak. http://hboehm.info/gc/ In all a poor choice.

Related

C function modification [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 2 years ago.
Improve this question
I had some fun with imitating OOP in C, but I got somewhat discouraged when I understood that I'll have to call member methods like obj->method(obj, ...). Then I thought about cloning and modifying functions at runtime. Can I implement a function like strdup but for functions using a simple parser to identify the return opcode to stop copying and then modify a value in the function to point to the object the member method refers to so that I can use just obj->method(...)?
No, at least from what it sounds like you are asking, which is to modify functions at run-time. Modifying functions at run-time is possible but is difficult and requires considerable knowledge (and is system-specific). However, you seem to be asking to be able to execute a function and modify the function so that it does something with the object it is associated with. However, by the time the function is executing, there is generally no information about that object available: In a call like obj->method(…), there is generally no reference to obj included in the arguments. So even if you could modify the function at run-time, it does not have the information needed to do the job you want.
There are ways to do it at compile-time. That is how C++ developed. If that is a feature you want, the best approach is to use C++.

Loading characters from array in mips [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When you load character from an array in mip does the data still exist at that position in the array ? if not, how can you loop thru the array and get each character within the array ? thanks (:
Though your question seem silly, it is actually a very legitimate question!
Form an outside perspective modern memories have a non-destructive readout.
This means that reading a memory location doesn't destroy the data held there.
So reading from an array won't destroy the item read.
Out of curiosity it is funny to note that internally, depending on the memory technology, reading may be a destructive operation (the common DRAM and the old Magnetic core memory are an example1) and that there exists (and existed) destructive memories.
MIPS could run in a system with destructive readout, that would be tricky however since MIPS is a Von Neumann architecture, instructions are read from the same memory where data are.
So reading an instruction would also destroy it.
Though one can arrange a mixed system where code is run from a non destructive memory and data is in a destructive one, such configuration is so unusual that you can safely assume that it wont never happen.
1 Read-only memory like ROM, PROM and in general non-volatile memories have non destructive reading (so do Flash ROMs). In general memory that stores "charges" have destructive readouts.

What are some modern C idioms/guidelines to prevent memory related bugs? [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 6 years ago.
Improve this question
In the past few years, I can see a lot of C++ best practices recommended everywhere. The C++ guidelines aren't applicable to C even to a smaller degree. Particularly for people coming from a relatively high level language like C++ or Java (like myself), programming in C seems very dangerous.
I know that C is too low level a language to make any guarantees, but is there anything I must be careful of while writing C code so that I can minimize the chances of memory related bugs?
There are many techniques reffered to as defensive programming, too many to list here. For instance, you may adopt to always set pointer to NULL, when you free the allocated memory:
free(p);
p = NULL;
This avoids leaving p as a dangling pointer. The one obvious benefit is that if one calls free again on p, it would not do anything, so you potentially avoid the double free issue.
The second method is to use numerous tools that may help you track the ubiqutous mistakes such as buffer overflow, just to mention Valgrind and AddressSanitizer.

What is the advantage of using pointers in C? [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 8 years ago.
Improve this question
Why do we use pointers in C Programming?
In general pointers are able to access the address where the int/float/char etc... is stored.
Are there any other uses?
It depends on what you try to achieve:
you can change the value of a variable inside a function
you can pass a struct to a function without having to copy all its fields - think of a function that receives a struct.
you can point to a specific variable/struct and point to it from other structs
and many other advantages (advantages is purpose dependant and it depends on whats your program is doing).
Pointers are quite basic C and there is a lot of material online you should get yourself familiar with them and the advantages will pop up themselves.
The reason is that pointers are used to bodge into C some vital features which are missing from the original language: arrays, strings, & writeable function parameters. They can also be used to optimize a program to run faster or use less memory that it would otherwise. A few tasks these days, such as programming microcontrollers, still need this.

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.

Resources