Getting data from memory block / pointer issues.

F

FaXiA

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);
 
B

Ben Pfaff

FaXiA said:
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.
 
B

Barry Schwarz

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>>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top