NO

Author Topic: Container libraries  (Read 2398 times)

Offline CandCPlusPlus

  • Member
  • *
  • Posts: 57
Container libraries
« on: August 26, 2022, 10:17:20 PM »
I'm rather curious about what people on here typically use in C for containers and I'm wondering if there are any simple and lightweight open source container libraries for C that you can use for C++ like containers

Personally, when I use containers in C++, it seems like I use std::vector 90% of time, maybe std::map 8% or 9% of time and everything else I only use in niche cases. I have read about creating dynamic arrays and creating functions to manipulate them in C and it looks moderately easy to accomplish. However, I tend to think that there's always the chance of bugs when creating it yourself, especially if it's more sophisticated vs. simply including possibly very well tested containers/container libraries in your project.
« Last Edit: August 27, 2022, 07:22:15 PM by CandCPlusPlus »

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 175
    • Bcx Basic to C/C++ Translator
Re: Container libraries
« Reply #1 on: August 27, 2022, 02:54:23 AM »
I've never used it and I don't know whether it depends on any Lcc-Win32 extensions  that may not exist in Pelles C but Jacob Navia shared a containers library about 10 years ago that could be a jumping off point for you.

https://code.google.com/archive/p/ccl/downloads

Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Container libraries
« Reply #2 on: August 27, 2022, 09:37:12 AM »
problems with ccl source files
bloom.c
list.c

May the source be with you

Offline John Z

  • Member
  • *
  • Posts: 790
Re: Container libraries
« Reply #3 on: August 27, 2022, 11:16:31 AM »
Hi CandCPlusPlus,

Searching the web there are several to choose from, here is a possibly useful link too

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1625.pdf

To C or not to C++ that is the question . . .

I think one is best served learning C without trying to turn it into C++ at the outset.  After a good foundation in C is acquired then adding C++ abstractions back to the toolkit is a better approach  IMO.

Having said the above here is a another possibly useful link to a lib with frequent mentions

https://docs.gtk.org/glib/ 

John Z

Offline CandCPlusPlus

  • Member
  • *
  • Posts: 57
Re: Container libraries
« Reply #4 on: August 28, 2022, 06:11:04 PM »
I found several rather promising libraries on github, especially the STC one. I do also agree to a certain degree with John Z. I probably should experiment with the native C functions more and learn those well. It's probably wise to do so.

The STC one even has a wrapper for strings. It definitely makes C have C++ like containers and such. O.O With this library being fully C99 compliant it should, in theory, work in Pelles C.

I'll post the links for everyone else.
STC - Smart Template Containers for C
https://github.com/tylov/STC

Containers
https://github.com/bkthomps/Containers

I think one is best served learning C without trying to turn it into C++ at the outset.  After a good foundation in C is acquired then adding C++ abstractions back to the toolkit is a better approach  IMO.
« Last Edit: August 28, 2022, 06:47:32 PM by CandCPlusPlus »

Offline CandCPlusPlus

  • Member
  • *
  • Posts: 57
Re: Container libraries
« Reply #5 on: October 26, 2022, 12:43:17 AM »
The STC library is the most useful with strings for me. It's moderately easy to create your own containers in C but the string library that's included with C is rather clunky and you have to be mindful to avoid buffer overflows. I want to create my own string library to get around that. In fact, I may post it on here and it would be a way of learning C for me. My personal string library would be more C-style than STC

Offline John Z

  • Member
  • *
  • Posts: 790
Re: Container libraries
« Reply #6 on: October 26, 2022, 11:06:39 AM »
Sounds like a good project.  I don't think many C++ fans visit Pelles C.  C does have potential pitfalls that a diligent programmer should always handle.  Newer C also has 'safe'er string operations too.  C can be entirely safe if the time is put in to ensure appropriate checks are done. 

John Z

Offline CandCPlusPlus

  • Member
  • *
  • Posts: 57
Re: Container libraries
« Reply #7 on: December 03, 2022, 08:36:20 PM »
When I get around to it, I may create a string library for C that tracks the size of the string with a variable in a struct and I will create various functions that manipulate the struct in a C-style manner. The functions will use the size being tracked in the struct to prevent buffer overflows. I do want to learn various forms of testing to test this custom library as well.

Offline John Z

  • Member
  • *
  • Posts: 790
Re: Container libraries
« Reply #8 on: December 06, 2022, 01:33:06 PM »
When I get around to it, I may ......
;)

Will the library be 'automagically' be tracking the number of characters, the number of bytes, or both for the string?
Undoubtedly one should use all of the memory safe and string safe C functions to create a string safe library of manipulation functions. 

Would you include new string functions?  One that seem to be missing is sub-string replace.
Where a search for string from an arbitrary position looks for a sub-string and if found replaces it with a replacement string of any arbitrary length including length 0 which would remove the sub-string. The return value would be either -1 if sub-string is not found or x where x is character position 1 past the end of a last located sub-string.  There could also be a max string input since the replacement could be much larger than the sub-string.

//i = start character, ptr = pointer to string to manipulate, sub-string, replacement string, max size
i = 0;
i = strreplace( i, ptr, "Abc", "ABCD", 50);  and
i = wcsreplace(i ptr, L"AbC", L"DEFGH",50);

John Z

Offline cosh

  • Member
  • *
  • Posts: 25
Re: Container libraries
« Reply #9 on: January 31, 2023, 03:52:40 PM »
Hi, you may try this library: https://github.com/coshcage/StoneValley for containers in c.
This is a library that gathers many data structures and algorithms together and you could use it directly.
The README file of this library may guide you to understand this library.
Hope you could enjoy it.
Regards.