First of all: try to use variable names with meaningful names.
What the hell does "hibnd1" stand for?
The Wild Wild West - Desperado!
Assignment Question - Please help!
You''re going to have to create a new array with the proper dimensions, and copy the existing variables over into their respective locations in the new matrix. It shouldnt be that bad.
Eh?
I''m afraid I have no clue what that code snippet is supposed to do, not to mention what''s wrong with it. Maybe you should explain exactly what the code is doing, because it seems very confusing.
I''m afraid I have no clue what that code snippet is supposed to do, not to mention what''s wrong with it. Maybe you should explain exactly what the code is doing, because it seems very confusing.
If you have an existing array of length n and wish to "extend" it to length N (where N > n), proceed as follows:
Extending this to multiple dimension requires care, but nothing else. Just be sure to organize your data in such a way that as few reallocations as possible are required. For example, if you have an NxM matrix (where N is the number of rows) and it''s organized internally as an array of N arrays, each of M elements, then increasing the number of columns means reallocating each of the N rows, while increasing the number of rows is a much simpler operation.
Frankly, though, I wish more institutions taught the use of the STL. There are remarkably few situations where you actually need to dynamically allocate arrays by yourself, and that knowledge can therefore be acquired when the student has a better grasp of general process.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
// 1. allocate new array of length Ntype * new_array = new type[ N ];.// 2. copy over values from existing arraymemcpy( new_array, old_array, n * sizoef( type ) );.// 3. zero out remaining elements in new arraymemset( new_array + n * sizeof( type ), 0, (N - n) * sizeof( type ) );.// 4. release old arraydelete [] old_array;.// 5. redirect old array to point at new arrayold_array = new_array;
Extending this to multiple dimension requires care, but nothing else. Just be sure to organize your data in such a way that as few reallocations as possible are required. For example, if you have an NxM matrix (where N is the number of rows) and it''s organized internally as an array of N arrays, each of M elements, then increasing the number of columns means reallocating each of the N rows, while increasing the number of rows is a much simpler operation.
Frankly, though, I wish more institutions taught the use of the STL. There are remarkably few situations where you actually need to dynamically allocate arrays by yourself, and that knowledge can therefore be acquired when the student has a better grasp of general process.
std::vector< std::vector< type > > matrix;matrix.resize( N );for( int n = 0; n < N; ++n ) matrix[ n ].resize( M );// presto! NxM matrix.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
Having to copy all the values over from the old array seems strange though, I attempted another question similar (1D array)to this and found that I could adjust the hibnd value and it worked.
These files (from a different question) show this:
http://binary.gamer.net.nz/stuff/asn1-1/tstArray.cpp
http://binary.gamer.net.nz/stuff/asn1-1/ARRAY.H
If you remove my code in the function grow() you'll see out of bounds errors, etc.
From what I can tell, hibnd1 and hibnd2 are simply the limits for the columns and rows, they dont actually do anything except allow the array to go to these values.
There are functions which check these limits when you copy new values into the array (contained in matrix.h). Gah, im still confused :p Cheers for your help though
[edited by - Aragorn992 on April 15, 2002 8:32:38 PM]
These files (from a different question) show this:
http://binary.gamer.net.nz/stuff/asn1-1/tstArray.cpp
http://binary.gamer.net.nz/stuff/asn1-1/ARRAY.H
If you remove my code in the function grow() you'll see out of bounds errors, etc.
From what I can tell, hibnd1 and hibnd2 are simply the limits for the columns and rows, they dont actually do anything except allow the array to go to these values.
There are functions which check these limits when you copy new values into the array (contained in matrix.h). Gah, im still confused :p Cheers for your help though
[edited by - Aragorn992 on April 15, 2002 8:32:38 PM]
I don''t download random source files. I don''t read through random source listings. If you want my help with source, you msut be able to describe what it should do, what it currently does and provide a concise listing (generally < 20 lines).
You need to copy over the values because the new block of memory has nothing to do with the old block of memory; they are completely separate. If you don''t copy the values and release the old block, you lose those values forever and have junk in your new array. If your previous attempt worked, you were either doing something very dangerous (my suspicion, seeing as you don''t know how to reallocate an array anyways) or you were very lucky. I don''t like lucky. Lucky results in hard-to-track-down bugs.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
You need to copy over the values because the new block of memory has nothing to do with the old block of memory; they are completely separate. If you don''t copy the values and release the old block, you lose those values forever and have junk in your new array. If your previous attempt worked, you were either doing something very dangerous (my suspicion, seeing as you don''t know how to reallocate an array anyways) or you were very lucky. I don''t like lucky. Lucky results in hard-to-track-down bugs.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
How in the world is that a strange idea?
Once you have created an array of size n, it extends to a certain point in memory. There are -no- promises made about what will be in the piece of memory just beyond your array, and you are not given control over it. If you just start writing outside of your array, you are liable to
1. Overwrite something your program or the OS needs
or
2. Get your data overwritten because the program can still use that memory for whatever it wants.
Trust in the idea that this is the way it is done.
Once you have created an array of size n, it extends to a certain point in memory. There are -no- promises made about what will be in the piece of memory just beyond your array, and you are not given control over it. If you just start writing outside of your array, you are liable to
1. Overwrite something your program or the OS needs
or
2. Get your data overwritten because the program can still use that memory for whatever it wants.
Trust in the idea that this is the way it is done.
Thankyou Oluseyi your code helped me out alot. As it turned out I had accidently skipped a unit of the study guide which made things seem very foreign.
Also is it necessary to do step 3 of your code? What exactly does it do?
Also is it necessary to do step 3 of your code? What exactly does it do?
quote: Original post by Aragorn992
Also is it necessary to do step 3 of your code? What exactly does it do?
Necessary? No. Good practice? Yes. When you allocate memory, you get whatever was in there prior to allocation - essentially garbage. Zeroing out the memory is just a good idea.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement