• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Pascal String Woes ...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pascal String Woes ...


  • Subject: Re: Pascal String Woes ...
  • From: Shawn Erickson <email@hidden>
  • Date: Wed, 11 Feb 2004 13:35:14 -0800

Humm... looking at the docs I guess it is correct to use 256 for buffer length in the function call but the docs are some what unclear ("accounting for the length byte" by including it or not?). I assume it means to include it in the byte count... so what I outlined below is smaller then it likely should be.

-Shawn

On Feb 11, 2004, at 1:05 PM, Shawn Erickson wrote:


On Feb 11, 2004, at 12:34 PM, J Nozzi wrote:

List:

Okay, I admit that my standard C knowledge is severely lacking here. I've been *meaning* to read the $50 books I bought, really I have. ;-) In the mean time, the following code dies at the indicated line (// DIES HERE) with a SIGSEGV / EXEC_BAD_ACCESS.

NSString * strA = [preferences objectForKey:@"contentsa"];
NSString * strB = [preferences objectForKey:@"contentsb"];
StringPtr strAPtr;
StringPtr strBPtr;

CFStringGetPascalString((CFStringRef) strA, strAPtr, 256, kCFStringEncodingASCII); // DIES HERE
CFStringGetPascalString((CFStringRef) strB, strBPtr, 256, kCFStringEncodingASCII);

CFStringGetPascalString is expecting you to provide it with a pointer to a buffer but you are not allocating such a buffer. In other-words you are handing it two pointers that are not initialized (strAPtr & strBPtr) and they do not point to any allocated block of memory.

So either (written in mail)...

StringPtr strAPtr = CFStringGetPascalStringPtr((CFStringRef) strA, kCFStringEncodingASCII);
StringPtr strBPtr = CFStringGetPascalStringPtr((CFStringRef) strB, kCFStringEncodingASCII);

...or...

String255 strAPtr; //only valid for this code block (allocated on the stack)
String255 strBPtr; //only valid for this code block (allocated on the stack)

CFStringGetPascalString((CFStringRef) strA, &strAPtr, 255, kCFStringEncodingASCII); //255 to account for length byte
CFStringGetPascalString((CFStringRef) strB, &strBPtr, 255, kCFStringEncodingASCII); //255 to account for length byte

...or...

StringPtr strAPtr = (StringPtr) malloc(sizeof(char) * 256);
StringPtr strBPtr = (StringPtr) malloc(sizeof(char) * 256);

CFStringGetPascalString((CFStringRef) strA, &strAPtr, 255, kCFStringEncodingASCII); //255 to account for length byte
CFStringGetPascalString((CFStringRef) strB, &strBPtr, 255, kCFStringEncodingASCII); //255 to account for length byte

...do something with pascal strings...

free(strAPtr);
free(strBPtr);

-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Pascal String Woes ... (From: J Nozzi <email@hidden>)
 >Re: Pascal String Woes ... (From: Shawn Erickson <email@hidden>)

  • Prev by Date: Re: Pascal String Woes ...
  • Next by Date: NSTableView selection and Drag&Drop
  • Previous by thread: Re: Pascal String Woes ...
  • Next by thread: Re: Pascal String Woes ...
  • Index(es):
    • Date
    • Thread