Hash table code question

This is a discussion on Hash table code question within the C Programming forums, part of the General Programming Boards category; Originally Posted by matsp Yes, I suppose so. But then I would try to ensure that I knew where and ...

  1. #16
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by matsp View Post
    Yes, I suppose so. But then I would try to ensure that I knew where and how much the padding was too - by adding my own filler bytes and then verifying that sizeof(mystruct) == expected size somewhere in the early stages of the code. That way, there would be no "surprise holes".

    --
    Mats
    I just use memcpy() and don't think about that (yes, syntax can get real ugly, but it is "more natural" operation for me)... But, do you perhaps know what standard (ANSI) says about that?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rasta_freak View Post
    I just use memcpy() and don't think about that (yes, syntax can get real ugly, but it is "more natural" operation for me)... But, do you perhaps know what standard (ANSI) says about that?
    Yes, but what I suggested is not really to solve the copying problem, but rather the underlying problems caused by gaps/padding in a struct when using it across applications - and as my edit says, that's what I expect you are doing if you are using CRC to check it's consistency. Within an application, struct contents should only get lost if the application itself is buggy.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Oh, and another point is of course that copying with memcpy() gives you no typechecking:
    Code:
    struct X
    {
       int a;
    };
    
    struct Y
    {
       int b[1000];
    };
    
    ...
       struct X x;
       struct Y y;
    ..
       memcpy(x, y, sizeof(y));
       // Probably won't get much further, as you've just overwritten 3996 bytes of memory with contents of y.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    But isn't it "weird" that you can do:
    Code:
    struct _s
    {
       int a;
       int b;
    } x,y;
    
    x = y;
    but you can't do:
    Code:
    if (x != y)
    {
        ...
    }
    EDIT: I could be seriously wrong here, but it seems to me that for comparing, you have to use memcmp() (or compare each member individualy). And if you use memcmp(), why not memcpy() too?
    Last edited by rasta_freak; 08-13-2008 at 05:16 AM.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rasta_freak View Post
    But isn't it "weird" that you can do:
    Code:
    struct _s
    {
       int a;
       int b;
    } x,y;
    
    x = y;
    but you can't do:
    Code:
    if (x != y)
    {
        ...
    }
    EDIT: I could be seriously wrong here, but it seems to me that for comparing, you have to use memcmp() (or compare each member individualy). And if you use memcmp(), why not memcpy() too?
    Comparing a struct is more complex than that - in C++ you could make a compare operators (operator=, operator<, etc), but except for equality of trivial structs [no indirect content], you can't really compare structs directly. For example:
    Code:
    struct A
    {
       int x;
       char *str;
    };
    
    struct A a, b;
    
      a.x = 7;
      a.str = malloc(10);
      strcpy(a.str, "Hi");
      b.x = 7;
      b.str = malloc(10);
      strcpy(b.str, "Hi");
    Is a == b?

    Also, at least memcmp() won't overwrite some other memory in the process, so you have saved yourself one potential bug.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    But when you have pointers in struct, it's risky/invalid to use "a=b;" too (and memcpy() of course), so i personaly don't see any benefit in assigning struct to struct (furthermore, it seems to me risky and implemented as some sort of "hack"). But anyway, thanks matsp for yet another "lessons"

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to make a hash table......trouble w/ memory?
    By cuizy in forum C Programming
    Replies: 3
    Last Post: 05-11-2009, 04:47 PM
  2. hash table *clueless* help :)
    By willc0de4food in forum C Programming
    Replies: 2
    Last Post: 10-15-2005, 03:54 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Hash Table implementation
    By supaben34 in forum C++ Programming
    Replies: 6
    Last Post: 09-26-2003, 12:48 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21