tags:

views:

270

answers:

5

I have the following struct:

struct Datastore_T
{
 Partition_Datastores_T  cmtDatastores; // bytes 0 to 499
 Partition_Datastores_T  cdhDatastores; // bytes 500 to 999
 Partition_Datastores_T  gncDatastores; // bytes 1000 to 1499
 Partition_Datastores_T  inpDatastores; // bytes 1500 1999
 Partition_Datastores_T  outDatastores; // bytes 2000 to 2499
 Partition_Datastores_T  tmlDatastores; // bytes 2500 to 2999
 Partition_Datastores_T  sm_Datastores; // bytes 3000 to 3499
};

I want to set a char* to point to a struct of this type like so:

struct Datastore_T datastores;

// Elided: datastores is initialized with data here

char* DatastoreStartAddr = (char*)&datastores;
memset(DatastoreStartAddr, 0, 3500);

The problem I have is that DatastoreStartAddr always has a value of zero when it should point to the struct that has been initialized with data.

What am I doing wrong?

Edit: What I mean by zero is that the "values" in the structure are all zeros even after I initialize the structure. The address is not zero, it is the values in the struct that are zero.

Edit: I think I am asking the question wrong. Let's start over. If I have a struct that is initialized with data, and another object maintains a field member that is a pointer to that struct, if the struct is changed directly:

struct Datastore_T datastores;
char* DatastoreStartAddr = (char*)&datastores;

datastores.cmtDatastores.u16Region[0] = Scheduler.GetMinorFrameCount(); // byte 40,41
datastores.cmtDatastores.u16Region[1] = Scheduler.GetMajorFrameCount(); // byte 42,43

Shouldn't I be able to access these changes using the DatastoreStartAddr pointer?

EDIT: The following code tries to read the data set in datastores, but using the pointer to the struct:

            CMT_UINT8_Tdef PayLoadBuffer[1500]= {NULL};
            int TDIS = 0;
            int DIS = 0;
            int DSA = 0;

            //copy DataStore info using address and size offsets
            if ((PayLoadBuffer + TDIS + DIS) < IndvDEMMax)
            {
               memcpy((PayLoadBuffer + TDIS), Datastores+DSA, DIS);
               TDIS += DIS;
            }

In the memcpy((PayLoadBuffer + TDIS), Datastores+DSA, DIS) line, Datastores should point to structure and attempts to access an offset in that structure. But since the value is always zero, it copies zero in the PayLoadBuffer.

A: 

i just tested your code and it is not zero. Try to post bigger piece

Andrey
+4  A: 

I don't know why you are getting an address of zero, but I would guess the code you don't show has something to do with it. Some other points:

  • consider using an array of Partition_Datastores_T inside your struct
  • do not use magic numbers for struct sizes, you want sizeof(Datastore_T )
  • there is no need for the intermediate char*

Edit: Bobby, to answer your supplementary question - yes you should be able to access it through a pointer, but not through a char * (without jumping through some hoops). You want:

struct Datastore_T datastores;
struct Datastore_T * DatastoreStartAddr = &datastores;

and when you use that pointer:

DatastoreStartAddr->cmtDatastores.u16Region[0] = Scheduler.GetMinorFrameCount(); 

Please note the use of the -> operator.

anon
Such an explicit typecast to char* will be needed if you want to use pointer arithmetics with 1-byte-alignment. I also disagree with your "use an array" - the symbol names are there for easy identification/meaning of the variable. You would strip the meaning of the symbol prefixes "sm_" "tml" and so on if you have to use an array where you have to address the values by index and not by name.
Robert
@Robert The cast is not needed for memset(), which is what he's doing. And I didn't say "use an array", I said he should consider doing so. Without knowing how he's using the struct, it's hard to say if the named substructures have meaning or not.
anon
@Neil: if the code above would make the whole program you would be completely right: memset takes a generic void* pointer as first parameter and no explicit cast is necessary. But because the majority of posted code on SO are only fragments it's more likely that the cast to a char-pointer has a meaning (for example for speeding up coding following pointer arithmetics).
Robert
@Robert No, it is far, far more likely that the OP doesn't know what he is doing.
anon
@Neil: with all the question edits it becomes clearer and clearer that you are right.
Robert
@Robert To you maybe - not to me.
anon
Thanks for the help guys
Bobby
@Neil: This has a `C++` tag, so there's no need for the `struct` in front of the type name.
sbi
@sbi Thank you for that useless bit of information. Do you think I don't know that? Do you think my answer would be improved by, at the same time I am trying to work out what the OP is asking about, I change the syntax (which is quite legal C++) of his question? This is the sort of thing that gives mindless pedantry a bad name.
anon
@Neil: I'm not sure why you're so annoyed. Yes, I'm pretty sure you know that. I am, OTOH, not so sure everybody else does - especially not the OP. Since this usually unnecessary C-ism is something we see again and again, I feel the highest voted answer should point that out. If you feel different, that's fine with me, it's your answer, after all.
sbi
@sbi Your comment was explicitly addressed to me. If you wanted to point this out to Bobby you should have addressed it to him, added an answer of your own, or made a comment on the question itself.
anon
@Neil: Since I felt the answer was missing something, it was addressed to the person who wrote the answer.
sbi
A: 

You are doing it in the wrong order - should be like this

struct Datastore_T datastores;
char* DatastoreStartAddr = (char*)&datastores;
memset(DatastoreStartAddr, 0, sizeof(Datastore_T));

// Elided: datastores is initialized with data here

Now datastores is still initialized. And like everyone else says you might want this instead

struct Datastore_T * DatastoreStartAddr = datastores;
memset((void *)DatastoreStartAddr, 0, sizeof(Datastore_T));
Jeff
A: 

I'm assuming that the code you are not showing is correct. It might be wise to show it to us for scrutiny. Likely, what I say below is not the problem at all.

Because of the cast, it might be that you're having aliasing issues here. If you have set aggressive compiler optimization flags (e.g. -fno-strict-aliasing on gcc), the compiler would assume that those two pointers can never refer to the same thing, because they have different types. Then either or both of the representations might be cached in different CPU registers, so updates to one would never be reflected in the other.

Again, this is a long shot. Considering the size of your struct (I didn't see that when I started answering your duplicate question), it is very unlikely that it would reside anywhere else but in main memory. But you could try turn down your compiler optimizations and see if it makes a difference.

Thomas
A: 

At what point is your structure's values zero? If it's before the cast and memset(), the problem is with your initialization. If it is after the cast and memset(), then the values in your structure are zero because memset() overwrote with 0's the values you had initialized it with. The values in datastores should also be zero after the memset().

DAG