Skepdick wrote: ↑Thu Jun 26, 2025 8:50 am
It's a bit like interacting with an API without seeing the implementation.
Especially during the initial stage of development, you can use a remote API or a local library without even looking at the source code. The most important information at that point are one or more examples of how to use a particular function. The source code itself will not tell you that. With just the source code, you won't even get properly started with the task of using an API. The source code only matters when you get confronted with unexpected behavior, because only the source code can tell you why exactly things go wrong for your particular input data, especially, if the error message does not convey much information, or if there is not even an error message because the erratic behavior is actually considered fine; which obviously also happens.
For example, how to use the libC strlen() function:
Code: Select all
#include <stdio.h>
#include <string.h>
int main() {
char my_string[] = "Hello";
print("%z\n", strlen(my_string));
return 0;
}
// Prints "5"
That is the kind of examples that get you started with the API. The source code of strlen() does not matter and will probably never matter because this simple function is unlikely to behave unexpectedly:
Code: Select all
https://codebrowser.dev/glibc/glibc/string/strlen.c.html
/* Copyright (C) 1991-2024 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <libc-pointer-arith.h>
#include <string-fzb.h>
#include <string-fzc.h>
#include <string-fzi.h>
#include <string-shift.h>
#include <string.h>
#ifdef STRLEN
# define __strlen STRLEN
#endif
/* Return the length of the null-terminated string STR. Scan for
the null terminator quickly by testing four bytes at a time. */
size_t
__strlen (const char *str)
{
/* Align pointer to sizeof op_t. */
const uintptr_t s_int = (uintptr_t) str;
const op_t *word_ptr = (const op_t*) PTR_ALIGN_DOWN (str, sizeof (op_t));
op_t word = *word_ptr;
find_t mask = shift_find (find_zero_all (word),
s_int);
if (mask != 0)
return index_first (
mask);
do
word = *++word_ptr;
while (! has_zero (
word));
return ((const char *) word_ptr) + index_first_zero (
word) - str;
}
#ifndef STRLEN
weak_alias (__strlen, strlen)
libc_hidden_builtin_def (strlen)
#endif
The implementation of the strlen() function yields a lot of mental overhead and noise that will probably not help you using the function. You are almost surely better off looking at a second example, if the first example was not clarifying enough.
Of course, the source code must still be available, as a matter of development policy. Sooner or later, with intensive use, you will indeed likely run into a situation that can only be solved by stepping through the source code. However, that is not how it typically starts.