Is the value of an object stored in memory independently, or is the entire object itself stored in memory when saved?

Storing values in memory is straightforward, but when it comes to objects, I'm curious about how exactly they are stored. Can you explain this process?

Answer №1

Exploring how objects are stored in memory can provide valuable insights:

Typically, most objects store all their properties within a single memory block ("a" and "b"). Each memory block includes a pointer to a map that outlines the object's structure. Any named properties that exceed the object's limit are usually placed in an overflow array ("c" and "d"). Numbered properties, on the other hand, are often stored separately in a continuous array.

To delve deeper into this topic, check out this resource.

Answer №2

When an object is created, the values of its member variables are stored in memory. These values are linked to the class that the object belongs to, forming a cohesive unit with the associated code block. Essentially, an object comprises data along with specific function calls tailored to manipulate it.

Take for example a language like C which is based on functions. Consider the following structure:

struct Square (  
    int upperX;  
    int upperY;  
    int lowerX;  
    int lowerY;  
}  

In this case, we are storing information about a square in memory...

Square s = malloc(sizeof(Square));  
s->upperX = 10;
s->upperY = 10;
s->lowerX = 20;
s->lowerY = 20;

The above code snippet demonstrates operations carried out on a Square object. It leverages the memory allocated to hold the Square struct...

Square's representation in memory is straightforward with its corresponding values.

Now, in an object-oriented language such as C++ or Java, the implementation may look slightly different while achieving the same result. For instance:

class Square {
    private int upperX;
    private int upperY;
    private int lowerX;
    private int lowerY;
    
    public Square(int x1, int y1, int x2, int y2) { /* code to assign values to members */ }
    
    public draw() { /* code to draw...*/ }
}

An object can be instantiated like so:
Square s = new Square(10, 10, 20, 20);

Behind the scenes, the allocation and assignment processes occur, but now 's' represents an object because the actual Square "struct" in memory is tied to the logic within the Square class.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What causes errors in URL routeProvider with AngularJS?

Implementing routeProvider in Angular JS allows me to structure my links like this: www.site.com/profile#/profile/profession/3 However, when trying to access the page, Angular JS displays the following error message: Uncaught Error: Syntax error, unreco ...

The Redux state fails to start with the default initial state

I'm a newcomer to react-redux. In my reducer, I have the following structure: const initialState = { Low: [ { id: 0, technologyId: 0, technology: '', type: '', ...

Define the format for the output file name in TypeScript

I am trying to change the filename of a TypeScript generated js file. How can I accomplish this? For instance, I currently have MyCompany.ClassA.ts The default output filename is MyCompany.ClassA.js However, I would like the output filename to be MyComp ...

Manipulating float values in jQuery is similar to formatting numbers in PHP with the number_format() function

I am looking for a way to output floating numbers in JavaScript that functions exactly like the number_format function in PHP. Sample Javascript Code Math.round(totalCredit).toFixed(2) Sample PHP Code echo number_format(22212 , 2); The code above re ...

Make sure to validate a form when submitting from an external source rather than through an HTML

In my form, there are various input fields (some acting as inputs even if they're not). Each field is validated upon submission by angular validation and html attributes like required, ng-maxlength, minlength, etc. Now, we want to implement a keyboar ...

Retrieve data from the controller of the selected element on the subsequent page using AngularJS

Currently, I am in the process of developing an ecommerce platform using angularjs. In my controller, I have a list of various products structured like this: $scope.products = [ {imgLink: 'product1.jpg', name: 'Wingtip Cognac Oxford&apo ...

JavaScript failing to trigger autoClose functionality in Bootstrap dropdown

I tried implementing this code to toggle a bootstrap 5 dropdown, but the autoClose feature didn't work as expected. const dropdown = new bootstrap.Dropdown(document.querySelector('.dropdown-menu'), { autoClose: true }) dropdown.toggle() ...

Opening a Material UI dialog results in a change in the style of other components

Whenever I open the Dialog component from a button on the AppBar component, the margin on my navbar elements changes unexpectedly. I have checked the HTML using dev tools and there are no CSS changes on either of the components. Any suggestions on how to p ...

ExtJS 5 - implementing multiple level grid grouping

Currently, I am trying to find a solution to organize my data by months and then further group the months by years. At the moment, I have only been able to group the data by years. Is there someone who can assist me with this issue? This is my model: Ex ...

Scrolling to the complete height of a div using scrollTop

Experience an automated scroll to the bottom of the div, followed by a smooth upward motion over 5 seconds, all while looping seamlessly. A small issue arises where the code only recognizes the standard height set for the div, not its actual scrollable he ...

Easy method for importing videos into NextJs

Import Coding Guidelines Encountering an error after importing the code, unable to find any solutions online ...

What is the best way to retrieve the current element during an event in a React application

Code: class Locations extends React.Component { render() { let { home, work, onChange } = this.props; return <div className="controls"> <GoogleMapLoader containerElement={ <d ...

only one of the ng-bind-html elements on the page is functioning

I am currently working on an AngularJS application and encountered a problem with this block of code. Only the first ng-bind-html works for me: <div ng-bind-html='newsTitle'> <div ng-bind-html='newsDetail'></div> &l ...

Press and hold feature using CSS or JavaScript

When working with a jQuery draggable element, my objective is to change the cursor to a move cursor when clicking and holding the header. I have attempted using CSS active and focus properties, but so far no changes are taking place. ...

Struggling to get the knockout js remove function to function properly within a nested table structure

I've been encountering issues while trying to eliminate the formulation elements with the 'Delete comp' link. I can't seem to figure out why it's not functioning as expected. Moreover, the 'Add another composition' link o ...

Prevent scrolling using mousewheel click (auxclick event)

Currently working with Angular 9 and encountering an issue with the (auxClick) event triggering on a mousewheel click. The problem arises when attempting to use this event while there is a scroll present on the page, as it does not trigger correctly due to ...

What is the best way to align a jqdatetimeinput jqwidget in the vertical center?

Facing some alignment issues here. My goal is to perfectly center my datetimeinput widget from jqWidget. I aim to have the datetimeinput vertically centered on the page, and directly underneath it, a centered date range based on the selected datetimeinput. ...

Angular Unit testing error: Unable to find a matching route for URL segment 'home/advisor'

Currently, I am working on unit testing within my Angular 4.0.0 application. In one of the methods in my component, I am manually routing using the following code: method(){ .... this.navigateTo('/home/advisor'); .... } The navigateTo funct ...

What is the method to implement an invisible filter in AngularJS?

When I enter text into the input field in a list view, it filters my list based on the visible username. For example, typing "pa" will display only one row that matches the filter. However, when I type in the "keyword filter" input field, which is not visi ...

What is the best way to retrieve a specific value from a JSON file using a numerical identifier?

Imagine a scenario where there is a JSON file structured like this: { "data": [ { "id": "1", "name": "name1" }, { "id": "2", "name": "name2" } ] } If you know the ...