• 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: Malloc()
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Malloc()


  • Subject: Re: Malloc()
  • From: Development <email@hidden>
  • Date: Tue, 2 Apr 2002 14:16:11 +0200

Hi,

char *buffer;
buffer = malloc(bufferSize);
NSLog(@"buffer size is: %i", sizeof(buffer));

here is where the problem is. the log output is 'buffer size is: 4', even when i change the malloc
statement to read buffer = malloc(129) (129 is the size of the buffer i need in my test case).

When you ask for C to give you sizeof(buffer) you're actually asking it to give you the size of the variable called buffer. That variable is a pointer, and pointers on a Mac are 4 bytes long, so it's giving you the right answer.

What you're actually trying to find is the size of the data that buffer points to, so you might think that sizeof(*buffer) would work. Not so!


Shouldn't

int buffersize = GetPtrSize(buffer);

do the job?
At least when creating the pointer with

buffer = NewPtr(129);

or isn't this supported in cocoa?

hth,
Dirk Stegemann
_______________________________________________
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: 
 >Re: Malloc() (From: Pete Yandell <email@hidden>)

  • Prev by Date: Re: Cocoa:registerForDraggedTypes and NSTableView
  • Next by Date: Re: Shared objects and other data
  • Previous by thread: Re: Malloc()
  • Next by thread: Re: Malloc()
  • Index(es):
    • Date
    • Thread