Re: Malloc()
Re: Malloc()
- Subject: Re: Malloc()
- From: Vince DeMarco <email@hidden>
- Date: Tue, 2 Apr 2002 10:20:24 -0800
On Tuesday, April 2, 2002, at 10:12 AM, Jonathan Wight wrote:
On 04/02/2002 11:31, "Vince DeMarco" <email@hidden> wrote:
On Monday, April 1, 2002, at 10:49 PM, Jason Moore wrote:
Hello all.. Sorry to keep bothering everyone with C questions, but this
one has me totally stumped..
I need to pass some data (in the form of a char* buffer) into a Unix
library function. I'm getting my data from an NSData object using
GetBytes:range:. My problem is that my buffers are not working.
i have:
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).
any ideas?
Try using malloc_size(buffer)
sizeof(buffer) will return the size of the buffer variable instead.
Vince is right - sizeof(buffer) is incorrect - but I'd suggest _not_ using
mallocsize. You wont want to use that function if you're trying to write
standard C, it's one of those common to most unix functions. Also
mallocsize
doesn't necessarily return the same value you passed to to the original
malloc - malloc could have tacked on a few extra bytes for it's
convenience.
The best way of getting the size of a malloc-ed block IMHO is merely by
storing the requested size of that block when you malloc-ed it. You
created
the memory so you should know how large the block of memory is. Just don'
t
discard that information.
Also consider using NSData/NSMutableData - they take the pain out of
memory
allocation for you.
Jon.
P.S. Disagreeing with an apple guru. I'm going to rot in hell I know it!
You are right though. Normally your program should not really care how
much it allocated, as Jon said
its best to keep track of the size yourself. and realloc etc.. when
appropriate if thats what you are
trying to do.
vince
_______________________________________________
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: Jonathan Wight <email@hidden>) |