I am currently taking Visual Basic class at my school. It is mostly self taught since the teacher has a pre-algebra class that same hour (please note that I go to a [I]very [/I] small school and the pre-algebra class has a grand total of a whopping two students). After this year of Visual, I want to start learning C# or C++, since my interest is in game programming. I know arrays are an important part of programming and I just got to that chapter in the book; however, I find the book does a poor job explaining them, and I was hoping Facepunch could help me understand and work with arrays.
Imagine an array as a multidimensional variable. Like,
string lol (10);
lol [0] = "rofl";
lol [1] = "noob";
lol [2] = "get what i'm sayin?";
Mind if that isnt 100% correct I haven't used arrays in a while. (Visual Studio would surely point out an error and you can use the context menu on the error for assistance)
[editline]08:12PM[/editline]
Forget the ; that isn't needed in VB.
Arrays, most simply put, are collections of objects. Whether they be integers, strings, or otherwise. A string itself is an array of chars (at least in C++, I'm not sure how VB.Net handles it internally).
In [b]unmanaged[/b] C++, an array (Object* varName = new Object[size]) is a collection of memory pointers to objects stored in memory by an index. You can then access the object via the pointer stored in the array. Upon creation of the array, a space is allocated in memory for that array. This is why you have to actually specify how large the array is and why you cannot add more objects than the limit without recreating the array/allocation of memory. In managed/.NET code, the Collections (List, SortedList, etc) objects piggyback off of the default array in the runtime but make it easier for the end user by handling allocation and whatnot.
-snip-
[QUOTE=JWJ;25120362]A string itself is an array of chars (at least in C++, I'm not sure how VB.Net handles it internally).[/QUOTE]
It's internally a char array, but it's abstracted to just be a string. It's not an array in VB.
[QUOTE=esalaka;25127364]It's internally a char array, but it's abstracted to just be a string. It's not an array in VB.[/QUOTE]
However it does implement IEnumerable<char>
[QUOTE=JWJ;25120362]In [b]unmanaged[/b] C++, an array (Object* varName = new Object[size]) is a collection of memory pointers to objects stored in memory by an index. You can then access the object via the pointer stored in the array.[/QUOTE]
That's an incredibly misleading statement. In C++, an array is just like in C; a contiguous block of memory that stores a collection of variables of the same type. Now, in your example, you're using a specific type of array: an array of pointers to heap-allocated objects. The only reason that that array stores [i]pointers[/i] instead of actual objects is because you've [i]declared[/i] it as an array of pointers, although you need to do that because you're allocating new objects on the heap using the new operator, which returns a pointer. You could easily make an array of stack-allocated objects that actually stores each object contiguously and accesses them directly, but pointers allow you more freedom.
[QUOTE=shill le 2nd;25141385]That's an incredibly misleading statement. In C++, an array is just like in C; a contiguous block of memory that stores a collection of variables of the same type. Now, in your example, you're using a specific type of array: an array of pointers to heap-allocated objects. The only reason that that array stores [i]pointers[/i] instead of actual objects is because you've [i]declared[/i] it as an array of pointers, although you need to do that because you're allocating new objects on the heap using the new operator, which returns a pointer. You could easily make an array of stack-allocated objects that actually stores each object contiguously and accesses them directly, but pointers allow you more freedom.[/QUOTE]
Oh, well then. Thanks for the info! :v:
[QUOTE=JWJ;25141482]Oh, well then. Thanks for the info! :v:[/QUOTE]
you're welcome, thanks for brohoster
[QUOTE=Encryption;25119284]Imagine an array as a multidimensional variable. Like,
string lol (10);
lol [0] = "rofl";
lol [1] = "noob";
lol [2] = "get what i'm sayin?";
Mind if that isnt 100% correct I haven't used arrays in a while. (Visual Studio would surely point out an error and you can use the context menu on the error for assistance)
[editline]08:12PM[/editline]
Forget the ; that isn't needed in VB.[/QUOTE]
Thanks for the info, does anyone know of any good books or websites to start learning c++ or c# from?
Sorry, you need to Log In to post a reply to this thread.