Pointer help

This is a discussion on Pointer help within the C Programming forums, part of the General Programming Boards category; I need help understanding why my pointer doesn't update Code: int readThis( unsigned int addr, void* mybuf ) { lseek( ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    15

    Pointer help

    I need help understanding why my pointer doesn't update

    Code:
    int readThis( unsigned int addr, void* mybuf ) {
    
    lseek( infd, rootAddr, SEEK_SET ); // root Addr calculation omitted from thread
    
    read( infd, mybuf, PAGESIZE); // PAGESIZE is the global variable used for the block size
    
    bcopy(mybuf, &VMCACHE[victimPage][0], PAGESIZE); // copies the contents read from the previous instruction into a cache (char VMCACHE[x][y])
    mybuf = &VMCACHE[victimPage][offSet]; // attempts to set the contents to the specific data within the cache
    
    Record* pRecord = mybuf;
    Record ppRecord = *pRecord;
    printf("%s\n", ppRecord.string); // actually outputs the right data
    
    
    return 0;
    }
    Code:
    int listArtists(unsigned int addr) {
    Record thisbuf;
    
    readThis( addr, &thisbuf ); // calling the function above
    printf("Buffer:%s\n", thisbuf.string); // outputs different data from mybuf, although it seems like they point to the same set of data
    }
    Basically, what my code does is update fetch a block sized amount of data and tries to store it into the cache. Then I try to update the buf to hold the selected value within that block.

    I have deleted a lot of extraneous code also, but the main thing is that the first printf involving ppRecord.string outputs something entirely different from thisbuf.string.

    Based on the code above, since the ppRecord points to the same destination of mybuf and mybuf is the pointer passed by thisbuf, shouldn't the two printf statements be equivalent?

    ADDITIONAL INFORMATION:
    I forgot to mention that thisbuf.string prints the original value assigned to the location of mybuf from the read() function. Therefore thisbuf never gets updated from any new assignment after the initial read.
    Last edited by Salem; 03-09-2011 at 11:42 AM. Reason: restore!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Your logic seems to be flawed.

    1) thisbuf is declared as type Record
    2) You're copying PAGESIZE bytes into that buffer
    3) You're then pointing that buffer to an index within itself

    It seems to me like you'll be accessing memory you're not supposed to when you start poking around in the buffer. Shouldn't you be declaring thisbuf as a pointer to Record instead in your listArtists() functions? Then you can read PAGESIZE bytes into a separate PAGESIZE buffer and point your passed-in buffer to a position within that PAGESIZE buffer. Better yet, to make your readThis() function reentrant, you should allocate memory for the passed-in buffer and then copy the relevant data from your PAGESIZE buffer to the newly allocated memory that the passed-in buffer now points to.

    Code:
    void readThis(Record **buf)
    {
      char pageBuffer[PAGESIZE];
      bcopy(pageBuffer, &VMCACHE[victimPage]0], PAGESIZE);
      *buf = malloc(sizeof(**buf));
      bcopy(*buf, pageBuffer + offset, sizeof(**buf));
    }
    
    void listArtists()
    {
      Record *thisbuf;
      readThis(&thisbuf);
    }
    Something like that. Untested, YMMV. Looking at it though, the memory allocation is unnecessary:
    Code:
    void readThis(Record *buf)
    {
      char pageBuffer[PAGESIZE];
      bcopy(pageBuffer, &VMCACHE[victimPage]0], PAGESIZE);
      bcopy(buf, pageBuffer + offset, sizeof(*buf));
    }
    
    void listArtists()
    {
      Record thisbuf;
      readThis(&thisbuf);
    }
    Maybe I'm missing something though.
    Last edited by itsme86; 03-09-2011 at 09:25 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    I'm not allowed to modify the original listArtists() function in any way. With the original seek and read (reading from the original addr as opposed to rootAddr) the thisRead() function works as required.

    I'm sorry but I still don't have a full grasp of C (first big project) therefore I still have trouble understanding pointer logic. But in laymens terms, the reason why I point to an index within the buffer is because after I store the page, I need to access a "Record" from within that page.

    When I create an additional record within the function to point to the new location described by the pointer mybuf, it prints the right data. However, thisbuf doesn't seem to point where mybuf does, if that makes any sense.

    Also, I looked at your code and bcopy(x, y, z) copies z bytes from x into y. Sorry but I don't understand how copying from an unitialized array would work?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,680
    Quote Originally Posted by vexed View Post
    When I create an additional record within the function to point to the new location described by the pointer mybuf, it prints the right data. However, thisbuf doesn't seem to point where mybuf does, if that makes any sense.

    Also, I looked at your code and bcopy(x, y, z) copies z bytes from x into y. Sorry but I don't understand how copying from an unitialized array would work?
    In order to change a value you must pass it by reference or return the value in a return statement.

    You change the local copy of the pointer; but since it was NOT passed by reference it does not change in the original program.

    To change the address a pointer points to inside a called function; you must pass in a pointer to a pointer, not just a pointer.

    Tim S.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    Code:
    readThis( addr, &thisbuf );
    Is this not passing by reference? The value of "thisBuf" is changed once to the value read from read( infd, mybuf, PAGESIZE). Sorry if I'm a little slow to catching on. Could you propose an example of how I could fix my reference problems?

    Thank you guys so much for being patient with me.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    Thanks guys you helped me solve the problem. You were right, I need to update the values at the location given using the bcopy function.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Please don't delete your posts just because you got your answer.
    It defeats the whole purpose of a forum where people can read and learn from others.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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