Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2008 by Ivor Horton; ISBN: 9780470225905
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 29th, 2008, 12:31 AM
Registered User
 
Join Date: Nov 2008
Location: montreal, quebec, Canada.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question about value class type

Hello,

I am reading the book and I am on page 208. One of the code is

int^ value = 99;

and it says that value of type int^ and the value it points to on the heap is initialized to 99.

But isn't 99 is of a fundamental type and thus of value class type? if so, by default shouldn't it be created in stack (Tracking Handle, page 207)? not using gcnew here, right?

The same thing was mentioned later in the page where the code is

int^ result = 0;

and it says "you first create the handle result that points to a value 0 on the heap".

So is all integer literals in handle initialization gets created in heap?

If so in page 209 (next page), first para last line, it says "the expression on the right of the assignment is a value class type and its address is stored in result". So by default it should be created in stack no? (Tracking Handle, page 207)

I apologize if this a stupid question. But please let me know...

Sa'ad

Reply With Quote
  #2 (permalink)  
Old November 29th, 2008, 12:42 AM
Registered User
 
Join Date: Nov 2008
Location: montreal, quebec, Canada.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok I did web search and I am now referring to page 381

double pi = 3.142;

is created in stack

But

double^ two = 2.0;

is created in heap. Now in the above para, it says "when you declare a data item of a value class type, memory for it will be allocated on the stack but you can create value class objects on the heap using the gcnew operator..."

Now in

double^ two = 2.0;

gcnew is not used. But it is still created in heap. Is it because,

double^ two = 2.0;

is really equivalent to

double^ two = gcnew double(2.0);

If so, now it kind of make sense.

Help!

Reply With Quote
  #3 (permalink)  
Old November 29th, 2008, 05:18 AM
Authorized User
 
Join Date: Nov 2008
Location: , , .
Posts: 15
Thanks: 1
Thanked 1 Time in 1 Post
Default

Saadli,

You can take my words with a grain of salt because I haven't been following the CLR section of the book, I've mostly been on the Native part.

However page 207, under the Tracking Handles topic, it says that:
  • ...variables allocated on the CLR heap, which includes all CLR reference types, cannot be declared at global scope.
When I try to define a global like this:

Code:
int^ value = 99;
I get this compiler error:

error C3145: 'value' : global or static variable may not have managed type 'System::Int32 ^' 1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap

I can only imagine that the value variable is indeed a heap value, as a definition like this works fine at global scope:

Code:
int value = 99; // Definitely on the stack
That being the case, I cannot tell you what the real difference is between these definitions:

Code:
int^ value = 99;
int^ value = gcnew int(99);
The compiler gives the same error (C3145) for each of these global definition.

Here's a link about handles that you can paste into the URL field of the VS2008 help: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vclang/html/70c411e6-be57-4468-a944-6ea7be89f392.htm

Hope this helps...
Reply With Quote
  #4 (permalink)  
Old December 3rd, 2008, 01:34 PM
Registered User
 
Join Date: Nov 2008
Location: montreal, quebec, Canada.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Jabney,

Thanks for the reply.

I think an easier way for me to understand this would be to see if there is a way to print out the address kept on a handle. That way I can print out the address and guess where the heap is starting from.

Do you know how to print the address in a handle? Please let me know.

thanks
Sa'ad


Reply With Quote
  #5 (permalink)  
Old December 3rd, 2008, 10:15 PM
Authorized User
 
Join Date: Nov 2008
Location: , , .
Posts: 15
Thanks: 1
Thanked 1 Time in 1 Post
Default

Hi saadli,

I don't know how to print a handle address to the console window, but it's possible to use the debugger by setting a breakpoint on the line after the assignment statement and then entering debugging mode. The handle addresses appear in the Locals tab.

I tried this myself. The two addresses are very similar:
Code:
    int^ val  = 99;
    int^ val2 = gcnew int(100);

These variables have the following addresses:
Code:
     val    0x01dc0f90    System::ValueType^
    val2    0x01dc0f9c    System::ValueType^

These addresses are only 12 bytes apart, so I think it's safe to say that both exist on the heap.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nullable Type Question Aaron Edwards ASP.NET 2.0 Professional 1 August 30th, 2007 08:37 AM
Class Cannot be Pulic in This Type of Prj BrianWren Pro VB 6 3 May 1st, 2007 05:07 PM
Research Type Question nadee Oracle 0 March 19th, 2007 12:12 AM
question about sub New() in class hertendreef ASP.NET 2.0 Basics 7 January 3rd, 2007 02:10 PM
Data Type Question dparsons ASP.NET 1.0 and 1.1 Professional 0 March 23rd, 2006 06:07 PM



All times are GMT -4. The time now is 11:47 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.