ADC Home > Reference Library > Technical Q&As > QuickTime > QuickTime for Windows >

QTML, c2pstr and Pascal strings


Q: I'm porting some QuickTime code from Macintosh over to Windows where I'm using Visual C++. I've run into some annoying issues creating pascal strings. I keep getting access violations calling c2pstr, my code works fine in CodeWarrior. Any idea what's going wrong?

A: The most likely reason for the access violations is that the string data is in read-only memory.

C strings start with the first character of the string and are terminated with a null character (ASCII value zero). Pascal strings start with a length byte and have no termination character.

On the Macintosh, they have different types:

C strings are char *

Pascal strings are unsigned char *

When you call c2pstr it overwrites the string in place to convert it from C to Pascal - it does this by shuffling all the characters forward, removing the terminating null character and inserting a length byte at the start.

For example, "hello\0x00" becomes "\0x05hello".

You should never pass a literal C string to c2pstr on any platform, there are a couple of reasons for this. First, the string data may be in read-only memory, and the modification will cause an access violation. Second, even if the first call succeeds, subsequent calls will continue to shuffle characters, generating garbage and causing memory corruption.

"\0x05hello" becomes "\0x06\0x05hello", assuming the byte after the string buffer is a null character.

While C compilers on the Macintosh support the "\p" convention for creating Pascal strings, example "\phello". Win32 compilers generally don't support this convention; they generate a string which is the same as "phello".

To help code portability, QTML (QuickTime Media Layer) frequently examines Pascal strings which have their length byte set to 112 (112 = 0x70 = 'p'), and checks to see if these strings are really mis-generated C strings. If they are, it calls c2pstr to convert them in place to Pascal strings. However, it only does this once, so it doesn't fall into the generating garbage trap described above.

Developers taking advantage of this QTML feature must take care to put such Pascal strings in writable memory. You may be able to configure your development system to do this.

Alternatively, you can make the string the initializer for a non-constant string buffer:

static char helloStr[] = "\phello";

Developers should note, there are recommended functions available for converting between C and Pascal Strings.

    CopyPascalStringToC
    CopyCStringToPascal
    c2pstrcpy
    p2cstrcpy


While these functions were written to allow in-place conversion, (i.e. The src and dst parameters can point to the same memory location) they do not suffer from the restriction of only doing in-place conversions which as mentioned, can cause you to incorrectly change the contents of a read-only buffer or easily cast away a const.


[Jan 21 2002]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.