Thread: how sizeof works and how to useinet_pton faster?

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    4

    how sizeof works and how to useinet_pton faster?

    Hello,

    please be easy with me because I have struggling learning the C language and bad english.

    sizeof question:

    I don't understand how to figure this without compile.

    Code:
    char x[4];
    sizeof(x)    -> gives 4
    sizeof(&x)  -> gives 8, why?
    Code:
    struct sockaddr_in sa;
    sizeof(sa)   -> gives 16, why?
    sizeof(&sa) -> gives 8, why?
    Code:
    unsigned long x;
    sizeof(x)   -> gives 8, why?
    sizeof(&x) -> gives 8, why?
    how is it possible that they are same?


    inet_pton: which one of them is the faster? :

    my goal is given ipv4 address is valid and for that I found more than one solution using inet_pton function, I want to use the fatest solution.


    Option 1:
    Sending address of (unsigned long) to third parameter in inet_pton function.
    Like shown here: C - IP address validation

    Option 2
    Sending the address of sockaddr_in to third parameter in inet_pton function.
    Like shown here: Determine if a string is a valid IPv4 address in C - Stack Overflow

    Option 3:
    Creating an array of char (4 size), and sending it to third parameter in inet_pton function.
    Beause it's doing memset 4 times: inet_pton.c source code [glibc/resolv/inet_pton.c] - Woboq Code Browser

    Which one of the options is faster?

    I ran benchmark using perf tool and it appears that Option 2 is the fatest as it run less time.

    .

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    how is it possible that they are same?
    Pointers to objects don't necessarily have to be of different suzes: a pointer to a char could be the same size as a pointer to a struct object containing an array of a thousand chars, as long as the pointer can point to the memory occupied by the objects.

    Which one of the options is faster?

    I ran benchmark using perf tool and it appears that Option 2 is the fatest as it run less time.
    You seem to have answered your own question.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    Quote Originally Posted by laserlight View Post
    Pointers to objects don't necessarily have to be of different suzes: a pointer to a char could be the same size as a pointer to a struct object containing an array of a thousand chars, as long as the pointer can point to the memory occupied by the objects.


    You seem to have answered your own question.
    I don't have any reputation here but that is a derogatory comment

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    Quote Originally Posted by Teeth TriHard View Post
    I don't have any reputation here but that is a derogatory comment
    Derogatory?! Why?

    Pointers have, indeed, in most platforms, the same size. A pointer holds a memory address...
    And, if you did the test with perf and found an answer, you really seem to have answered your own question, didn't you?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Well, if you're not satisfied by the result of your own measurement, and would like a definitive answer from the standard in which inet_pton is defined, then the answer is: none of them are guaranteed to be the fastest; it depends on the implementation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    Quote Originally Posted by laserlight View Post
    Well, if you're not satisfied by the result of your own measurement, and would like a definitive answer from the standard in which inet_pton is defined, then the answer is: none of them are guaranteed to be the fastest; it depends on the implementation.
    I tested with perf again and the elapsed time is not constant, running it couple times doesn't give me a close result to the previous one.


    I'm still struggling with the first question. I understood that using & on a variable will give me the addess of it but not with the sizeof, how it works, why those are different? Could you explain it ? if not point a references please?

    Code:
    char x[4];
    sizeof(x)    -> gives 4
    sizeof(&x)  -> gives 8, why?
    Code:
    struct sockaddr_in sa;
    sizeof(sa)   -> gives 16, why?
    sizeof(&sa) -> gives 8, why?
    Code:
    unsigned long x;
    sizeof(x)   -> gives 8, why?
    sizeof(&x) -> gives 8, why?

  7. #7
    Guest
    Guest
    Code:
    // 4 byte object, 8 byte (64bit) address
    char x[4];
    sizeof(x)    -> gives 4
    sizeof(&x)  -> gives 8
    
    // 16 byte object, 8 byte address
    struct sockaddr_in sa;
    sizeof(sa)   -> gives 16
    sizeof(&sa) -> gives 8
    
    // 8 byte object, 8 byte address
    unsigned long x;
    sizeof(x)   -> gives 8
    sizeof(&x) -> gives 8

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,181
    Figure out the name of your Operating System and the Name and version of your Compiler.
    Then, maybe someone could help you with your sizeof question.

    Edit: Will likely need the CPU information, also.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    Quote Originally Posted by Guest View Post
    Code:
    // 4 byte object, 8 byte (64bit) address
    char x[4];
    sizeof(x)    -> gives 4
    sizeof(&x)  -> gives 8
    
    // 16 byte object, 8 byte address
    struct sockaddr_in sa;
    sizeof(sa)   -> gives 16
    sizeof(&sa) -> gives 8
    
    // 8 byte object, 8 byte address
    unsigned long x;
    sizeof(x)   -> gives 8
    sizeof(&x) -> gives 8
    Thanks! That is much clearer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  2. Which is faster?
    By Yarin in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2008, 05:20 PM
  3. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  4. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 11:00 AM
  5. Which is Faster....
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-19-2002, 07:18 AM

Tags for this Thread