AnimeGalleries [dot] NetAnimeWallpapers [dot] ComAnimeLyrics [dot] ComAnimePedia [dot] ComAnimeGlobe [dot] Com


User Tag List

Closed Thread
Results 1 to 4 of 4

Thread: [Tips - C] Grab bag of useful techniques with structs

  1. #1
    4: [Classified brah] Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris's Avatar
    Gil
    101,951.98
    Gender
    Gifts Tuxedo Mask Rose Mario Question Block Pen
    Mentioned
    301 Post(s)
    Tagged
    0 Thread(s)
    Latest Post
    03-06-2015 01:53 AM
    User Info Thanks / Tagging Info Gifts / Achievements / Awards vBActivity Stats
    Join Date
    Aug 2001
    Location
    Tau Ceti V
    Age
    37
    Threads
    617
    Posts
    19,697
    Blog Entries
    620
    Rep Power
    14769

    Default [Tips - C] Grab bag of useful techniques with structs

    This works in C++ as well, but most of it can be done with classes instead. This is a set of work-arounds to get object-oriented features in C.

    Arbitrary length structs:

    You can use C's lack of bounds checking to create arrays that have arbitrary size.
    Code:
    struct foo {
      int type;
      char data[1];
    };
    
    /* ... */ 
    
    struct foo *create_foo_with_length(int type, int data_length) {
      /* One byte is always part of sizeof(struct foo). Add data_length - 1 to 
         compensate for the rest of the bytes */
      return malloc(sizeof(struct foo) + data_length - 1);
    }
    
    /* ... */ 
    
    void bar() {
      struct foo *f = create_foo_with_length(TYPE_BAR, 100);
      /* This is now valid: */
      int i;
      for(i = 0; i < 100; i++) {
        f->data[i] = 2*i;
      }
      free(f);
    }
    This is common in network programming. You can construct a system that allows for data chunks of different size by reading the first few bytes corresponding to the type id, and then allocate the rest of the buffer size depending on the type.

    Next technique is "struct inheritance":

    Code:
    struct base {
      int type;
      int p;
      int q;
    }
    struct childA {
      struct base base;  /* Must be first element */
      int r;
      int s;
    }
    
    struct childB {
      struct base base;  /* Must be first element */
      int t;
      int u;
    }
    
    
    void bar() {
      struct childA c;
      struct childB c;
      struct bar *a = (struct base *) &c;
      struct bar *b = (struct base *) &c;
      /* a and b are now a valid pointers of type c, corresponding to 
         a.child and b.child respectively. */
      /* If the base struct has a field indicating it's type, you can use that to 
         indicate what sort of struct it is, and pointer cast to those types to access
         their members in a valid fashion */
         
    }



    Hey look, Japan made a movie about me!

  2. #2
    Thats life. Awesome? Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume's Avatar
    Gil
    7,744.42
    Gender
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Latest Post
    10-22-2012 06:06 PM
    User Info Thanks / Tagging Info Gifts / Achievements / Awards vBActivity Stats
    Join Date
    Aug 2007
    Location
    Virginia
    Age
    35
    Threads
    31
    Posts
    392
    Rep Power
    244
    Gamer IDs

    Wii Code: 4768-7102-5291

    Default

    I actually find this post to be useful. I've always heard talk of C having the ability to use OO features. However I still would prefer procedural over encapsulation anyday.

  3. #3
    Thats life. Awesome? Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume has a reputation beyond repute Shin Natsume's Avatar
    Gil
    7,744.42
    Gender
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Latest Post
    10-22-2012 06:06 PM
    User Info Thanks / Tagging Info Gifts / Achievements / Awards vBActivity Stats
    Join Date
    Aug 2007
    Location
    Virginia
    Age
    35
    Threads
    31
    Posts
    392
    Rep Power
    244
    Gamer IDs

    Wii Code: 4768-7102-5291

    Default

    Also, libnet allocates that memory behind the scenes >_> for the chunks of data.

  4. #4
    4: [Classified brah] Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris has a reputation beyond repute Eris's Avatar
    Gil
    101,951.98
    Gender
    Gifts Tuxedo Mask Rose Mario Question Block Pen
    Mentioned
    301 Post(s)
    Tagged
    0 Thread(s)
    Latest Post
    03-06-2015 01:53 AM
    User Info Thanks / Tagging Info Gifts / Achievements / Awards vBActivity Stats
    Join Date
    Aug 2001
    Location
    Tau Ceti V
    Age
    37
    Threads
    617
    Posts
    19,697
    Blog Entries
    620
    Rep Power
    14769

    Default

    Here's another pretty neat trick:

    Code:
    int x;
    if(foo) x = 1;
    else if(bar) x = 2;
    else if(baz) x = 3;
    else x = 4;
    Can also be written
    Code:
    x = foo ? 1 : 
          bar ? 2 : 
          baz ? 3 :
          4;
    Also works with returns and whatnot, and in any language that has the "test ? true : false" operator (C, C++, PHP I think, + other languages).



    Hey look, Japan made a movie about me!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. SSBMelee: Improving your game
    By Lucidian Dyne in forum Video Games
    Replies: 51
    Last Post: 11-03-2005, 02:53 PM

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