|
-
February 4th, 2003, 06:42 PM
#1
Dyanmic Array
Can someone give me a good and clear explanation of dyanamic array?
1. its syntax
2. when to use it
3. advantages and disadvantages
Thanks!
-
February 5th, 2003, 03:47 PM
#2
One solution is a vector or a deque. Both are simple and practical.
Kuphryn
-
February 5th, 2003, 07:14 PM
#3
1.
char* pszDynArray = NULL;
unsigned int nSize = 30;
pszDynArray = new char[30];
2. When you are concerned about your memory usage, or don't know the size of the data you will be storing.
3. Come on, do your own homework!
-
February 5th, 2003, 08:51 PM
#4
Originally posted by mwilliamson
1.
char* pszDynArray = NULL;
unsigned int nSize = 30;
pszDynArray = new char[30];
2. When you are concerned about your memory usage, or don't know the size of the data you will be storing.
3. Come on, do your own homework!
mwilliamson is correct, but he neglected to mention a very
integral part of the equation:
Code:
delete [] pszDynArray;
Without that delete, a memory leak would occur.
--Paul
-
February 5th, 2003, 10:00 PM
#5
yeah, it seems I also forgot to use nSize 
char* pszDynArray = NULL;
unsigned int nSize = 30;
pszDynArray = new char[nSize];
-
February 6th, 2003, 12:12 AM
#6
Thanks for the replies guys.
I got confused after reading a particular article and had started to think that dynamic array was something different than what I knew it to be.
And, yes kuphryn, I also use vector in lieu of dynamic arrays quite often and when appropriate.
To the other guys, I actually use a little more efficient method for creating dynamic arrays than what you've posted. See below:
char inFile[] = "";
cin>>inFile;
ifstream in(inFile);
No need for pointers and no need to define any array size.
-
February 6th, 2003, 12:19 AM
#7
What makes your think that your method is *more efficient*?
As a note, the methods above all allocate physical memory. What you are doing is creating a file and using the disk as your *memory*. Don't mean to burst your bubble... but disks are SLOW.
Btw, you have a buffer overflow in your code as well. In other words, you are overwriting something you probably do not intend to.
- Robert
-
February 6th, 2003, 12:41 AM
#8
Originally posted by RobAnd
What makes your think that your method is *more efficient*?
As a note, the methods above all allocate physical memory. What you are doing is creating a file and using the disk as your *memory*. Don't mean to burst your bubble... but disks are SLOW.
Btw, you have a buffer overflow in your code as well. In other words, you are overwriting something you probably do not intend to.
- Robert
1. No my method does not use the disk instead of physical memory in particular. The creation of the file (inFile) serves a different purpose than you think. It has to do with the rest of the program. The code you should focus on is: char inFile[] = "";
The next code could have been something else.
2. Could you please expand on the buffer overflow issue?
You might have something there that I am not aware of, newbie as I am.
Thanks!
-
February 6th, 2003, 12:53 AM
#9
Basically, any time you allocate a constant string (as you have), the compiler moves it to a string table in the process heap and allocates exactly enough space to fit the constant string. In your case 1 byte for the NULL terminating string. If you try to write to this address pointed to by the variable and you write more than 1 byte, you will have essentially overwritten memory you did not intend to. This is called a buffer overflow.
Now... imagine you use cin >> inFile... and the user types some insanely long string. You could potentially overwrite code that at some point will get called. When that code gets calls your application is most likely going to crash because it does not make sense anymore.
Worse yet, someone could enter just the right text to cause your application to execute "meaningful" code that does something malicious.
I guess what I am trying to say is, you are not allocating a dynamic array when using that code in C/C++. You are setting yourself up for problems.
- Robert
-
February 6th, 2003, 01:22 AM
#10
That makes sense in some ways.
But isn't it a way to create dynamic arrays without declaring an array size?
In the following code:
char* pszDynArray = NULL;
unsigned int nSize = 30;
pszDynArray = new char[nSize];
you still are declaring an array size.
What happens when you input more than 30 char?
-
February 6th, 2003, 01:26 AM
#11
There is no way to allocate a completely dynamic array. Arrays in C/C++ cannot grow by themselves. If you want this kind of functionality then use the vector class... or if you are daring... write your own dynamic array class. You will still need to be constantly re-allocating every time the array tries to grow beyond the current size. That is just the nature of C/C++.
- Robert
-
February 6th, 2003, 04:33 AM
#12
char* pszDynArray = NULL;
unsigned int nSize = 30;
pszDynArray = new char[nSize];
Doesn't the above code suffers from the same problem if you input more than 30 char elements?
Further, it is weak as far as being dynamic.
I am currently working a better quick fix to all this till I learn better ways to handle such issues. I will post the code when I am done.
After the quick fix, I will see if I can create my own dynamic class.
-
February 6th, 2003, 07:15 AM
#13
There is no quick fix; the man was right about what he was
saying: when you don't specify a size, the array gets its size from
its initialization parameters. Also, arrays are stack-based and,
thus, cannot grow at runtime.
There's no need to write your own class for arrays; like others
have posted [and as you have acknowledged] there is an existing
class called std::vector that will do everything you need. If you
want different functionality, there's always list ... or deque.
--Paul
-
February 6th, 2003, 08:12 AM
#14
As has been mentioned, for general dynamic arrays - vector.
For dynamc arrays of char - string.
Specifically for you example :
Code:
std::string inFile;
cin >> inFile;
fstream in(inFile.c_str());
-
February 6th, 2003, 09:01 AM
#15
Originally posted by Philip Nicoletti
As has been mentioned, for general dynamic arrays - vector.
For dynamc arrays of char - string.
Specifically for you example :
Code:
std::string inFile;
cin >> inFile;
fstream in(inFile.c_str());
^gold!
Just what I needed (the conversion from string to char*).
Thanks to everyone. After reading your replies I now have a better understanding of the mechanism of arrays. Mainly, as PaulWendt said, arrays are stack-based and,
thus, cannot grow at runtime. In my implementation, I was actually trying to make the array grow at runtime as if it was a vector.
The term dynamic array led me to expect more functionality and implementation than possible.
update: I re-wrote the program using vectors and found that if works much better that way.
Last edited by doumalc++; February 6th, 2003 at 09:53 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|