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.
Related
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.
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
I'm building a portable C library that needs to interact with a user-defined peripheral.
Here's an example,
My library needs to use a user-defined putc() and a getc().
To my understanding, there's two way to do this cleanly:
Using an "install" function that requires user to define interface with function pointers
// Inside mylib.h
typedef int (*mylib_port_putc)(char c);
typedef int (*mylib_port_getc)(char *c);
void mylib_install_port(mylib_port_putc, mylib_port_getc);
Using the extern keyword to let the user decide where to define the interfaces.
// Inside mylib.h
extern int mylib_port_putc(char c);
extern int mylib_port_getc(char *c);
What's the best way to do this?
I understand that "best" is difficult to define, but your opinion would be greatly appreciated.
EDIT:
I disagree with the StackOverflow maintainers to flag this question as inappropriate. I don't think this website should be limited to "how do I do X?" questions. I understand that asking for opinions will probably not generate a clear answer, but, to me, this is what mentorship looks like.
When you're starting, there's a lot of value in studying knowledgeable people debating tradeoffs.
Sorry for the rent. I know your job is hard. Happy holidays.
I think that the only correct way is the first -
you provide a library initialize(...) function for registering user callback functions - port_putc, port_getc, maybe also callbacks for you library logging, etc.
Such an approach is flexible, and very common.
In the second case, you require the library users to define 2 functions with predefined names in their code.
What happens if he didn't?
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 :)
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 1 year ago.
Improve this question
I want to find the C libraries' source code to find out more about the functions used.Is GCC the only resource I can count on?I couldn't use the doxygen html version of the GCC libraries,to tell the truth it seems complex to me,for example I couldn't find the printf function's source code(Was I looking in the wrong place?).
Thanks in advance.
OTOH, in addition to glibc:
uclibc
dietlibc
BSD libc
Reading the source code is one thing. Reading a good book that includes source code is another thing entirely. And I'm not sure you can do better than The Standard C Library, by PJ Plauger. It's 20 years old, but for me it's still a page-turner.
Man, I feel old.
The GNU version is here:
Info:
http://www.gnu.org/software/libc/
Download:
http://ftp.gnu.org/gnu/glibc/
You may need to narrow down your question a bit. The implementation varies. Not everything related to implementation details (perhaps pretty much all of it - someone with more standard knowledge can chip in) is prescribed by the C/C++ standard.
In the end you may understand how a particular library decided to do it. It's still useful knowledge, but not THE answer.
The simplest and cleanest standard C library I've ever seen is Minix's standard library.
I ported it on at least 3 toolchains with virtually no effort.
I actually grew up with that library.
I couldn't find the printf function's source code
Here is the source code for this function, it seems to be calling to __vbprintf_internal, which is found glibc/stdio-common/vfprintf-internal.c. Glibc source code is also available in bootlin, I think it is more readable and convenient code.
int __printf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = __vfprintf_internal (stdout, format, arg, 0);
va_end (arg);
return done;
}
glibc/stdio-common/printf.c
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 4 years ago.
Improve this question
Does anyone have tips/resources for how to, in the best way, structure your C code projects? (Different folders etc.) And how do you know when it's good to split code into separate files? And what is an example of a good Makefile?
My project is not that big, but I wanna start to structure my code at an early stage..
Structuring code needs some experience but mostly common sense.
For splitting code, you usually go for readability: conceptually coherent functions/datatypes should go in the same file. You can take c standard library as a good example. It is better to keep your data structure definitions and function declarations in separate headers. This allows you to use the data structures as part of a compilation unit even if you have not defined all the functions.
Files that provide similar functionality should go in the same directory. It is good to avoid deep directory structure (1 level deep is best) as that complicates building the project unnecessarily.
I think Makefiles are OK for small projects, but become unwieldy for bigger ones. For really serious work (if you want to distribute your code, create an installer etc) you may want to look at cmake, scons, etc.
Have a look at the GNU coding standards: http://www.gnu.org/prep/standards/standards.html
Look at the gnu make manual for a simple example Makefile. You can also pick up any opensource project and look at the Makefile. Browsing code repositories in sourceforge.net may be useful.
Read one of the many C coding standards available on the internet and follow one that looks reasonable for your requirements. A few links:
GNU Coding Standards
C Coding Standards at IRAM (pdf)
Indian Hill C Style and Coding Standards
The following books also contain effective guidelines on writing good C code:
The C Programming Language
The Practice of Programming
The Elements of Programming Style
This is sometimes overlooked, but security is an issue in big projects. Here's some advice about how to program securely.
Here is an idiom I like:
Declare structs in a header so that their size is known by client code. Then declare init and deinit functions to the following convention:
The first parameter is a struct foo*.
The return type is a struct foo*.
If they might fail, the last parameter is either int* (simplest), enum foo_error* (if there are several ways it can fail that the calling code might care about) or GError** (if you're writing GLib-style code).
foo_init() and foo_deinit() return NULL if the first parameter is NULL. They also return the first parameter.
Why do it this way? Calling code doesn't have to allocate heap space for the structure, it can go on the stack. If you are allocating it on the heap, though, the following works nicely:
struct foo* a_foo = foo_init(malloc(sizeof(*a_foo)));
if (a_foo == NULL) {
/* Ruh-oh, allocation failure... */
}
free(foo_deinit(a_foo));
Everything works nicely even if a_foo == NULL when foo_deinit is called.