Thread: Passing by reference of by value?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't say they have to be the same size.
    If char* and int* have different sizes, then void* cannot be the same size as both of them, yet it is implicitly convertible to both types, back and forth. So no, void* does not have to be the same size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    But if void* were to be larger than a char* you would lose data by casting it to char*
    And if int* were to be larger than a void* you would lose data by casting to void*

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You would possibly, yes. Yet it isn't possible for void* to be of two sizes, and different pointers can be different sizes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    void* and char* must have the same representation. But other pointers can be different sizes. Note that the quote from the standard that you posted said that the results shall compare equal only for the object pointer->void pointer->object pointer. The first bit, void pointer->object pointer, does not guarantee the preservation of value.

    See also 6.2.4p26:
    A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Does that actually apply to modern computers though?
    You mean all the ones you've heard of so far?
    The C-FAQ isn't a complete catalogue of all the weird machines out there.
    As for real examples, it mentions the x86 - most people have heard of that right?

    And given the baffling continuing popularity of TurboC, the 16 bit model can definitely come up with different sized pointers.

    Remember, if you intend a career programming C or C++, you're going to come across potentially dozens of different compilers and processors.
    Basing everything off "it works for me today" will almost certainly lead to trouble at some point.

    > But if void* were to be larger than a char* you would lose data by casting it to char*
    C only defines the round trip
    T* -> void* -> T*

    If you try something like char* to int* (with or without void*), then all sorts of problems can happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    I guess I misinterpreted the meaning of it then.
    Although on a side note I can't see how having different sized pointers would work. A pointer has to be the same size as the cpu registers to be able to address the entire memory space.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No one says they have to address the entire memory space. Some memory space might be reserved for other things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Yeah I suppose you are right. I guess I am to used to win32/x86 coding and I'm failing to see the bigger picture. And I also have a bad habbit of being quite stuborn at times

  9. #24
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by _Mike View Post
    I guess I misinterpreted the meaning of it then.
    Although on a side note I can't see how having different sized pointers would work. A pointer has to be the same size as the cpu registers to be able to address the entire memory space.
    Yeah, I don't get it either. I would agree with what you just said.

  10. #25
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    So wait, if all pointer sizes are not the same, then why is it not required to cast malloc()?

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Epy
    So wait, if all pointer sizes are not the same, then why is it not required to cast malloc()?
    Because a pointer to void is convertible to a pointer to any object type.
    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

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is to say, you could think of it this was:
    The compiler implicitly adds a cast to malloc and behaves accordingly. You do not have to specify the cast explicitly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Thanks

  14. #29
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    but if malloc were to allocate memory at an address that is to big for the pointer in question to hold then you'd get very weird results

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm pretty sure that's something the implementers of malloc has to be aware of.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM