...

View Full Version : Declaring arrays in C++ and C#



BrickInTheWall
07-12-2009, 12:20 AM
Hi everyone,

I'm a bit confused...I'm learning C# right now and was just reading through a tutorial about arrays as things have changed a bit (especially since there are no pointers allowed in safe code)...

My book tells me to declare the array like so (c#):


int[] myIntArray;
myIntArray = new int[5];


of course, I'm used to declaring arrays the standard C/C++ way


int myIntArray[5];

but what confuses me is the use of the 'new' keyword in the C# declaration. I'm used to doing that in C++ when I want to allocate memory on the free store for the array like so:



int* myIntArray;
myIntArray = new int[5];

this would usually require me to deallocate the memory after I'm done with it right? (like in the destructor of a class for example) using delete[]...
My book doesn't even mention this...can somebody please clear things up for me a little here? Its also kinda of weird for me not to be using a pointer to declare the array since...in a strict sence...thats what it basically is.

Cheers,
Chris

oracleguy
07-12-2009, 01:35 AM
Calling new is correct in this instance. In C# since it is a managed environment, you do not need to worry about cleaning up your memory, it handles it for you. It has what is called a garbage collector (http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29) that handles it. The reason you have to always new things is because in C# everything is an object. So you have to create instances of them to use them. It might sound a little weird but it can be incredibly powerful.

Does your book talk about value types and reference types and the differences between them?

You can also new on the same line as the variable deceleration like in C++ in case you didn't know that too.

BrickInTheWall
07-12-2009, 01:39 AM
You can also new on the same line as the variable deceleration like in C++ in case you didn't know that too.
my book doesn't say that but I've already figured that that would work :)



Does your book talk about value types and reference types and the differences between them?

It's somewhere in my book I'm sure...the thing's like 1200 pages...and I think I've browsed over it by accident when looking for something...reference typed are stored in heaps right?

Oh and thanks for the info on the garbage thing...thats pretty convenient :)

oracleguy
07-12-2009, 01:44 AM
It's somewhere in my book I'm sure...the thing's like 1200 pages...and I think I've browsed over it by accident when looking for something...reference typed are stored in heaps right?

Make sure when you do get to it you read over it. I would hope something like that would be in the first few chapters. Basically you got the gist, value types are stored on the stack and reference types are stored on the heap.

As you know there are no pointers in C# but reference types are always referred to by their pointers internally. So if you pass a reference type into a function, the equivalent C++ version would be passing it by pointer or by reference.

BrickInTheWall
07-12-2009, 01:49 AM
Make sure when you do get to it you read over it. I would hope something like that would be in the first few chapters. Basically you got the gist, value types are stored on the stack and reference types are stored on the heap.

As you know there are no pointers in C# but reference types are always referred to by their pointers internally. So if you pass a reference type into a function, the equivalent C++ version would be passing it by pointer or by reference.
thats good...I just started learning C# and was a bit bummed about pointers being marked as unsafe (though I can understand it...I've crashed some programs here and there cause I wasen't watching out :P)
I checked and it's somewhere in the OOP chapter.

I have to get used to the fact that a lot of things are objects like you said...
Thanks for the help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum