I'm a programming novice taking some online courses. The courses have moved off c, but i want to keep what I've learned sharp, so I'm writing an amateur string library for personal use. I made a linked list based function (properly frees all nodes after the linked list is transcribed into an array) that returns a pointer to a character array for dynamic string input and utilization back in main(). The array in main does need to be freed as well; put a reminder in the function docstring.Are there any known safety edge cases for a strategy like this? Does it generally make more sense to save all of the work and just initialize arrays with way more space than needed versus allowing for completely dynamic creation? It just annoys me that char arrays need to be fixed size at compile time, and I wanted to see if I could work around that for increased safety and interactivity.
None that I'm aware of. But why don't you just use realloc and increase the buffer 16>32>64? no need for a ll
>but i want to keep what I've learned sharpthen do something that is actually useful for you. solve a problem that doesn't have solution, or the solutions available are not to your liking.that's how you both stay sharp, and discover things you didn't know before. doing sophomore-level shit is almost the least useful pathway imaginable.getting that out of the way, i have no idea how you got from strings to linked lists, and what is it that you're trying to do specifically. but whatever it is, i have a hunch that it's wrong and a symptom of the general tendency to force every round problem into the square basic data structures and shit paradigms beginners get taught.
>>106511175just look at how std::vector in c++ works. It has a starting allocation size, but it can be expanded by reallocating and copying if you run out of space upon trying to insert, the max size available to the user is kept so as to keep the illusion of it being smaller than it is. When it actually has to bump up against the limit of what was allocated, then you can worry about the time it takes to insert.
>>106511289kek you sound like the biggest nocoder faggot on this board. Leave forever
>>106511175Agree with the other posters, using a linked list for string input sounds like an extremely overdesigned solutionAs for freeing the final array, there isn't really an proper way to do it. The most intuitive, to me, way to signal that you need the user to free the pointer when he'a done with it, is to make the user provide the pointer in the first place. so something likevoid getString(char** destination)
>>106511256>>106511291For some reason it irks my very mild autism to have to allocate anything at compile time. Weird right?>>106511289I'm pretty new.I was annoyed about having initialize arrays with a size with fgets() (plus fiddle with '/n'), or risking overflow with scanf(), so I invented (reinvented I'm sure) a way of allowing array size being defined at run time by the user. I know it's not much, but it was an itch I wanted to scratch.
>>106511175Sharp ay?
>>106511324The free is hardcoded by me, but the array size is dynamically generated based upon user input. I know the whole thing is retarded, but for whatever reason char arrays in c have become my "toy trains" from the first introduction to them. They both fascinate me, and pass me off.
Don't create your own library. Use these https://learn.microsoft.com/en-us/cpp/c-runtime-library/string-manipulation-crt?view=msvc-170Use ones with security enhancements such as https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-170How to convert between various string types:https://learn.microsoft.com/en-us/cpp/text/how-to-convert-between-various-string-types?view=msvc-170The strings types that are covered include char *, wchar_t*, _bstr_t, CComBSTR, CString, basic_string, and System.String.In all cases, a copy of the string is made when converted to the new type. Any changes made to the new string won't affect the original string, and vice versa.
>>106511386I'm a lowly hobbyist, and the off chance I ever did get involved with actual production code, I would never use my hack libraries in the real world. Just wanted to play around with it and see if it was doable. It's a bit of a Rube Goldberg for sure. It works. It doesn't leak memory or have any ub. Can't overrun or result in out of bounds. Just not sure if there's a way to input malicious characters that make it through getchar(), into the nodes, and get transcribed to the returned array.
>>106511352Well in case you haven't noticed, it is a compiled language and it is more code efficient and statically type correct to determine allocation size at compile time. You know the size at compile time? Okay now you can do compile time bounds checking and when you lower the code to assembly you can use an allocation instruction with an immediate value rather than telling the cpu to wait on a register to become available. You also have a compile time source of how large the function call stack will be. Whether or not programmer likes the behaviour is why abstractions exist.By choosing an allocation at compile time you are only paying for the behaviour you ask for.
>>106511175Post the code.Reading English confuses me.
>>106511444So, like the other guys were saying realloc is best practice. Alright. At least i got a little more practice rudimentary data structures. Still at the point where traversing nodes isn't quite muscle memory.Thanks all!
>>106511468Lol. My power went out. In the middle of refactoring this mini project actually. That's the only reason I'm here phone posting.
>>106511481(nta)theres several ways you could deal with this problemyou could "over-allocate" (actually over commit), as in allocate a lot of available memory, and play on the fact that said memory isnt "occupied" until you write to itofc theres realloc, but one could write a version thereof with mmap directlybut these are advanced methodsat your level, i think you should look into what an arena is.you create a linked listand each node's data is an allocated region of memorythen you distribute pointers from said memory regions until you run out.when you do, you allocate a new node, new region of memoryi think its the next logical step after learning linked lists
>>106512019Thank you. I'll take a look at both. The mmap looks a bit intimidating at first glance, but I'm going to have to get to it eventually. I definitely need to explore errno.h before going that deep, as it wasn't even covered in any of the materials and I'm defensive coding in the clunkiest possible ways.
>>106512178What kind of strings? how big do you expect them to be? you can just use brk to do you own simple version of realloc you don't need mmap
>>106512374I guess I was just aiming for seeing if I could replicate a total python style dynamic at run time input function. I guess I succeeded in a simple hobby way, but it's not a very c like solution at the end of the day. Needless complexity. Array size initializing at compile with realloc as/ if needed would've been way briefed. I'm not really building this for any application. Just wanted to "fix" something that I didn't like now that I have the basic skills to do it. I'm in my Dunning-Kruger "peek of mount stupid" phase.
>>106512439Lol *peak
>>106511175https://en.cppreference.com/w/cpp/container/vector.htmlhttps://en.cppreference.com/w/cpp/string/basic_string.htmlstd::wstring
>>106512439he who seeks for methods without having a definite problem in mind seeks for the most part in vain.
>>106511175UTF8 only compatible converterhttps://github.com/yhirose/cpp-unicodelib
>>106512474Thompson just wanted to fuck around with rad shit in the computer lab. Bell Labs had to basically gang stalk him just to get him to begrudgingly take their money to fuck around with it on the clock. I'm seeking enjoyment. Like reading a good book or bird watching or whatever gay shit.
>>106512178yeah, ofcmmap is the last thing you should be thinking aboutimplement arenas when youre done with your current project insteadyou need 3 functions for these:one to create an arenaone to destroy oneand a function to get a pointer, to abstract the internalsthen your whole memory management becomes:-create arena-do your thing-destroy arenaits not only a programming exercise, its also an organizational measure for your code one which you will be using all throughout your c programming "careeer"
>>106512508Thanks for the pointers broseph.
>>106511352>For some reason it irks my very mild autism to have to allocate anything at compile time. Weird right?No, not at all, I had that too, you felt it less elegant to reserve more than you potentially needed, but dynamic allocation comes with its downsides as well.2MB stack buffer/cache? Yeah, I'm reserving that nowadays, if I can feel I can justify it (e.g. a program that parses gigabytes of disk data from SSDs).Also, all that processing power to know the exact length of space a string required vs. just allocating enough... it gets complicated not with shorter strings, but if you need to allocate more than you reserve.
>>106511386Hi, <MICROSOFT-AI>Definitely create your own libraries if you are serious about C, publishing and using them for anything other than learning is something that rightfully should be questioned, but most certainly not by Microsoft.
>>106512515happy to help
>>106512518Yeah. The thread is helping me see the error in thinking. I'm introducing actual time complexity heavy computations and allocating, albeit temporarily, 8 byte pointers per 1 byte character, just to avoid a quick size initialization and quick realloc if needed.Was a fun quick exercise for a beginner though, and fun actually talking tech on a board i only used to be able to lurk.
>>106511352>I invented (reinvented I'm sure) a way of allowing array size being defined at run time by the user. use getline
There's no good way to write a library if you don't even know what you want. Either figure that out or start flexing your creative muscles and get to rewriting. At the very least do some stringbased leetcodesThe current c string library is the way it is because lots of people had problems with it, you get a veryPython dynamic runtime was a solution to a problem of making development more frictionless. It's a failed solution of course because you spend far more time looking for bugs than you do writing code. Luckly people have written transpilers to solve the python problemOf course the reason python failed is that it actually wasn't made trying to solve that problem. It was made as ABC and shell replacement, the general problem a shell tries to solve is "how can users use effectively use a computer system". While the general problem ABC tried to solve was teaching students how to program. From this effective fusion the most powerful baby duck syndrome language was born that will forever hamstring developers who can't into shell
>>106512569>8 byte pointers per 1 byte characterHah, I think I can figure out what you were going for...I had the hardest time understanding hashmaps; not how they work or how they are good/better at certain things, but if I use e.g. MD5 hashsums as the key, I'd need to reserve 2^32=4GiB of memory just for one hashmap (and you either follow me with that or you don't).The key is that hashmaps are a mix of these combinations, "hash-LUTs" and algorithms to pick the right LUT, so as to keep the memory requirements sane.Diffie-Hellmann key exchange is another thing, once you wrap your head around it, you understand E2E.https://www.youtube.com/watch?v=g8vHhgh6oM0Great voice acting and adaptation, it's a pity there were only two seasons.
>>106512569in general, most programmers go through a phase where they want to write everything in a generic, reusable way that handles all edge cases. write it once and reuse it everywhere. that's great for libraries that can't anticipate how they will be used, so it's a great skill to have to be able to think through all of this. but when writing applications, you generally know all the usage sites, and solving problems you don't actually have leads to a lot of unnecessary code. the amount of bugs increases with the amount of code, so it's generally best to only have the minimum amount of code that's required to solve the problem. that way, you can easily change it when requirements change and don't have to guess "was this code here for an actual reason or just future proofing? well, better drag it along so i don't break anything" which leads to bloat
>>106512624>MD5 hashsums as the key, I'd need to reserve 2^32=4GiBMy bad, it's 2^128 (and then multiplied by the width of a pointer) GB of memory, as MD5 is 128 bits, not 32.Hashmaps are constant-look-up-time, it hashes the index/key, and the result should ideally be a memory address. The width of the hash result is the width of the memory required.What can be done is breaking it down into dynamically allocated smaller componentents, e.g. byte-sized chunks of 8-bits and then create a 16-array pointer list to follow to get the data, when using a 128-bit hashsum, not have to preallocate 2^128GB of data.But when you're at that level, where you can't even explain the problem you're having to somebody else, because they just will never understand the problem to begin with/intrinsically, the only advice I can give you is too keep a thick skin and never lose it!
>>106511175ALL memory allocation should be made in blocks, see arena allocator.