structs ALWAYS on the stack?

E

E. Robert Tisdale

Casper said:
I've been told that structs, being value types,
always reside on the stack and, thus,
need not be deleted manually but simple go out of scope in due time.

I have also read that all C++ *new* keywords
must be coupled with an eventual *delete* to free the memmory again.

My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?

Example:

struct tree {
struct tree *parent;
struct tree *child;
String itemData;
};

tree* prootNode = new tree;
tree* psubNode = new tree;

prootNode->child = psubNode;
psubNode->parent = prootNode;
// ...etc...

struct tree rootNode;
struct tree subNode;

rootNode.child = &subNode;
subNode.parent = &rootNode;

Both rootNode and subNode are automatically destroyed
when the thread of execution passes out of the scope
where rootNode and subNode were defined.
 
S

Sharad Kala

Casper said:
I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.
I have also read that all C++ *new* keywords must be coupled with an
eventual *delete* to free the memmory again.

This is correct.
My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?

You have to call delete else you do a memory leak.

-Sharad
 
B

Bill Thompson

Casper said:
I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.

I have also read that all C++ *new* keywords must be coupled with an
eventual *delete* to free the memmory again.

My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?

Example:

struct tree {
struct tree *parent;
struct tree *child;
String itemData;
};

tree* rootNode = new tree;
tree* subNode = new tree;

rootNode->child = subNode;
subNode->parent = rootNode;
...etc...

Thanks in advance,
Casper

It would be more correct to state that variables declared inside a function
are stored on the stack (as are copies of variables in function parameters).
If you declare an instance of a struct outside of a function definition, it
is certainly NOT on the stack.

struct tree A_Global_Item;

foo()
{
struct tree A_Stack_Item;
struct tree *An_Allocated_Item = new(struct tree);
}

A_Global_Item is not stored on the stack.
A_Stack_Item is stored on the stack.
An_Allocated_Item is a pointer that is stored on the stack; but what it
points to, the allocated memory that will contain the tree object, is not
stored on the stack, but in 'wherever new gets its memory from' (e.g. the
heap).

When you are done using the tree object, the memory should be returned to
the heap by using the delete function.
 
K

Karl Heinz Buchegger

Casper said:
I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.

Who told you this nonsense?

A struct object behaves the very same way as any other object.
If it gets auto created as in

struct test
{
int a;
};

void foo()
{
int i;
test j;
}

it gets destroyed when the scope of that object is left (a scope
is basically the enclosing block of that variable - from the '{'
to the '}' )

There is no difference between i and j in the above example: both
objects come to life when the function is entered and are destroyed
when the function is left. If all of this happens on the stack or
not, is implementatio dependent. There are CPU's out there which
don't have a stack at all.

On the other hand, if you allocate something in the freestore, then
you are responsible for deleting it.

void foo()
{
test* j;

j = new test; // alllocate an object in the freestore

...

delete j;
}

So yes. Every 'new' has to be paired with a 'delete' during the execution
of the program, otherwise you have a memory leak.
 
C

Casper

I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.

I have also read that all C++ *new* keywords must be coupled with an
eventual *delete* to free the memmory again.

My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?

Example:

struct tree {
struct tree *parent;
struct tree *child;
String itemData;
};

tree* rootNode = new tree;
tree* subNode = new tree;

rootNode->child = subNode;
subNode->parent = rootNode;
....etc...

Thanks in advance,
Casper
 
C

Casper

E. Robert Tisdale said:
struct tree rootNode;
struct tree subNode;

rootNode.child = &subNode;
subNode.parent = &rootNode;

Both rootNode and subNode are automatically destroyed
when the thread of execution passes out of the scope
where rootNode and subNode were defined.

What then if my rootNode is defined globally and I have an algorithm
which recursively mounts nodes to my rootNode. Does this mean all other
than my rootNode seizes to exist after returning from my "mount-method"?

What I want to do is create a tree which I can reference nodes to from a
secondary data structure. I need not methods so structs seemed most
effecient in my view.

Regards,
Casper
 
A

Andrew Koenig

Casper said:
I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.

They need not be deleted manually unless they were allocated manually. Just
like ints.
I have also read that all C++ *new* keywords must be coupled with an
eventual *delete* to free the memmory again.

Yes. Code need not be *written* that contains a delete for every new, but
code must be *executed* that contains a delete for every new. So, for
example, you could use new to allocate several objects and then delete them
in a loop.
My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?

Example:

struct tree {
struct tree *parent;
struct tree *child;
String itemData;
};

tree* rootNode = new tree;
tree* subNode = new tree;

rootNode->child = subNode;
subNode->parent = rootNode;

At some point, you will need to delete rootNode and subNode unless you want
a memory leak.
 
A

Azumanga

Casper said:
I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.

I have also read that all C++ *new* keywords must be coupled with an
ev

The simple and quick answer :)

If you new it, you delete it
If you new[] it, you delete[] it
If you malloc it, you free it
If you didn't do any of these, you leave it ^_^

Azumanga
 
R

Richard Herring

Azumanga said:
Casper said:
I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.
I have also read that all C++ *new* keywords must be coupled with an
ev

The simple and quick answer :)

If you new it, you delete it
If you new[] it, you delete[] it
If you malloc it, you free it

Better, you only do these things as parameters to the constructor of an
appropriate smart pointer, and it does the rest.
 
J

jeffc

Casper said:
I've been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.

structs, like any other data, are not necessarily value types. Who said
that they were?
I have also read that all C++ *new* keywords must be coupled with an
eventual *delete* to free the memmory again.

My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?

If you create a struct with new, then you must delete it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top