SixOThree
SixOThree

Reputation: 771

Does the amount of code make a data structure larger?

I have an arraylist of items that only have maybe ten variables in them. But the items have a good bit of code, and I'd like to add more. I'm curious how this will effect the size of my structure. My intuition tells me each one has a stack, but with arguments getting passed around and stuff I've probably not considered I'm unsure. So roughly just how much is code adding to my data structure?

Upvotes: 4

Views: 226

Answers (7)

Evan Rogers
Evan Rogers

Reputation: 768

The size of an object instance is (mostly) the sum of its data members. Function members are not stored per-instance, and have no effect on instance size.

Your program has a single stack per thread. When a function is called, stack memory is reserved for each parameter and variable of that function, and freed when it returns.

Your object is stored on the stack if it is instantiated in a function without new. If you use new, only a reference is stored on the stack, and the instance itself is stored on the heap.

:)

Upvotes: 2

Charlie
Charlie

Reputation: 1598

Even though you put code in the same set of curly braces as data, the compiler will separate it out and put it in a different memory section.

That way, you only need one copy of the code - it's not necessary to make a new copy for each item that uses it.

Only in very large projects does the actual size of the compiled code become a problem; usually, it's not much bigger than the source text. On small programs, it's typically a few hundred K or a meg or two at most. It's not worth worrying about on modern machines.

Upvotes: 3

George Armhold
George Armhold

Reputation: 31074

Each object does not have its own stack. Stacks are per thread/process, not per object.

Upvotes: 1

aJ.
aJ.

Reputation: 35460

The class member functions on which object works are stored differently and it has no effect on size of the object. Where as data members inside the class will be part of the object and object size will change if you change the type of the variable.

ex:

class Test
{
    1. member functions

    //it does not matter how many functions you have, this section does not contribute
    // to object size

    2. member variables
    //this contributes to the object size, define an int or  double etc will change the object size
}

Upvotes: 1

FlySwat
FlySwat

Reputation: 175603

Code is not stored on the heap.

Regardless of how many objects you create, you'll only have the code in one place.

Upvotes: 1

JohnOpincar
JohnOpincar

Reputation: 5813

The code associated with a class is not usually duplicated per-instance so adding code should not impact the size of your arraylist.

Upvotes: 3

Mitch Wheat
Mitch Wheat

Reputation: 300579

Code does not add to the size of your structure.

Upvotes: 10

Related Questions