Programming thread

  • 🔧 Chat is under maintenance.
    🧊 New server is in an office waiting on a mexican.
  • Want to keep track of this thread?
    Accounts can bookmark posts, watch threads for updates, and jump back to where you stopped reading.
    Create account
What do people actually use these for btw? I've never encountered a use case where any kind of linked list was the best choice.
A pedagogical project where we have to handle two stacks, which aren't actually stacks but behave more like stack + queue. For that, a doubly linked list was the most natural solution. Though, I guess I could've used a circular buffer with start + size.
 
A pedagogical project where we have to handle two stacks, which aren't actually stacks but behave more like stack + queue. For that, a doubly linked list was the most natural solution. Though, I guess I could've used a circular buffer with start + size.
Yeah, a circular buffer is always a better representation for a deque. The main thing you'd want to use singly- or doubly-linked lists for is for some sort of structure that's mostly accessed sequentially but needs to insert (or delete) somewhere other than the end(s) frequently enough to make moving array elements around to make room for a new element (or fill in the hole left by a deleted element) prohibitively expensive. (Although you'd want to either use some sort of skip list or maybe have an index that uses a hash table or balanced tree to map keys onto pointers to list nodes to avoid linear search to find internal elements.)
 
Yeah, a circular buffer is always a better representation for a deque. The main thing you'd want to use singly- or doubly-linked lists for is for some sort of structure that's mostly accessed sequentially but needs to insert (or delete) somewhere other than the end(s) frequently enough to make moving array elements around to make room for a new element (or fill in the hole left by a deleted element) prohibitively expensive. (Although you'd want to either use some sort of skip list or maybe have an index that uses a hash table or balanced tree to map keys onto pointers to list nodes to avoid linear search to find internal elements.)
Yeah, i didnt think of such a simple solution at first, but at least my implementation now allows me to reset the order to the starting one with a singular array traversal. (which in my case is actually pretty handy)
 
The main thing you'd want to use singly- or doubly-linked lists for is for some sort of structure that's mostly accessed sequentially but needs to insert (or delete) somewhere other than the end(s) frequently enough to make moving array elements around to make room for a new element (or fill in the hole left by a deleted element) prohibitively expensive.
Yeah, that's the text book answer, but are there actually many cases like that where another solution wouldn't be better? Did you yourself ever encounter such a use case?
 
Yeah, that's the text book answer, but are there actually many cases like that where another solution wouldn't be better? Did you yourself ever encounter such a use case?
Never. In practice, the only thing I use linked lists for is maintaining a free list for a custom allocator, where I know I'm going to need another block of the same size again soon and don't want the overhead of free() potentially coalescing it with a buddy block repeatedly just for malloc() to split them apart again. Something like this:
C:
#include <stdlib.h>

/* not thread-safe, but threads are gay anyway */

struct RECORD {
    /* fields ... */
};

struct FREE_RECORD {
    struct FREE_RECORD *next;
};

static struct FREE_RECORD *freelist = NULL;
static size_t arena_size;

/* returns number of records allocated */
size_t init_RECORD_arena(size_t count) {
    struct RECORD *arena;
    struct FREE_RECORD *p;
    size_t i;
    arena_size = count = MAX(count, 1);
    arena = malloc(count * sizeof(struct RECORD));
    if (arena == NULL)
        return 0;
    for (i = 0; i < count; i++) {
        p = (struct FREE_RECORD *) &arena[i];
        /* add to head of free list */
        p->next = freelist;
        freelist = p;
    }
    return count;
}

struct SOME_RECORD *alloc_RECORD(void) {
    struct FREE_RECORD *p;
    if (freelist == NULL) {
        /* allocate another block of records */
        if (init_RECORD_arena(arena_size) == 0)
            return NULL;
    }
    /* pull from head of free list */
    p = freelist;
    freelist = freelist -> next;
    return (struct RECORD *) p;

}

void free_RECORD(struct RECORD *p) {
    struct FREE_RECORD *f = (struct FREE_RECORD *) p;
    if (p == NULL)
        return;
    /* add to head of free list */
    f->next = freelist;
    freelist = f;
}
 
The one thing I truly dislike about C is not how easy it is to shoot your leg, but it's the lack of a good standard library. If you need something simple like a vector, then you need to either roll your own implementation (more code to maintain), use an external library (yay more dependencies), or use alternatives like linked lists (which modern CPUs loathe). Inevitably I go back to C++ and have to deal with a load of other bullshit, but at least I get a good enough standard library.
 
Never. In practice, the only thing I use linked lists for is maintaining a free list for a custom allocator, where I know I'm going to need another block of the same size again soon and don't want the overhead of free() potentially coalescing it with a buddy block repeatedly just for malloc() to split them apart again. Something like this:
C:
I see, you essentially need a stack, but since you're managing unused memory you can use that instead. The memory non-locality is somewhat excused since, assuming allocated records will be used soon, they need to be loaded into the cache anyway. But after the order of slots has been shuffled around though by non-FILO record allocs/frees, consecutive record allocations would be all over the place despite large consecutive slices being available. Makes sense. You're leaking the arenas though.
 
The one thing I truly dislike about C is not how easy it is to shoot your leg, but it's the lack of a good standard library. If you need something simple like a vector, then you need to either roll your own implementation (more code to maintain), use an external library (yay more dependencies), or use alternatives like linked lists (which modern CPUs loathe). Inevitably I go back to C++ and have to deal with a load of other bullshit, but at least I get a good enough standard library.
The upside is that there are plenty of good libraries you can just paste into header files like stb as opposed to automating supply chain attacks with shit like npm
 
Yeah, that's the text book answer, but are there actually many cases like that where another solution wouldn't be better? Did you yourself ever encounter such a use case?
I have. The key is that you can maintain a link layer separated from an otherwise sorted structure and it can speed up retrieval in the same way as a SQL index. You have your array of text samples sorted in order of discovery, but you're also scoring those text samples. So you have a link layer per score that just has sibling links, a reference to the data and maybe the calculated score if you're doing something with thresholds. It's all very concurrency friendly, linked list insertions take up very little lock time compared to arrays and maintaining the meta sorts separately let's you push the expensive stuff, NLP and parsing, out of your main thread.
 
The key is that you can maintain a link layer separated from an otherwise sorted structure and it can speed up retrieval in the same way as a SQL index. You have your array of text samples sorted in order of discovery, but you're also scoring those text samples. So you have a link layer per score that just has sibling links, a reference to the data and maybe the calculated score if you're doing something with thresholds

Just throwing this out there for people to chew on:

What if, hear me out, this was how we did trees instead of pointers (except the rare cases you really need that) .
 
I have. The key is that you can maintain a link layer separated from an otherwise sorted structure and it can speed up retrieval in the same way as a SQL index. You have your array of text samples sorted in order of discovery, but you're also scoring those text samples. So you have a link layer per score that just has sibling links, a reference to the data and maybe the calculated score if you're doing something with thresholds. It's all very concurrency friendly, linked list insertions take up very little lock time compared to arrays and maintaining the meta sorts separately let's you push the expensive stuff, NLP and parsing, out of your main thread.
Excuse me if I'm being exceptional, but wouldn't a binary heap or something similar be much better for the score layer(s)? Especially for inserts since you don't need to traverse half the list for each individual insert.
 
What does everyone here think of unity builds for c/c++

In case you dont know, unity builds are including other c files directly instead of having the linker do it, after they get compiled to objects
 
What does everyone here think of unity builds for c/c++

In case you dont know, unity builds are including other c files directly instead of having the linker do it, after they get compiled to objects
forces you to recompile the entire project every time you make any change no matter how small, rather than only recompiling the translation unit where you actually changed something
the bigger your project the worse this will be
 
What does everyone here think of unity builds for c/c++

In case you dont know, unity builds are including other c files directly instead of having the linker do it, after they get compiled to objects
It's link-time optimization for roody-poos. I don't know why this sort of stuff is seeing a resurgence in C and C++ circles lately; header-only/single-file libraries are becoming fashionable again too despite being the single worst way to do static linking. Why yes, I would LOVE to fix 25 vendored versions of 2018-era zlib.
 
It's link-time optimization for roody-poos. I don't know why this sort of stuff is seeing a resurgence in C and C++ circles lately; header-only/single-file libraries are becoming fashionable again too despite being the single worst way to do static linking. Why yes, I would LOVE to fix 25 vendored versions of 2018-era zlib.
i will NOT accept stb-style lib slander this shit is way too convienient and nice
especially when you're still using ctags
 
i will NOT accept stb-style lib slander this shit is way too convienient and nice
especially when you're still using ctags
Unix weenies will yearn for the M-., build themselves an imitation out of duct tape, and then contort all processes around the duct tape instead of solving the actual issue plaguing them istg
 
I've tried it and it is rather inconvenient. It is highly likely that whoever wrote the library was not compiling it with the same -W* flags, so you'd have to either fix those warnings, or add annoying ifdef macros disabling warnings. A lot of fun both ways.

If you change the library itself it might get updated and if you want to use the newer version, you'd have to reimplement the fixes.

If the library uses macros instead of functions, and your approach is to disable warnings, then you'd have to put those warning disabling macros in every place where they are being used.

Another possible solution is to download the library, and just compile it with it's own flags as a separate target. It works, but what's the point then?

There is another annoyance when it comes to header-only libraries. How are you going to include the library in several files? A redifinition error is bound to happen in such a scenario if the library has any definitions in it. I've seen this approach:

Somewhere in the library:
#ifdef NIGGER_DEFINE_SOURCES
// definitions
#endif

Your first include directive:

#define NIGGER_DEFINE_SOURCES
#include <roody-poo.h>
#undef NIGGER_DEFINE_SOURCES

undef is used so that you don't add those definitions again the next time you include.
 
Last edited:
So, in K&R, there’s a challenge problem where you have to write a program that will read a C file and print everything except the comments. Not sure why they thought this was a good beginner problem but whatever. I almost had the right solution last time I tried this but now I think I have a better one! I need to manage states, the three major states are print, check, and skip. Each have mini states (print needs to include characters in “ and ‘; check needs to figure out if we start a comment, end a block comment, or print the \ followed by the next character) that must be checked. I believe using bit flags would be the right way to go:

C:
//enum for bit flags

enum State{
    PRINT = 1, // 01 any odd number means print
    CHECKCOM = 2, // 10 checks if we start a comment
    QUOTE = 3, // 11 ends with either " or ' depending on start
    LINECOM = 4, // 100 ends with newline and starts with //
    BLOCKCOM = 8, // 1000 ends with * followed by /
    BLOCKCHECK = 10 // 1010 checks if * is followed by /
};

Then use a switch statement to check states while going through a file and change it if needed. For the quote state we need to hold onto the starting single or double quotation mark and compare any quotation marks we hit to change. I think I figured it out, I will try it tonight and report results. Tell me if I’m missing something!
 
So, in K&R, there’s a challenge problem where you have to write a program that will read a C file and print everything except the comments. Not sure why they thought this was a good beginner problem but whatever.
Because it's relatively simple to take a token stream, and write everything that isn't //\n and /**/ as an output.
 
Back
Top Bottom