Jack
Jack

Reputation: 16724

Is MAX_PATH always same size, even if _UNICODE macro is defined?

Should I make room to it, like this:

 len = MAX_PATH * sizeof(_TCHAR) + sizeof(_TCHAR);

or is:

len = MAX_PATH + sizeof(_TCHAR);

Right size to hold a path including unicode?

Upvotes: 2

Views: 15006

Answers (3)

Mahonri Moriancumer
Mahonri Moriancumer

Reputation: 6003

The MAX_PATH macro definition (260 bytes) is an adopted ANSI standard. Unfortunately, this standard was developed long before the advent of the 32-bit OS.

Currently, Unicode versions of the (Windows) file libraries support a path up to 32,767 bytes. However, the MAX_PATH definition remains as it was for (16-bit) MS-DOS

Upvotes: 2

Remy Lebeau
Remy Lebeau

Reputation: 597016

MAX_PATH (which is always 260) is expressed in characters, not in bytes.

Use the first one when allocating raw memory that is expressed in byte sizes, eg:

LPTSTR path = (LPTSTR) LocalAlloc(LMEM_FIXED, (MAX_PATH + 1) * sizeof(TCHAR));

Use the second one when allocating memory that is expressed in characters, eg:

TCHAR path[MAX_PATH + 1];

LPTSTR path = new TCHAR[MAX_PATH +1];

Upvotes: 10

doptimusprime
doptimusprime

Reputation: 9415

MAX_PATH is defined as 260 in Windef.h irrespective of Unicode.

First approach is fine if you are holding number of bytes. Second approach does not make any sense. If you are holding number of characters, you should use MAX_PATH+1.

Upvotes: 3

Related Questions