Re: Are bad developer libraries the problem with M$ software?
From: Glynn Clements (glynn.clements@virgin.net)
Date: 11/19/02
- Previous message: Frank Knobbe: "Re: Are bad developer libraries the problem with M$ software?"
- In reply to: cdavison@nucleus.com: "Re: Are bad developer libraries the problem with M$ software?"
- Next in thread: Elliott Mitchell: "Re: Are bad developer libraries the problem with M$ software?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
From: Glynn Clements <glynn.clements@virgin.net> Date: Tue, 19 Nov 2002 03:45:19 +0000 To: "" <cdavison@nucleus.com>
"" wrote:
> As a follow-up to my post, where I said:
>
> "I believe you mean strlen and not sizeof. sizeof(mystr) will return
> the same as sizeof(char*), which is sizeof(int) in most cases or 4 on
> 32-bit platforms.
>
> Unless there's something I wasn't aware of: you're using a bizarre
> compiler, or C++, or there's a special case for char arrays on the
> stack."
>
> Looks like there's a special case for char arrays on the stack (which
> I guess technically would be of type char[] and not char*).
It's nothing to do with the stack; arrays in the data segment are no
different to arrays on the stack.
The size of an array is the product of the number of elements and the
size of each element. The size of a pointer is fixed, and is normally
the same as the platform's "word size" (e.g. 2, 4 or 8 bytes for 16,
32 and 64 bit platforms respectively).
However, it's easy to get confused by the various ways in which C
treats arrays and pointers as being equivalent. E.g. arrays are always
passed by reference (as a pointer to the start of the array). If you
declare a function's argument as an array, e.g.
void foo(char arg[])
it's actually a pointer.
> Once you pass the string around to functions as a char*, or allocate
> it dynamically, you can't use sizeof.
>
> #include <stdio.h>
> void fn (char *p) {
> printf ("sizeof(p) = %d\n", sizeof(p));
> }
> int main () {
> char x[15];
> printf ("sizeof(x) = %d\n", sizeof(x));
> fn (x);
> return 0;
> }
>
> Output:
> sizeof(x) = 15
> sizeof(p) = 4
Note that you would get exactly the same result if the array "x" was
in the data segment (a global variable or a "static" local variable)
rather than on the stack.
-- Glynn Clements <glynn.clements@virgin.net>
- Next message: Andrew Griffiths: "Re: Are bad developer libraries the problem with M$ software?"
- Previous message: Frank Knobbe: "Re: Are bad developer libraries the problem with M$ software?"
- In reply to: cdavison@nucleus.com: "Re: Are bad developer libraries the problem with M$ software?"
- Next in thread: Elliott Mitchell: "Re: Are bad developer libraries the problem with M$ software?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|