Getting data from memory block / pointer issues.

Discussion in 'C Programming' started by FaXiA, Feb 3, 2005.

  1. FaXiA

    FaXiA Guest

    Hi all,

    I'm working on a program that reads a structure from a file (1000
    records).

    The file read part works great, but I'm hitting a roadblock
    on howto read the data from the pointer.

    I'm aware that *(&WordPtr+(i*5)) gives me the AddrID at location of i
    (0-999)

    What is the right way of getting AddrName at for example struct row 50?

    Thanks.

    // parts of code.. :

    struct AddrData {
    unsigned int AddrID;
    char AddrName[15];
    };

    struct AddrData Addr;
    struct Addr *AddrPtr;
    int i;

    // allocate memory
    AddrPtr = malloc(sizeof(Addr)*1000);

    // open file, read data
    fread(&AddrPtr, sizeof(struct AddrData), 1000, filePtr);
     
    FaXiA, Feb 3, 2005
    #1
    1. Advertising

  2. FaXiA

    Ben Pfaff Guest

    "FaXiA" <> writes:

    > I'm working on a program that reads a structure from a file (1000
    > records).
    >
    > The file read part works great, but I'm hitting a roadblock
    > on howto read the data from the pointer.
    >
    > I'm aware that *(&WordPtr+(i*5)) gives me the AddrID at location of i
    > (0-999)


    I'm not sure why you'd want to express it that way. The natural
    way to refer to the (i+1)th AddrID is AddrPtr.AddrID. You
    seem to want to express that in terms of "words", but that's not
    usually a good way to do things in C.

    > What is the right way of getting AddrName at for example struct row 50?


    AddrPtr[50].AddrName (or AddrPtr[49].AddrName, depending on how
    you're defining "row 50").

    > struct AddrData {
    > unsigned int AddrID;
    > char AddrName[15];
    > };
    >
    > struct AddrData Addr;
    > struct Addr *AddrPtr;
    > int i;
    >
    > // allocate memory
    > AddrPtr = malloc(sizeof(Addr)*1000);


    When calling malloc(), I recommend using the sizeof operator on
    the object you are allocating, not on the type. For instance,
    *don't* write this:

    int *x = malloc (128 * sizeof (int)); /* Don't do this! */

    Instead, write it this way:

    int *x = malloc (128 * sizeof *x);

    There's a few reasons to do it this way:

    * If you ever change the type that `x' points to, it's not
    necessary to change the malloc() call as well.

    This is more of a problem in a large program, but it's still
    convenient in a small one.

    * Taking the size of an object makes writing the statement
    less error-prone. You can verify that the sizeof syntax is
    correct without having to look at the declaration.

    > // open file, read data
    > fread(&AddrPtr, sizeof(struct AddrData), 1000, filePtr);


    The & is incorrect; remove it.
    --
    Here's a tip: null pointers don't have to be *dull* pointers!
     
    Ben Pfaff, Feb 3, 2005
    #2
    1. Advertising

  3. FaXiA

    FaXiA Guest

    Got it working.

    Thanks for your help!
     
    FaXiA, Feb 3, 2005
    #3
  4. On Thu, 03 Feb 2005 11:36:34 -0500, "FaXiA" <>
    wrote:

    >Hi all,
    >
    >I'm working on a program that reads a structure from a file (1000
    >records).
    >
    >The file read part works great, but I'm hitting a roadblock
    >on howto read the data from the pointer.
    >
    >I'm aware that *(&WordPtr+(i*5)) gives me the AddrID at location of i
    >(0-999)


    Wrong for so many reasons:

    1. WordPtr is undefined. Based on your comment about what you want
    and Ptr suffix in the name, it looks like it should be a pointer to
    struct AddrData.

    If it is such a pointer, then using the & operator guarantees that
    you are no longer pointing at structures but at memory around the
    pointer itself.

    If you remove the &, then the correct arithmetic would be
    *(WordPtr + i). However, that expression does not have type unsigned
    int which your comment implies you want.

    2. On the other hand, WordPtr could be the first element of an array
    of struct. Then you would need *(&WordPtr + i) but you would still
    have the type problem.

    3. Maybe WordPtr is a char*. The you would need
    *(WordPtr + (i*sizeof(struct AddrData))). (It should be obvious that
    sizeof(struct AddrData) will not be 5 on most systems people here
    use.) Of course, there is still the type problem.

    >
    >What is the right way of getting AddrName at for example struct row 50?
    >
    >Thanks.
    >
    >// parts of code.. :
    >
    >struct AddrData {
    > unsigned int AddrID;
    > char AddrName[15];
    >};
    >
    >struct AddrData Addr;
    >struct Addr *AddrPtr;


    struct Addr is an undefined type. I assume you meant AddrData.

    >int i;
    >
    >// allocate memory
    >AddrPtr = malloc(sizeof(Addr)*1000);
    >
    >// open file, read data
    >fread(&AddrPtr, sizeof(struct AddrData), 1000, filePtr);


    This does not read the data into the allocated memory pointed to by
    AddrPtr. It attempts to read the data into AddrPtr itself. sizeof
    AddrPtr bytes later it invokes undefined behavior. Lose the &.



    <<Remove the del for email>>
     
    Barry Schwarz, Feb 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper
    Replies:
    1
    Views:
    706
    Showjumper
    Mar 19, 2005
  2. morrell
    Replies:
    1
    Views:
    967
    roy axenov
    Oct 10, 2006
  3. ben
    Replies:
    5
    Views:
    599
    Ulrich Eckhardt
    Jan 11, 2005
  4. Replies:
    10
    Views:
    706
    Chris Torek
    Feb 4, 2005
  5. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
Loading...

Share This Page