Copying buffers?

This is a discussion on Copying buffers? within the C Programming forums, part of the General Programming Boards category; Our assignment is to make a simplified stdio.h. I have most of it done and working, but I am having ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Copying buffers?

    Our assignment is to make a simplified stdio.h. I have most of it done and working, but I am having a problem with with my_fwrite. I am trying to copy the buffer that the user sends to me into the stream's buffer. I figured that I would just copy 1 byte at a time, using their size as an indicator on when to stop copying. Is there a better way to do this? (like copy a certain amount of bytes into my buffer all at once?) Im sure there is a way, but not quite certian on how to go about it. The way I am doing it isnt working, as I am getting the following errors/warnings. (BTW, this has to be done on a *nix system, so done on a linux gcc compiler.)

    gcc -O2 -Wall -pedantic -ansi -std=c99 -c -o my_fwrite.o my_fwrite.c
    my_fwrite.c: In function 'my_fwrite':
    my_fwrite.c:26: warning: dereferencing 'void *' pointer
    my_fwrite.c:26: error: void value not ignored as it ought to be
    my_fwrite.c:31: error: invalid lvalue in assignment
    make: *** [my_fwrite.o] Error 1

    Here is the relevant code :
    Code:
    typedef struct {
    char 	*buf;	/* pointer to buffer */
    int	fd;	/* file descriptor */
    int 	nbuf;  /* size of buffer */
    char	*first;	/* pointer to first character position */
    char	*last;	/* pointer to last + 1 character position */
    int	status;	/* see flags above */
    } MY_FILE;
    
    size_t my_fwrite(const void *ptr, size_t size, size_t nitems, MY_FILE *stream) {
    if((stream->status & MY_IOWRT) == 2) {
    	int i = 0;
    	int j = 0;
    	while(i <= nitems) {
    		if((int)stream->last == ((int)stream->nbuf + (int)stream->buf)) { //If buffer is full, write out buffer.
    			int write_return;
    				write_return = write(stream->fd, stream->buf, stream->nbuf);
    			if(write_return == -1) {
    				return i;
    			}
    			stream->first = stream->buf;
    			stream->last = stream->buf;
    		}
    		while(j <= size) {
    			*stream->last = (unsigned char)*ptr;
    			j++;
    		}
    		j = 0;
    		stream->last = stream->last + size;
    		(int)ptr = (int)ptr + size;
    		i++;
    	}
    return i - 1;
    }
    return MY_EOF;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > *stream->last = (unsigned char)*ptr;
    Try:
    *stream->last = *(unsigned char *) ptr;

    > (int)ptr = (int)ptr + size;
    Not sure, but you might be able to do:
    ptr = ptr + size;

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Not sure, but you might be able to do:
    ptr = ptr + size;
    No, you can't add void* pointers like that. The reason is that due to pointer arithmetic, that is actually:
    ptr = ptr + size * sizeof(*ptr);
    Since you can't dereference a void* like that, the compiler will yell at you. In order to do point arithmetic with void*, you have to cast it to something first.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >No, you can't add void* pointers like that. The reason is that due to pointer arithmetic
    Ok, then make that:
    Code:
    	ptr = (unsigned char *) ptr + size;

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Or simply do at the start
    const unsigned char *localPtr = ptr;

    Then use that for all your stepping and indexing.

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Or implement it in assembly using rep stosd.

    I'll probably get flogged for that one.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Salem
    Or simply do at the start
    const unsigned char *localPtr = ptr;

    Then use that for all your stepping and indexing.
    That's the best idea. Then you don't need all the casting.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Bubba
    Or implement it in assembly using rep stosd.

    I'll probably get flogged for that one.
    Yeah, but coming from you, it actually makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in 16 and 24-bit audio data into (32-bit) integer buffers
    By theblindwatchma in forum C Programming
    Replies: 2
    Last Post: 04-13-2008, 11:12 PM
  2. Deep and Shallow Copying
    By peckitt99 in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2007, 09:37 PM
  3. Copy to Buffers
    By egomaster69 in forum C Programming
    Replies: 3
    Last Post: 12-05-2004, 05:50 PM
  4. copying buffers
    By Nor in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2003, 06:54 AM
  5. Copying a file
    By rkjd2 in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2001, 10:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21