开发者

Setting pointers to structs

开发者 https://www.devze.com 2022-12-25 06:29 出处:网络
I have the following struct: struct Datastore_T { Partition_Datastores_TcmtDatastores; // bytes 0 to 499

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_Datast开发者_开发技巧ores_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.


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.


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


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


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.


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号