Coder Social home page Coder Social logo

Comments (6)

tezc avatar tezc commented on May 12, 2024

Hi @midnqp, glad this repo is being helpful to you.

Just to be clear, you wonder why it passes double pointer e.g. char** instead of char* right? Reason is that some functions might need to change the value of the pointer.

For example, here: see the link
This function makes another allocation and updates the value of the pointer. To do that, you need to get char**.

So, you need to get "pointer to pointer" if you need allocate new memory in your function. In other cases, as in your example above, if you just want to change its content (its characters) it is not necessary.

One exceptional case is sc_str_destroy(). You may use char* as it just calls free() for the pointer. But just to make it safe to call twice, this function sets pointer to NULL. See: link

So, user does not have to remember if it called sc_str_destroy() before, it will be safe to call it multiple times.

e.g:

char *c = sc_str_create("hello");
sc_str_destroy(&c); // This call will set c = NULL 
...
sc_str_destroy(&c); // Safe to call it twice.

// Otherwise you would need to write 
if (c != NULL)  {
    sc_str_destroy(c);
    c = NULL;
}
// So, it saves you adding these NULL if checks in your application.

Let me know if this is clear? Cheers!

from sc.

midnqp avatar midnqp commented on May 12, 2024

Wow, this is really simple and clear as a crystal ✨ Thanks for your time in explaining this to me! I actually did notice you mention that everytime we use set, create, append a new pointer is created. Perfect!

However, we could extend the previous pointer's memory. But.. oh, realloc() still malloc()s under the hood. So we don't have a choice other than to create a pointer everytime.

Wow, this library is well-built.

from sc.

tezc avatar tezc commented on May 12, 2024

Be careful with realloc, it is not guaranteed to return the same value even when you call it to shrink the memory. I've seen that is happening, I had to debug a painful issue :)

So, you don't have any other way, you have to get char** if you want to (potentially) update the pointer.

from sc.

midnqp avatar midnqp commented on May 12, 2024

One last thing: kindly elaborate a tiny bit on this please? "it is not guaranteed to return the same value" - I understand realloc() returns a new pointer, but not the same value?

from sc.

tezc avatar tezc commented on May 12, 2024

I understand realloc() returns a new pointer, but not the same value?

My point is realloc() may return same value on some calls and it may return another value on other calls. Because allocator (malloc(), realloc() implementation) is free to return whatever it wants.

void *x = malloc(10);

// y points to same memory with x
void *y = x;

// Extend memory allocation. After realloc(), will y be equal to x? 
y =  realloc(y, 20);

 
// Assume that x points to a memory which has already 20 bytes empty space, 
// then allocator can mark your allocation as 20 bytes and it can return the same pointer. (y == x);
//
// If there is not enough space (assume x points to 10 bytes empty space and there are used bytes 
// by someone else just after that) , allocator has to return another pointer to you (y != x)

So, my point is we cannot say realloc() will return same or different value. It totally depends on allocator implementation.
Your interpretation is correct, if we assume realloc() will return new pointer all the time, we won't make mistakes but as you can see above, in reality, that might not be true :)

Let me know if it is clear.

from sc.

tezc avatar tezc commented on May 12, 2024

@midnqp please feel free to open issue if you have other questions! Cheers!

from sc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.