c++ dynamic array
Hi All,
I'm trying to create a dynamic array in c++. Basically the size of the array is based on the number of lines in an input file. The code looks like this. Code:
Code:
cannot allocate an array of constant size 0 Thanks in advance |
The way you are allocating memory is not dynamic, the compiler has to know the value of 'lines' before execution.
Check this out: http://www.codeproject.com/KB/cpp/arrayDinamic.aspx Hope it helps. +Kunsheng |
Quote:
You can use STL std::vector class, or allocate array using new[] or use malloc. I suggest std::vector or new[]. |
Quote:
Question, does Code:
Int **array Thanks |
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
In this case, reading from the "inside" outward, you see: array is a pointer. the thing it points to is a pointer. the thing that points to is an int. |
Quote:
Code:
int size = rand() & 0xFF; Quote:
|
Quote:
Code:
#include <iostream> |
Yes this is a g++ extension adding pedantic to the compile flags will cause it to fail.
|
Quote:
Code:
#include <iostream> Kevin Barry |
Quote:
Kevin Barry |
Quote:
I don't think anyone here suggested it was valid C++. I said I thought is was NOT valid C++. (privately I was sure it was not valid C++, but I prefer not to express certainty about the C++ standard unless I'm actually looking at the relevant section of the standard when I make the statement.) I think the original question was why it didn't work for the OP (we still don't know) and the further question was whether it works in g++ (it does). Quote:
|
Usually, the way that I like to handle such requirements is with some convenient kind of "container class." In other words, a hash or a list or something that I can easily refer-to using "(say...) a number" as an index.
Although it is "natural" to envision such a thing as being "a 'square' block of storage whose elements can be accessed by multiplication from a base-address," such a perspective is quite limited ... especially if the array is known to be sparse. Perhaps counter-intuitively, such an arrangement can actually be more efficient than the alternative, especially if the array is large, because "memory" is virtual. Data-structures that have a large "footprint" are therefore penalized... sometimes, quite considerably. |
Quote:
|
Quote:
Quote:
|
Quote:
That isn't necessarily a contradiction to your claim that support for this non standard feature is a flaw in g++. But, on balance, I disagree with that at well. Why should the language make things harder for the programmer when it is easy to make things easier for the programmer? This extension is certainly not a confusing syntax. It is not an error prone feature to use (anyway less so than using new or std::vector or any of the other ways of getting the same functionality). Its biggest flaw is that it hides a potential cost/limit (the risk of stack overflow). But C++ generally hides costs and limits (that is one of the flaws of the language). Like C, C++ gives you the level of control needed to write very tight code. But unlike C, C++ tends to also let you write concise, fairly simple looking code that costs much more (time/space) to run than you might guess by looking at it. This form of allocation for a run time determined amount of data is usually more efficient than using new or std::vector or any other method approved by the C++ standard. It is simpler to code and easier to understand when reading the code. Unfortunately, both Windows and Linux lay out the x86 32 bit address space in a stupid manner creating an unnecessary constraint on stack size, which makes this g++ extension a little less useful on those (and other common) architectures. But it is still usually useful and a C++ programmer is supposed to know when a feature shouldn't be used because of costs or limits. |
All times are GMT -5. The time now is 03:37 PM. |