Re: size_t ?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>I have been reading up on UNICODE, hence normal characters and wide characters
>and personally, I think Windows got complicated, but probably had no choice
>given that computers started of with an 8 bit ascii bytes.... But one thing
>is for sure, is that I find myself reading complex documentation that refers
>to excuse the expression, "under the window's hood information", causing me
>to read it several times to try and make sence of it.
>
>Sure I can just skip it and just take it for granted that a char type is
>just a char type and the hell with where TCHAR comes from. But I just can't
>do that.
>
>I have appreciated all your help guys....
>
>OKAY, My question....
>
>I have read that:
>
>wchar * pc = "Hello";
>iLenght = strlen(pc);
>
>is a pointer to a chartacter string, where iLenght will get the lenght of
>the string stored at the location of pc.

No, this is incorrect. "Hello" is an 8-bit character string, where each
character is of type "char". So, your example would be:

char * pc = "Hello";
int iLength = strlen(pc);

iLength will contain the value 5. Note that the string actually occupies 6
bytes, because there is a 0 byte at the end to mark the end of the string.

>And that if I do this:
>
>wchar * pw = L"Hello";
>iLenght = strlen(pw);
>
>we will get a warning stating that the strlen function accepts a pointer to
>a character but is getting a pointer to an unsigned short.

The 16-bit character type is "wchar_t", not "wchar". So:

wchar_t * pw = L"Hello";

>So we must use:
>
>iLenght = wcslen (pw)
>
>instead.

Correct. iLength will contain 5. The string occupies 12 bytes: 5
characters at 2 bytes each, plus a 2-byte zero terminator.

>It has been written that wcslen is the wide character version of the strlen
>and declared in STRING.H and WCHAR.H.
>
>Here is the start of my gray zone:
>
>-Why declared in 2 locations?

Convenience. Programmers are used to including <string.h> for their string
functions, and wchar.h provides a nice place to collect all of the wide
string functions.

>-Can I view this, to satisfy my curiosity (I typed out both files in a
>search and I did not find the following declarations)?:
>
>size_t __cdecl strlen (const char *)
>size_t __cdecl wcslen (const wchar_t *)

Well, declarations are in both files:

C:\Tmp>findstr strlen \vs\vc7\include\wchar.h \vs\vc7\include\string.h
\vs\vc7\include\string.h:_CRTIMP size_t __cdecl strlen(const char *);
\vs\vc7\include\string.h: size_t __cdecl strlen(const char *);

C:\Tmp>findstr wcslen \vs\vc7\include\wchar.h \vs\vc7\include\string.h
\vs\vc7\include\wchar.h:_CRTIMP size_t __cdecl wcslen(const wchar_t *);
\vs\vc7\include\string.h:_CRTIMP size_t __cdecl wcslen(const wchar_t *);

>-Is size_t an integer type, and where is it declared in the windows header
>files and how is it declared?

It's in several different convenient locations:

C:\Tmp>findstr typedef.*size_t \vs\vc7\include\std*
\vs\vc7\include\stddef.h:typedef unsigned __int64 size_t;
\vs\vc7\include\stddef.h:typedef _W64 unsigned int size_t;
\vs\vc7\include\stdio.h:typedef unsigned __int64 size_t;
\vs\vc7\include\stdio.h:typedef _W64 unsigned int size_t;
\vs\vc7\include\stdlib.h:typedef unsigned __int64 size_t;
\vs\vc7\include\stdlib.h:typedef _W64 unsigned int size_t;

As others have said, this is not a Windows header, but rather a standard C
header. Note that the __int64 definition is used in a 64-bit program, and
the "unsigned int" is used in a 32-bit program.

>-And what does the __cdecl mean?

There are several different mechanisms used to pass parameters between
functions, for technical reasons that won't mean very much to you right
now. The ones you'll encounter most often are __stdcall, __cdecl, and
__fastcall. The default is __stdcall; since C run-time routines must often
use __cdecl, it has to be specifically mentioned.
--
- Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.
.



Relevant Pages

  • [TOMOYO #15 3/8] Common functions for TOMOYO Linux.
    ... This file contains common functions (e.g. policy I/O, pattern matching). ... Since TOMOYO Linux is a name based access control, ... TOMOYO Linux's string manipulation functions make reviewers feel crazy, ... the Linux kernel accepts all characters but NUL character ...
    (Linux-Kernel)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • Re: RfD: Escaped Strings
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... \b BS (backspace, ASCII 8) ... \ ** escapes to characters much as C does. ...
    (comp.lang.forth)
  • Re: A note on computing thugs and coding bums
    ... code is valid for any character set that is legal in C (which is a ... characters in the required source character set ... A String, in C Sharp or Java, can be redefined. ... allow programmers to handle some other data format, ...
    (comp.programming)