0

I have two questions:

  1. Am I right that on 4-bit systems, a pointer is 4 bytes?

  2. Are "pass by reference" and "pass by pointer the same thing, just different wording?

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Mark
  • 6,052
  • 8
  • 61
  • 129
  • You mean 4 bytes, or 32bit as every body call it. And those are all the same: `pass by reference`, `pass by pointer"` or, I add, `pass the address of`. –  Jan 29 '13 at 22:52
  • 1
    @Sp. Pass by pointer and pass by reference are not the same thing. –  Jan 29 '13 at 22:53
  • @H2CO3 How come? they simes the same thing to me, we pass an address of something. –  Jan 29 '13 at 22:54
  • 1
    @Sp. Any decent C++ guy would kill you for this, but I'm not a C++ programmer so I won't. [But read this.](http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c) –  Jan 29 '13 at 22:56
  • @H2CO3 OK I got it, we are talking about C++ |:. –  Jan 29 '13 at 22:59
  • 2
    @Sp. And C. This question is **very confusing** since it mixes up C and C++ concepts. But in practice, they're two distinct languages **with loads of huge conceptual and syntactic differences.** –  Jan 29 '13 at 23:00
  • @Linuxios Although you're right about the overwhelming popularity of 8-bit systems, this question **is** about the language-lawyer aspect. –  Jan 29 '13 at 23:01
  • @H2CO3 I agree with all your remarks and your excellent answer, but your language is a bit on the snarky side. I think it would be easier for people to accept your very valuable advice if leave out derogatory phrases like "why do you think that ... exists?". (The specific example was deleted in the meantime, along with the question, for those coming late to the party.) – Daniel Roethlisberger Jan 29 '13 at 23:05
  • @Sp. Don't confuse bytes with octets. There is no requirement that a byte be 8 bits in C. This is the rationale for CHAR_BIT. – autistic Jan 29 '13 at 23:53
  • +1 for nostalgia of 4004 – Cole Tobin Jan 29 '13 at 23:54
  • 1
    @Sp.: In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values. – tomlogic Jan 30 '13 at 00:50

3 Answers3

7

Am I right that on 4-bit system a pointer size is 4

If your system has 1-bit bytes, then for sure it is. (But C doesn't support platforms where bytes are shorter than 8 bit anyway.)

Is "pass by reference" and "pass by pointer" the same thing, but different wording?

No. Pass-by-pointer is a C approach for emulating pass-by-reference. The concept is different.

  • @KingsIndian I was thinking about the exact same thing. Deleted the comment. –  Jan 29 '13 at 23:09
  • 1
    No? I thought it doesn't support any system where a `char` isn't a byte. A byte isn't always 8 bits... Look at mainframes from the 80's – Cole Tobin Jan 29 '13 at 23:49
  • 1
    @ColeJohnson A char is always a byte in C. However, a byte need not be 8 bit. What you are talking about is an octet. –  Jan 30 '13 at 05:34
  • I know the difference between an octect and a byte. I was stating that your mention that "C doesn't support platforms where bytes are shorter than 8 bit[s]" is incorrect IIRC as C just requires a `char` to be a byte, not an octect. I was pointing out that mainframes from the 80's and such don't use 8-bit octects. Some use 6-bit. Feel free to disagree, but we can agree on one thing - a bit is either high or low (`1` or `0` respectively) – Cole Tobin Jan 30 '13 at 16:29
2

The size of a pointer doesn't necessarily correlate to the native word size of the CPU; for example, the original Macintosh ran on a 32-bit processor (Motorola 68000) which only had 24 address lines, so pointers were limited to 24 bits. The pointer value was stored in a 32-bit word, but the top 8 bits weren't used. Some enterprising programmers used those top 8 bits to store other data with the pointer. This caused some heartburn when the 68020 came out, which had 32 address lines. It took a while to rewrite that code so that it was "32-bit clean".

Note also that pointers to different types need not be the same size.

In practice, on any modern desktop system (read: x86) all pointer types will be either 32 or 64 bits wide. But don't rely on that being true for all architectures.

As for pass by "pass by reference" and "pass by pointer", no, they are not simply different wordings of the same concept.

In a pass-by-reference system, the formal parameter in the function definition and the actual parameter in the function call designate the same memory (or at least changes to one are reflected in the other). Looking at some old-school Fortran code:

C234567890
      PROGRAM CALLSW
      INTEGER M, N

      M = 1
      N = 2

      WRITE(*,*) M, N
      CALL ISWAP(M, N)
      WRITE(*,*) M, N

      STOP
      END

C234567890
      SUBROUTINE ISWAP(A, B)
      INTEGER A, B
      INTEGER TMP

      TMP = A
      A = B
      B = TMP

      RETURN
      END

The formal parameter A in ISWAP designates the same object in memory as M in the main program, so writing to A changes the value in M. You can think of A as a pointer to M (or that A and M are both pointers to the same object), but the language hides that pointer-ness from the programmer.

C passes everything by value; the formal parameter and the actual parameter always designate different objects in memory, so writing to one doesn't affect the other. When we want to modify an object in a subroutine, we must explicitly pass its address using the & operator, and then dereference it in the subroutine with the * operator:

void iswap(int *a, int *b)
{
  int tmp;

  tmp = *a;
  *a = *b;
  *b = tmp;
}

int main(void)
{
  int m, n;

  m = 1;
  n = 2;

  printf("m = %d, n = %d\n", m, n);
  iswap(&x, &y);
  printf("m = %d, n = %d\n", m, n);

  return 0;
}

Instead of passing m and n to iswap, we pass the results of the expressions &m and &n, which are pointers to the two objects. Similarly, in the iswap function, we don't write to a or b, we write to the results of the expressions *a and *b. a and m refer to two completely different objects in memory, as do b and n. Writing to a will not affect m at all.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • This is the correct answer. A pointer doesn't need be the _native_ word size (native as a word on x86 is generally _16_-bits, not 32 or 64 [depending on operating mode]) – Cole Tobin Jan 30 '13 at 16:31
0

Regarding parameter passing in C, Wikipedia's entry on C states:

Function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.

tomlogic
  • 11,489
  • 3
  • 33
  • 59