EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Structures and Memory

Structures and Memory

|

Microcontroller › AVR › Structures and Memory

  • This topic has 3 replies, 2 voices, and was last updated 11 years, 11 months ago by AJISH ALFRED.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • December 9, 2013 at 11:15 pm #2755
    Ray Hall
    Participant

    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 #10714
    AJISH ALFRED
    Participant

    Hi 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 #10718
    Ray Hall
    Participant

    Thank 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 #10727
    AJISH ALFRED
    Participant

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

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • KiCad custom symbol definition correct approach November 12, 2025
  • Anyone In The US Ordered From AliExpress Recently? November 12, 2025
  • My Advanced Realistic Humanoid Robots Project November 12, 2025
  • restarting this Christmas project November 12, 2025
  • WTB: "The Theory Of Servicing AM, FM, And FM Receivers" by Clarence R. Green and Robert M. Bourque November 11, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise