Microcontroller › AVR › Structures and Memory
- This topic has 3 replies, 2 voices, and was last updated 11 years ago by AJISH ALFRED.
-
AuthorPosts
-
December 9, 2013 at 11:15 pm #2755Ray HallParticipant
I have a large struct (2084 bytes) and would like to know if the whole struct is loaded into memory every time you access just one of the values.
Example. This is a small part of the struct.
struct RealTimeData {
uint8_t startChar;
uint8_t PacketHi;
uint8_t PacketLo;
uint8_t PacketID;
uint16_t rpm;
uint16_t map;
uint16_t ect;
uint16_t iat;
uint16_t afr;
uint16_t batt;
uint16_t bap;
uint16_t swrilValvePos;
uint16_t swirlValvePosPercent;
uint16_t swrilDuty;
uint16_t swrilActual;
uint16_t swrilTarget;
uint16_t actualPressure;
uint16_t targetPressure;
uint16_t spillControlTime;
uint16_t injectorTime;
uint16_t speed1;
uint16_t speed2;
uint8_t gear;
uint8_t slipPercent;
uint16_t tpsMain;
uint16_t tpsSub;
uint16_t fpsMain;
uint16_t fpsSub;
uint16_t tpsMainMax;
uint16_t tpsMainMin;
uint16_t tpsSubMax;
uint16_t tpsSubMin;
uint16_t fpsMainMax;
uint16_t fpsMainMin;
uint16_t fpsSubMax;
uint16_t fpsSubMin;
uint16_t dbwMotorDuty;
uint8_t errorCntDBW;
uint8_t acRequest;
uint8_t acClutch;
uint8_t mainFan;
uint8_t subFan;
uint32_t crcCnt;
}data;
When I use this code…
void SpillTime(void)
{
data.spillControlTime = 2100;
}
Is the whole struct loaded in to memory. I am trying to understand if I need to use a pointer.
Ray.
December 10, 2013 at 5:19 am #10714AJISH ALFREDParticipantHi Ray,
Yes, as soon as you declare the variable data, that much memory will get allocated.
You can create a pointer by creating a pointer type ‘data’ like;
struct RealTimeData
{
.....
.....
}*data;Use dynamic memory allocation with the help of malloc()
data = (struct RealTimeData*) malloc (sizeof (struct RealTimeData)); //you may need to //modify this line
//to get compiled
You can use it in code like
When I use this code…
void SpillTime(void)
{
data -> spillControlTime = 2100;
}
December 10, 2013 at 9:12 pm #10718Ray HallParticipantThank you for the reply.
I have never used Malock before. Why is it needed in this case.
Ray.
December 12, 2013 at 4:23 am #10727AJISH ALFREDParticipantmalloc() is a method used for dynamic memory allocation. malloc() creates a block of memory whenevr that function is called and in our case we are pointing our structure pointer to that memory block.
“Every pointer needs to be pointed to a memory before it is accessed”.
In this case the memory of the size of the structure will get allocated only after the malloc() is called. Once you’ve done, you can remove that memory using the function free().
Suggest you to google it for a good tutorial
http://www.codingunit.com/c-tutorial-the-functions-malloc-and-free
http://www.cprogramming.com/tutorial/c/lesson6.html
Still having doubts, please feel free to post here.
-
AuthorPosts
- You must be logged in to reply to this topic.