Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Higher Abstraction Lib for C?

Tags:

c

Are there any higher abstraction libraries for C which helps doing basic stuff very easily without really worrying about memory/ pointers/ etc.

Ex: Python provides very higher abstraction than C. It can be concatenating strings, arrays etc. Simply, we got any lib in C which takes care of that job?

like image 677
Surya Avatar asked Sep 20 '12 05:09

Surya


People also ask

Are libraries an example of abstraction?

A library is a kind of abstraction: you don't have to know any of the details of how it's coded.

What is abstract in C programming?

Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.

How can C Support abstraction?

Yes. C has the base Data types , and C structs and typedefs allow the full range of data abstraction that other languages allow.


2 Answers

There are only a few purposes for programming in C. The main ones that come to mind are:

  1. You need a language that has a concept of storage duration, whereby you can write bounded-space or in-place algorithms that use memory that's already been obtained, and thereby have absolutely no failure cases.

  2. You want to take advantage of the code-size or performance advantages of C idioms that either aren't idiomatic or aren't expressible in other languages. Again, most of these end up having to do with working with data in-place rather than moving it through intermediate data structures or wrapping it in abstract containers.

If you don't have such needs or don't even know what they mean, you might consider whether you should really be using C. Bringing inefficient idioms like string concatenation, abstract container classes, or lazy memory management with you from other languages to C will negate most/all of the possible benefits of using C while offering you few or none of the benefits of a higher-level language.

like image 53
R.. GitHub STOP HELPING ICE Avatar answered Oct 22 '22 17:10

R.. GitHub STOP HELPING ICE


Not worrying about pointers is pretty tricky, as someone has to clean up the mess eventually, and C has neither a garbage collector nor destructors when stuff goes out of scope.

  • The Apache Portable Runtime (APR) makes some forms of memory management easier by using memory pools. That way you can free bunches of objects simultaneously. APR provides its own set of pool-based string routines, but these strings are still handled as char*, so you can't really avoid the pointers here. You can of course do a typedef char* string and use that name instead.
  • You can also get a garbage collector for C, which will take even more care about memory management. This library itself will only provide a garbage collector, little else. I know of no high-level C library which makes use of it and provides string operations and similar.

One important question you might want to ask yourself: why do you want to stick with C, if you intend to use features already provided by other languages like C++? Perhaps your requriements can also be met by some mixed-language programming, where you write part of your application in C++ and either keep existing code in C, or do library calls using the C calling convention. That way, you can write new code at a high level and still use good old C where it is appropriate.

Noone is forcing you to use a specific coding style just because you are using C++. It is perfectly all right to write a program which mostly looks like a C program and still uses the features and data types of C++ where you need them. You don't have to write object oriented stuff yourself, you can stick to simple functions calling one another, in the standard imperative paradigm. But it's still nice to know that you can use objects in those situations where they help make your code easier to write, read and maintain.

like image 4
MvG Avatar answered Oct 22 '22 16:10

MvG