Passing by reference of by value?

This is a discussion on Passing by reference of by value? within the C Programming forums, part of the General Programming Boards category; It doesn't say they have to be the same size. If char* and int* have different sizes, then void* cannot ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    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
    21,397
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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.

Page 2 of 3 FirstFirst 123 LastLast
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, 09:18 AM

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