DEV Community

Discussion on: How to create Objects and write it's Garbage Collector in C

Collapse
 
pentacular profile image
pentacular

The only purpose of a pointer is to store the address of a variable that's all.

The purpose of a pointer is to index into an array.
Variables are effectively stored in arrays of length one.
Pointer arithmetic is very fundamental to C. :)

The static keyword in C is a storage class specifier. The default storage class specifier in C is auto but can be explicitly set to static using this keyword.

static doesn't affect scope -- it affects storage or linkage.

Any running process in the RAM consists of at least these segments:

This may be how your system works, but it's not necessarily the case.

b.str = (char *)malloc(2*sizeof(char));

Why are you casting malloc?

b.str = malloc(2); is sufficient, as sizeof (char) must equal 1.

the data type of it is size_t which can store the maximum possible integer value allowable by the system

size_t can store the largest object size, which might be as small as 16k.

It is not related to the maximum integer value allowable by the system.

No, because in the source code we can increase the program break by using system calls like sbrk() or brk(),

Note that sbrk and brk are not part of C.

On the other hand, the variables having the const keyword are stored inside the read-only area. eg: const char* s = "hello, world", the "hello, world" string literal being defined by the constant keyword is stored in the read-only area, however, the pointer char* s is stored in the read-write area.

This is incorrect.

"hello, world" is a string literal, and attempts to modify it produce undefined behavior, which means that it may be stored in read-only memory if the system feels like it.

The string literal is not affected by the const modifier on the char *s declaration.

What const char *s means is that you may not modify *s, not that *s points at something that cannot be modified.

In some cases the compiler may be able to deduce that no legitimate non-const pointers can be produced to an object and may then decide to store those differently, but it isn't required, and is independent of the behavior of string literals, which are defined that way mostly to allow consolidation.

while(*(s)!='\0'){

Why are you writing *(s) rather than *s? this just makes it harder to read. It is sufficient to write while (*s) { ... } here.

We will be writing a precise garbage collector as it frees all the memory allocated using malloc or realloc.

Well, no -- you aren't -- there is no portable mechanism by which you can accomplish this.

//This function will be called before the execution of the main
[Function Prototype] attribute((constructor));
//This function will be called before returning from main
[Function Prototype] attribute((destructor));

These are not part of C -- these are extensions provided by GCC.

So, now we don't need to worry about the Garbage Collector, it will automatically start monitoring and free up space at the end.

A garbage collector that only collects garbage when there is nothing except for garbage is not really a garbage collector.

It's also redundant since C does the same thing when main returns. :)

Collapse
 
saviourcode profile image
Sourabh Choure

First of all, I would like to express my immense gratitude and thanks as much as the size_t data type can hold for reading my first ever article, (which I posted publicly). It means a lot to me that someone took their precious time out to read my article which we might say is of no use.
As by reading only you might have deduced, I am still a beginner in the field of programming and have to learn a lot many things (that too correctly/unlearn bad practices in programming). The only excuse which I can give is, I never tried my level best and blamed my Non-CS background (Electronics Engineering) for it, the whole time (which of course is absolutely wrong, I realized this a few months ago when I started to write this article).
The points which you have given are like stepping stones for me and I will inspect each and every issue meticulously and do in-depth research. I won't let your time spent in finding bugs in this article go in vain, I will update this article with the right information as soon as I get them all resolved.
Again, thanks a lot for spending your time, I hope to see more such valuable conversations in future as well.

Collapse
 
pentacular profile image
pentacular

You are welcome. :)