Thread: size of the void *

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    Smile size of the void *

    Hi,

    I have tried following program and the output is 1. Does this mean by default void * can be treated as char *.

    Code:
    #include<stdio.h>
    int main()
    {
            void *ptr;
            int i;
            i = sizeof(*ptr);
            printf("%d",i);
            return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to the C standard:
    Quote Originally Posted by C99 Clause 6.2.5 Paragraph 19
    The void type comprises an empty set of values; it is an incomplete type that cannot be completed.
    Quote Originally Posted by C99 Clause 6.5.3.4 Paragraph 1
    The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.
    This is unlikely to have changed in C11, so I would conclude that you are observing the effects of undefined behaviour: the compiler could have rightfully refused to compile your program since the type of *ptr is void.
    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 2006
    Posts
    3,445
    What platform/compiler are you using?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    If it's undefined behavior than it doesn't matter what the compiler is being used.

    Anything that is "Undefined behavior" should never be used in ANY code. Standard C code should be portable to any standards compliant compiler. Undefined behavior is NOT.

    As Henny Youngman would say, "Patient: Doctor, Doctor! It hurts when I do this. Doctor: Don't do it!"

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by rstanley View Post
    If it's undefined behavior than it doesn't matter what the compiler is being used.
    I was asking so that I could provide compiler command options to turn on higher warning/error levels. Don't make assumptions. They'll get you in trouble.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by Elkvis View Post
    I was asking so that I could provide compiler command options to turn on higher warning/error levels. Don't make assumptions. They'll get you in trouble.
    You didn't say that. I was following the full thread, and responding to the full thread, not just your question.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by rstanley View Post
    You didn't say that. I was following the full thread, and responding to the full thread, not just your question.
    To be fair, I didn't give any reason for my asking. You made an assumption, and you were wrong.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    compiler is not even giving warning also regarding sizeof on a incomplete pointer(void *). I am using gcc version 4.6.3. Is there any issue with compiler installation?


    Code:
    #include<stdio.h>
    int main()
    {
            void *ptr;
            int i;
            i = sizeof(*ptr);
            printf("%d",i);
            return 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is probably fairer to say it is a concern with quality of implementation of the compiler (i.e. what diagnostics it produces) rather than compiler installation.

    sizeof is rather unique in C as it does not evaluate its operand (it only evaluates the size of the result, should the operand be evaluated). You are playing with one edge case which, in practice, few developers would bother to tickle. Such edge cases tend to be fertile ground for finding unexpected or invalid behaviour from compilers.
    Last edited by grumpy; 01-23-2015 at 04:03 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: invalid conversion from 'const void*' to 'void*'
    By Wahidin Wahid in forum C++ Programming
    Replies: 10
    Last Post: 04-17-2012, 02:17 AM
  2. error invalid conversion from ‘const void*’ to ‘void*’
    By Wahidin Wahid in forum C Programming
    Replies: 3
    Last Post: 03-27-2012, 08:18 PM
  3. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  4. unknown size for type 'void'
    By Noobwaker in forum C Programming
    Replies: 12
    Last Post: 04-05-2006, 06:41 AM
  5. int memCopy(void* , void*, int size)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:02 PM