Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Declaring arrays in C++ and C#

    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#):
    Code:
    int[] myIntArray;
    myIntArray = new int[5];
    of course, I'm used to declaring arrays the standard C/C++ way
    Code:
    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:

    Code:
    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

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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 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.
    OracleGuy

  • #3
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by oracleguy View Post
    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

    Quote Originally Posted by oracleguy View Post
    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

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by BrickInTheWall View Post
    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.
    OracleGuy

  • #5
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by oracleguy View Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •