By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,008 Members | 2,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,008 IT Pros & Developers. It's quick & easy.

structs ALWAYS on the stack?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"Casper" <ca****@jbr.dk> wrote in message
news:sx********************@wagner.videotron.net.. .
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
Jul 22 '05 #2

P: n/a
Casper wrote:
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.
Jul 22 '05 #3

P: n/a
E. Robert Tisdale wrote:
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
Jul 22 '05 #4

P: n/a
"Casper" <ca****@jbr.dk> wrote in message
news:sx********************@wagner.videotron.net.. .
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.
Jul 22 '05 #5

P: n/a
Casper wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

P: n/a
Ok thanks for all the answers, I am clear on the subject now! :)

Cheers,
Casper

Karl Heinz Buchegger wrote:
Casper wrote:
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.

Jul 22 '05 #7

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Casper wrote:

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?


I think the OP (or his informant) is getting confused with C#.

john
Jul 22 '05 #8

P: n/a
"Casper" <ca****@jbr.dk> wrote in message
news:sx********************@wagner.videotron.net.. .
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.
Jul 22 '05 #9

P: n/a
Casper wrote:
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
Jul 22 '05 #10

P: n/a
In message <41**************@bubblescope.net>, Azumanga
<az******@bubblescope.net> writes
Casper wrote:
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.
If you didn't do any of these, you leave it ^_^

Azumanga


--
Richard Herring
Jul 22 '05 #11

P: n/a

"Casper" <ca****@jbr.dk> wrote in message
news:sx********************@wagner.videotron.net.. .
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.
Jul 22 '05 #12

P: n/a
In message <41********@news1.prserv.net>, jeffc <no****@nowhere.com>
writes

"Casper" <ca****@jbr.dk> wrote in message
news:sx********************@wagner.videotron.net. ..
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?


Whoever designed C#, where the struct/class distinction is not that of
C++.
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.


--
Richard Herring
Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.