Archived

This topic is now archived and is closed to further replies.

memory Question

This topic is 5653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say for instance you declare an instance of a struct: struct Cstrct { int myint; }; Cstrct *myStruct; ///////////////// Because I declared it as a pointer, does it mean, that memory will only be allocated to it when I initlize it?, e.g: myStruct = new Cstrct; --------------- Gaming is Life ---------------

Share this post


Link to post
Share on other sites
When you create a pointer it automaticaly points to a place in memory, to get over this problem do this
Cstrct *myStruct = 0;
It is now null pointer and will only ahve info when you do somthing like
myStruct = new Cstrct;

Think thats what u were asking
Cheers!


Share this post


Link to post
Share on other sites
You can, but the real reason for setting a pointer to null is to avoid dereferencing it unintentionally wreaking havoc on the system (most of the time it just causes an access violation). A pointer that has been initialized without a value is called a "wild pointer." It''s considered good practice to initialize pointers to 0, unless you immediately assign them some other value.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
Oh, if the question was over whether NULL was the functional equivalent of 0, then yes. However, usage of NULL is normally frowned upon by those "in the know." You can explicitly define NULL as 0, though; I think the main reason it''s deprecated is because there is no standard definition for it.

Peace,
ZE.

Share this post


Link to post
Share on other sites
This may be a little nit-picky, but, oh well.

When you write:


Cstrct *myStruct;


Memory is allocated for your POINTER, but it's not until you make the call to new that memory is allocated to your STRUCT.

Your original assumption is right but if you want to get technical, this is even more correct.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 23, 2002 5:50:49 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
However, usage of NULL is normally frowned upon by those "in the know."



I''ve never heard of this before. Actually, I''ve heard it the other way around that you shouldn''t use 0 instead of NULL because NULL may not neccessarily be defined as 0.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites