Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Functions inside C structs

  1. #1
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Functions inside C structs

    Hi,
    I'm learning C, for short my question would be "(how) can I include/tighten functions into structs?".
    While learning and looking for this info in other books I couldn't find a way to write "objects with functions" that could be accessed like "myVector.add( param )", instead of "vector_add( &vector, param )".
    Here's a snippet:

    Code:
    #include <stdio.h>
    #include "BytesVector.h"
    
    void init( BytesVector *vector ) {
    	vector->iCurrentIndex = 0;
    	vector->iCount = 0;
    }
    
    //HERE CAN I AVOID PASSING EACH TIME THE POINTER?
    void add( BytesVector *vector, char text[] ) {
    	//do stuff
    }
    
    //HERE SAME ISSUE
    void setCurrentIndex( BytesVector *vector, int *iIndex ) {
    	vector->iCurrentIndex = *iIndex;
    }
    I know in C++ one can write like this:
    Code:
    void BytesVector::add( char text[] ) {
    	//do stuff
    }
    but in C you can't, is that correct..(?)
    62°23′30″N 145°09′0″W
    ёёмаёё..

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Functions inside C structs

    In C, structs can't contain functions.

    They can, however, hold function pointers.

  3. #3
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Functions inside C structs

    Why not use C++ if this is what you're after?
    LambdaGrok. | #ubuntu-programming on FreeNode

  4. #4
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Functions inside C structs

    Quote Originally Posted by CptPicard View Post
    Why not use C++ if this is what you're after?
    "Learning C" and still getting used to it.

  5. #5
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Functions inside C structs

    Quote Originally Posted by CptPicard View Post
    Why not use C++ if this is what you're after?
    That's a good question, I created a "hello world" in C and C++. By default In C it was about 8KB, in C++ about 200KB, that scared me..
    Since I'm coming from Java I would welcome the functionality of C++, but there's another issue: on Linux most applications (and even the kernel AFAIK) are written in C..
    If I find a way to create programs as small as those created in C I would certainly switch to it ( I guess ).

    Thanks both, I will try implementing "pointers inside structs"..
    62°23′30″N 145°09′0″W
    ёёмаёё..

  6. #6
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Functions inside C structs

    The C++ standard library does pull in a lot of stuff into your binary, but do remember that the more own code you write, the less, in percentage terms, that library code becomes. "Hello World" is the extreme example.

    Also, the difference is completely meaningless on modern computers. You aren't coding for a pocket calculator.

    Another way to view this: your 200k Hello World is certainly much smaller than anything you had with Java
    LambdaGrok. | #ubuntu-programming on FreeNode

  7. #7
    Join Date
    Dec 2007
    Location
    Ankara
    Beans
    41
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Functions inside C structs

    Java is alot like C++, as you said. Most apps are written in C, but C++ programmers *can* use C too.

    And, if you try that pointers inside structs approach, you'll waste 4 bytes for every member of that struct.

    Anyway, i'll give an example for C and C++ way:

    Code:
    struct A
    {
    	int x;
    };
    void resetA(struct A *arg)
    {
    	arg->x = 0;
    }
    And, C++ way:
    Code:
    class A
    {
    private:
    	int x;
    public:
    	reset()
    	{
    		x = 0;
    	}
    }
    I find C++ way simpler and better. And, using features of C++ is no harm, you'll get used to C too.

  8. #8
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Functions inside C structs

    Quote Originally Posted by CptPicard View Post
    The C++ standard library does pull in a lot of stuff into your binary, but do remember that the more own code you write, the less, in percentage terms, that library code becomes. "Hello World" is the extreme example.

    Also, the difference is completely meaningless on modern computers. You aren't coding for a pocket calculator.

    Another way to view this: your 200k Hello World is certainly much smaller than anything you had with Java
    Actually a Java "hello world" is 415 bytes, thus a lot smaller, if I make it an executable .jar file it will still be around 1KB. I almost created a file manager in Java and the executable with all the code in it was (and is) about 150KB (without such resources as icons), the executables in Java are so small mostly because they are mainly "text files with little binaries altogether compressed using the "zip" utility and named with a .jar extension". The actual pain # 1 in Java (for me) is the Java startup time (especially the cold startup). That's the main reason why I decided to learn and rewrite the file manager in a native language.

    I completely agree with you about code size, however I'm planning to make it available for download from the internet so the less people will have to download the better ( I guess ).
    62°23′30″N 145°09′0″W
    ёёмаёё..

  9. #9
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Functions inside C structs

    Quote Originally Posted by xlinuks View Post
    Actually a Java "hello world" is 415 bytes, thus a lot smaller, if I make it an executable .jar file it will still be around 1KB. I almost created a file manager in Java and the executable with all the code in it was (and is) about 150KB (without such resources as icons), the executables in Java are so small mostly because they are mainly "text files with little binaries altogether compressed using the "zip" utility and named with a .jar extension". The actual pain # 1 in Java (for me) is the Java startup time (especially the cold startup). That's the main reason why I decided to learn and rewrite the file manager in a native language.

    I completely agree with you about code size, however I'm planning to make it available for download from the internet so the less people will have to download the better ( I guess ).
    Java also runs a VM....a lot more memory (not that it makes a difference)

  10. #10
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Functions inside C structs

    Yes, Java runs a VM, that is # 2 pain for me, because:
    1) It eats up (much) more memory than a C/C++ app (yes, I did a comparison)
    2) The VM uses extra resources, especially while system iddle, thus its always eating more CPU than your app normally does.
    3) I call it "the wave effect". The difference in speed while executing same block of code before and after the VM (the Hotspot VM in my case) internally compiles the code.

    Thus I guess it actually makes a difference.. It might not make such a difference on the server side since the apps is only once started and runs for long days/months running basically same blocks of code..
    62°23′30″N 145°09′0″W
    ёёмаёё..

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •