Re: [OT] A bit confused on pointers...
Re: [OT] A bit confused on pointers...
- Subject: Re: [OT] A bit confused on pointers...
- From: Camillo Lugaresi <email@hidden>
- Date: Thu, 29 Dec 2005 03:37:44 +0100
On 29/dic/05, at 00:20, Sanri Parov wrote:
Hi everybody,
Cocoa is a great superset of C because... it has very little use of
pointers. :-))
I'll never understand why so many people have trouble with pointers,
yet are perfectly happy with graphics APIs (which have coordinates),
file I/O APIs (which usually have a file pointer), text fields (which
have a cursor), etc...
A pointer is essentially an address into memory. Dereferencing a
pointer means taking the memory at that address and interpreting it
according to the pointer type (int * -> an int at that address,
NSArray * -> an NSArray instance at that address, etc.). Adding (or
subtracting) n to a pointer means moving it n <type of the thing
pointed to> forward (or backward): adding 1 to an int * advances it
to the next int (= 4 bytes ahead, on a 32-bit platform); adding 1 to
a chat * advances it to the next char (= next byte); adding 24 to a
Thing * advances it 24 Things forward (adds 24*sizeof(Thing) to the
address).
Probably it' s because it's getting late now I am writing, but
here's the question.
I've got a file read with:
fread (buffer, 1,size,pFile);
in a for() cycle that reads several files. "buffer" is a char* and
so another variable called finalBuffer (char*)
All I want to do is to costantly update finalBuffer so it's the sum
of all the single buffer from every single file.
finalBuffer +=*buffer
is the right choice?
If you wanted to stitch a bunch of images together, would you try
adding the first pixel of an image to the coordinates of the other?
Probably not. What you really want to do, if I understand what you
wrote, is to concatenate the contents of several files into a single
memory buffer. You can do that by telling fread to load each file's
data into the buffer right after the data from the previous file,
like this:
char *buffer, *next;
size_t size, n, total = 0;
next = buffer = malloc(LOTS_OF_MEMORY);
for (...) {
....
if (total + size > LOTS_OF_MEMORY) crash_and_burn();
n = fread(next, 1, size, file); /* load data at address next; n is
the number of items read */
next += n; /* start loading the next file right where the data for
this file ends */
total += n;
....
}
At the end of the loop, next points after the last byte read, while
buffer points at the beginning of the buffer, and has all the data
laid out ahead in a nice orderly fashion. You should also check
fread's return value for errors, of course.
Pardon me, but after having coded just on databases and
NSTableViews my mind is starting to sink ...
Many thanks for all the kind replies and Happy New Year to everybody.
Happy new year to you too.
Camillo
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden