Thread: Faster C coding thanks to utilities?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Faster C coding thanks to utilities?

    Hello,

    I recently had to implement a deep copy for a cascade of C structs. Structs in structs are easy; structs containing variable length arrays of structs are tougher - you actually need to find the length of the array and copy the structs individually - because those structs also contain arrays of structs.

    I did it by running all the C source code through a grep script that pulled out the struct definitions. Within struct definitions I put comments by the arrays I wanted copied and made gawk assemble these into structname_copy() functions - and structname_memdup and structname_memfree functions but that's a side issue. It worked fine but it felt unsatisfactory. The gawk relied on my code being written consistently - it didn't use a C parser, so had I changed my coding style but once the code would have broken. Maintenance is going to be hell.

    Are there utilities for doing general parsing of C files, extracting chunks of code and similar? It would be nice to be able to have a set of C utilities that make this sort of thing as easy as running grep on a one-record-per-line data file. That way I gould generate the copy commands in a makefile and be confident that the code it creates will be good, no matter who else has monkeyed with the code in the meantime.

    Regards,
    Lock

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rommegrot View Post
    I did it by running all the C source code through a grep script that pulled out the struct definitions. Within struct definitions I put comments by the arrays I wanted copied and made gawk assemble these into structname_copy() functions - and structname_memdup and structname_memfree functions but that's a side issue. It worked fine but it felt unsatisfactory. The gawk relied on my code being written consistently - it didn't use a C parser, so had I changed my coding style but once the code would have broken. Maintenance is going to be hell.
    That's actually a clever approach. Several C code tools rely on specially formatted comments. For instance Lint, and certain source code translation tools.

    Are there utilities for doing general parsing of C files, extracting chunks of code and similar?
    There are plenty of refactoring tools available, although I'm not sure that any of them will do exactly what you need. They will certainly get you the names and members of the relevant structures, though.

    If you have programmers using different styles, I think you already have somewhat of a maintenance problem. I don't think introducing a tool is going to make things worse. In fact, it might open everybody's eyes to the advantage of having a consistent style.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://www.gccxml.org/HTML/Index.html
    This would allow you to generate an unambiguous representation of the source, and not rely on the maintainers playing by the rules for detailed formatting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    http://www.gccxml.org/HTML/Index.html
    This would allow you to generate an unambiguous representation of the source, and not rely on the maintainers playing by the rules for detailed formatting.
    Cool link. I had not seen that before.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    That looks like a most complete answer, thanks! xmlpath and xmlgrep to hand I'll start playing with it!

    Regards, Lock

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'm guessing you wont be able to convert your project to C++, but in C++ you just write an assignment operator and copy-constructor for your class, and copying becomes as easy as pie for the rest of the code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Faster bitwise operator
    By Yarin in forum C++ Programming
    Replies: 18
    Last Post: 04-29-2009, 01:56 PM
  2. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  3. does const make functions faster?
    By MathFan in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 09:03 AM
  4. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  5. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM