Is there a "C with classes" language that is not C++? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 months ago.
Improve this question
I'm looking for some kind of C dialect that is as minimalistic as C but has built-in classes support. So I can (and encouraged to) use macros, pointers to arrays and manual memory management but also create classes, add fields and member functions to them etc. This question appeared when I tried to implement some kind of OOP in C and typedef struct and function pointers do something similar to what I want, but "member functions" require to manually pass a pointer to the object as a parameter to them, and that's not what I want to do. I know that I can just write on C++ as on "C with classes" and I would, however C++ encourages a different programming style and I'm curious if there is something that is exactly what I want.
I was searching for "C with classes" but I've only seen C++ in results, so I expect that the answer is "just use C++" and I'm OK with that, but I'm just curious.

C++ encourages a different programming style
You can write C++ in whatever style you like. Just choose not to use the features (and libraries) that don't suit your C-with-classes aesthetic.
"C with classes" was originally compiled to C by Cfront, but that's extremely dead AFAIK.
I doubt there's much demand for a resurrected Cfront when simply choosing a subset of C++, and using a current C++ compiler, already does everything you actually require.
FWIW I have written object-oriented C in the past, and manually passing this isn't that much of a burden. Even in Python you have to declare the self parameter explicitly, and nobody seems upset about that. Having to pass it in explicitly as well isn't so bad.

Related

Why does ISO/IEC 9899 not standardize the definitions of the functions in the C standard library? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
ISO:IEC 9899 standardizes the prototypes of the functions of the C standard library and describes their behavior. It specifies the identifier, the return type and the parameter(s) with its matching type(s) of a certain C standard function.
But why it does not specify the definitions - (the core how the specific functions actually do work)?
Why can a C standard library function X differ in its actual source code between f.e. the gcc compiler suite on Linux (GNU C Library), clang suite on macOS and the core system dynamic libraries for Microsoft Visual C++ on Windows? Why is it dependent upon the implementation, the operation system and the relative compiler design?
Edit:
I know the question seems bad for the most of yours at the first sight but it has definitely a right to get answered, since I don´t know the reason for that yet.
I do not suggest that the ISO shall standardize the definitions because the question was closed as opinion-based - don´t get me wrong. I just ask why are things that way and want to learn from your knowledge and experience.
Take strlen for example. If the ISO C standard standardized the definition of this function, it would probably look like this:
size_t strlen(char *s)
{
size_t l = 0;
while(s[l]) l++;
return l;
}
This is highly inefficient. The GNU C library has implementations written in assembly and C that are very fast, but aren't portable.
Some functions may be impossible to standardize. For example, how would it define putchar, vfprintf, and fwrite? What about assembly functions like longjmp? Or "macros" like setjmp?
Other definitions may be exploited. For example, if the Standard C committee standardizes memcpy, two things would happen:
people can abuse the copy order, and
existing implementations would be invalidated.

Where is malloc defined in code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm learning about malloc and understand what it's used for. I'm curious if malloc is written in C. If so, I'd like to see the code that defines it. Anyone know where I can find the definition of malloc?
EDIT:
I'm aware of this link (and many like it on google) http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html but that's not the code definition of malloc. I'm looking for the source file (if it exists) where malloc is defined. Something that looks like this
void *malloc(size_t size) {
// code for how malloc is implemented
}
I recommend you to check the GNU C library: glibc.
http://www.gnu.org/software/libc/download.html
You can read the code there. In the malloc folder.
malloc() is defined in Standard Library, as far as all unix flavors are concerned, and probably more, since Standard Library belongs to the C library. Whichever system has a C library and C API implemented, one could at least expect it to have a malloc.
Here are few more (beside above mentioned GNU) links with source code:
NET BSD malloc
OSX malloc
Some legacy unix systems (IRIX for example), beside having Standard Library malloc() also used to have a fast libmalloc implementation.
Please, also take a look at this SO post.
Code for malloc and free
https://code.woboq.org/userspace/glibc/malloc/malloc.c.html
https://fossies.org/dox/glibc-2.23/malloc_8c_source.html
Above are link for code of malloc.
http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
You can find many of such definitions. You should try to google it first :)

How do programmers make a programming language on top of C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am interested in making my own programming language on top of C, but I have no
idea where to start.
So, I researched, this caught my attention:
A lot of languages are C-based.
Popular programming languages like C++ and Objective-C, and possibly C# and Java are all built on top of C. (Not to mention Python)
How did C++ and Objective-C creators managed to make a new language that is C based, but add object oriented programming concept added?
C based does not neccesarally mean that it's interoperable with c. It can just mean, that it uses the same paradigm or similar syntax like egyptian bracets.
Java is C based but also borrows from Smalltalk and it cannot run any of C code. C++ can call C functions because it's compiler produces the same binary code as C code. Once you have linked their binaries they become interoperable. Pythons implementation CPython does not produce any binaries first, it's an interpreted implementation, meaning it is run by an interpreter Programm which holds the whole parsed syntax tree in memory. And because the Interpreter is just a C programm itself, it can call other C functions. So there is no such thing like adding features in top of C. Those are different languages, with differen compilers/interpreters and grammatics which just borrow some of the grammatical rules from c.
The easiest way to start creating an own language, is by using a parser generator like antlr. And i would start creating an interpreter and not a binary compiler. Perhaps a compiler, which compiles to c. Knowledge about language grammatics is essential.

What to consider when writing portable C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm starting a pet project, aimed at portability. It's a simple platform game and i'm planning to compile this to many different platforms with different toolchains. The video/input/system stuff is already abstracted by having multiple video drivers, which i include based on ifdef's around my code. Each platform makefile has a define of the platform (DC, NDS, PSP, etc.) and then i include the proper video drivers, which are C files with various functions called around my code.
However, i'm not sure about other caveats of portable applications in C. Should i redefine stuff from the stdlib? u8, u16, u32 and s8, s16, s32, etc? What knowledge can you share with me for this project?
A portable program is a program that:
only uses the features of the language and library defined in the C Standard
does not invoke undefined behavior
does not depend on unspecified or implementation defined behavior.
For a list of undefined, unspecified and implementation defined behaviors, you can go the C Standard C11, Appendix J (Portability issues).
Writing in C is more or less portable as long as you make no suppositions about the sizes of your types and the pointers you use to access them. I personally prefer using the types defined in stdint.h (http://pubs.opengroup.org/onlinepubs/7999959899/basedefs/stdint.h.html) - this defines like uint8_t, uint16_t ... - but feel free to research more alternatives, such as types.h (from POSIX Standard: 2.6 Primitive System Data Types) which defines them as u_int8_t etc ...
Possibly, you will end up at the end defining your own types based on what you managed to mangle together from the various sources found on the net ... such as: game_int_16 ,game_int_32 ...

Automatic create function block diagram from ansi c code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Does anyone know a tool that is able to create "function diagrams" from ansi c code?
By "function diagram" I mean a chart that represents an overview of files, functions and their relations. I imagine it to be something like a circut diagram.
Eg. if have the following code:
//MyProgram.c
int main(int argc, char** argv)
{
Foo();
Bar();
return 0;
}
//Slave.h
void Foo();
void Bar();
The chart would be something like the following picture:
Does it have an official name? Dependency diagram, perhaps?
I've looked at bit on Doxygen. But that one clearly states that:
Doxygen has built-in support to generate inheritance diagrams for C++ classes.
Same thing with many UML tools. I don't have any classes. Although my c files may come close.
What you refers is called Call Graph.
There's a list of tools to generate them: http://en.wikipedia.org/wiki/Call_graph#Software
There couple of software which are not free anymore e.g www.scitools.com which has a tool called Understand C. This I'm sure will do what you are looking for, there are other tools like BOUML ( this is free tool). Other than this the list provided by qiao is good reference.
I think I've found the origin of my idea. Displaying code as "components with connected input/output" is a LabVIEW concept. (I'm not a complete nutter for seeking out this kind diagram then ;)
However as far as I know, LabVIEW doesn't do what I want it to. LabVIEW is for building and connecting functionality. Widely used by hw/fw people. (Which are also my target audience.) It will not reverse engineer code into diagrams.
Strange that these diagrams doesn't really exists. It seems to me that people such as embedded programmers, ansi c, low level, hw etc. would love them.

Resources