• 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: init char buffer to null
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: init char buffer to null


  • Subject: Re: init char buffer to null
  • From: email@hidden
  • Date: Wed, 13 Dec 2006 14:57:54 +1100

char *buffer;
buffer = malloc(delimiterLocation);
[input getBytes:buffer range:NSMakeRange(index, delimiterLocation)];
// do something
free(buffer);

ok this is simple, my problem lies with buffer initialisation,
i'm not that used to "C" and i was wondering if
Objective-c or Carbon had a specefic function to do such thing...
like a fill function...or something...

the problem is funny, is i read from the file an integer let's say 10 and at position 3
I had 5 because of old memory, i end up with the number 105 :)


anyways, I know it's a dumb question, and i did try and find in the docs
but i dont know what i am looking for so, i get a lot of nothing.

memset will initialise memory for you but I don't actually think you want to do that. You need to post more of the surrounding code.

I believe what he's saying is he's reading a file containing textual data, e.g. "10\n", and ending up with "103x8dehetkladg..." because he's not NULL terminating it. So, to the original poster, while you can get what you want by using calloc instead of malloc (calloc simply zeroes the memory it gives you, which you can do manually with memset as well), what you really want to do is fix the bug whereby you're not NULL terminating what you read. The easiest way to do that, going off the code above, would be just to add, after your getBytes:range: call:


buffer[delimiterLocation] = 0

Much more efficient than zeroing the whole buffer.

Wade Tregaskis

    ICQ: 40056898
    AIM, Yahoo & Skype: wadetregaskis
    MSN: email@hidden
    iChat & email: email@hidden
    Jabber: email@hidden
    Google Talk: email@hidden

    http://homepage.mac.com/wadetregaskis/

-- Sed quis custodiet ipsos custodes?


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: init char buffer to null
      • From: Chris Suter <email@hidden>
References: 
 >init char buffer to null (From: Sandro Noel <email@hidden>)
 >Re: init char buffer to null (From: Chris Suter <email@hidden>)

  • Prev by Date: Re: init char buffer to null
  • Next by Date: Re: init char buffer to null
  • Previous by thread: Re: init char buffer to null
  • Next by thread: Re: init char buffer to null
  • Index(es):
    • Date
    • Thread