So recently in the forums I have been giving out info on Dynamic Allocation (whether i am asked or not)
This little tidbit of C++, Java, C#, etc is one of my favorite things in the world, and it's beginning to pop up everywhere.
Especially now that I am starting to get into C# .NET programming.
But hey, I love it!
For all of you who don't "get it", let me explain.
The most useful thing for dynamic allocation is declaring multidimentional arrays.
For example, say I want a 2-D array, but I don't know how big it needs to be.
I could just declare it normally:
Thats good and all, but what's the point of having all of them extra boxes?
What if I could size the array on the fly?
Dynamic Allocation saves the day!
Say I want a 2-D array, but I only need an 8 by 8 box.
Declaring an array that's 20x20 would technically work, but thats 20^2 - 8^2 = 336 boxes unused!
Talk about a bigger waste of space than Rosie O'donald!
oh Yes. I went there.
Let us instead, declare what I like to call a DOUBLE POINTER!!! (remember pointers, kids?)
This is not a technical term, just something I've been throwin around the dorm room.
(Which is why I am the only one who knows it, as I have a single)
so we can get a 2-D array like this:
Not so bad eh?
Also, notice my capitalization in all the variables. C# will do that to you
Hope you enjoyed this, and MAYBE learned something?
Peace out
~Bodom
This little tidbit of C++, Java, C#, etc is one of my favorite things in the world, and it's beginning to pop up everywhere.
Especially now that I am starting to get into C# .NET programming.
But hey, I love it!
For all of you who don't "get it", let me explain.
The most useful thing for dynamic allocation is declaring multidimentional arrays.
For example, say I want a 2-D array, but I don't know how big it needs to be.
I could just declare it normally:
int Array[20][20];
Thats good and all, but what's the point of having all of them extra boxes?
What if I could size the array on the fly?
Dynamic Allocation saves the day!
Say I want a 2-D array, but I only need an 8 by 8 box.
Declaring an array that's 20x20 would technically work, but thats 20^2 - 8^2 = 336 boxes unused!
Talk about a bigger waste of space than Rosie O'donald!
oh Yes. I went there.
Let us instead, declare what I like to call a DOUBLE POINTER!!! (remember pointers, kids?)
This is not a technical term, just something I've been throwin around the dorm room.
(Which is why I am the only one who knows it, as I have a single)
so we can get a 2-D array like this:
int MaxRow; //Number of Rows int MaxCol; // Number of Columns int **Array; //DOUBLE POINTER!!!! MaxRow = 8; // Of course, this can be read // in from a file or from cin... MaxCol = 8; // Same with this. :P Array = int*[MaxRow] //Notice the * between // int and [, we want this // to be an array of POINTERS!!!! //WE can then use a for loop to allocate the other columns //One thing i didn't mention was that all these pointers in our //array can be allocated to different sizes. //Thus, we have a very DYNAMIC way to read in and store data! for(int i = 0; i < MaxRow; i++) Array = new int[MaxCol] //No * here! //we can then fill it with numbers, and access it as if it were //a normal 2-D array.
Not so bad eh?
Also, notice my capitalization in all the variables. C# will do that to you
Hope you enjoyed this, and MAYBE learned something?
Peace out
~Bodom
0 Comments On This Entry
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
- Adminsitration
- Apple
- assembly
- atmel
- AVR
- avr-libc
- Bada
- beginner
- Blackberry
- boot
- C
- C#, .NET, and XNA
- C++
- ccleaner
- code
- defragmentation
- Desktop
- dynamic
- embedded
- FooBada
- foobar
- foobar2000
- Gnome
- high
- interrupts
- Java
- level
- Linux
- Math
- Matricies
- Matrix
- microcontroller
- nasm
- New
- Open Source
- Operating System
- Oracle
- oscilloscope
- Programming
- Python
- Random Computer Stuff
- Random Rants
- Repair
- Samsung
- sector
- serial
- speed
- Square
- static
- timer
- UART
- Ubuntu
- unix
- USART
- virtualbox
- Visual
- VOIP
- Windows
- Windows 7
- x86
My Blog Links
Recent Entries
-
Playing in Pythonon Apr 24 2011 10:06 PM
-
-
Using Virtualbox as a bootloader testing environmenton Nov 13 2010 11:00 PM
-
AVR Oscilloscope, Part 1on Sep 13 2010 07:21 PM
-
My thoughts on Bada as an Operating System and as a Development Platformon Sep 05 2010 10:43 AM
Recent Comments
Search My Blog
16 user(s) viewing
16 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment









|