Comments (6)
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.
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.
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.
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.
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.
@midnqp please feel free to open issue if you have other questions! Cheers!
from sc.
Related Issues (20)
- A little problem about hashmap HOT 2
- # 📚 https://git.io/JvXDlhttps://git.io/JvXDl HOT 1
- 2023-01-15T13:51:12.2184877Z Use "--rerun-failed --output-on-failure" to re-run the failed cases verbosely. 2023-01-15T13:51:12.3359091Z
- 4
- test
- socket code does not compile on visual studio HOT 3
- Using this lib over FetchContent HOT 5
- Compile errors on Release HOT 7
- sc_str_append_fmt is trimming whitespaces at the end of the formated strings HOT 3
- Fix documentation in sc_list.h
- Add shrink option to sc_buf
- [error]“afunix.h”: No such file or directory sc_socket_test HOT 2
- What does this function? HOT 17
- EPOLLET in non-blocking sc_sock HOT 10
- Any plans on implementing AVL tree? HOT 5
- sc_array.h: error: implicit declaration of function 'memmove' HOT 3
- #if defined(_WIN32) || defined(_WIN64) is not enough to check for MSVC HOT 7
- sc_sock: WSAPoll is broken HOT 2
- sc_sock thread-safety HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sc.