0

Is there anything in particular to look out for with dynamic arrays of custom types?

I am trying to create a dynamic array of ConditionParameter (definition below)

ConditionParameter* m_params;
...
m_params = new ConditionParameter[m_numParams];

But the result of the above line is only one new object of type ConditionParameter, the address of which is stored in m_params.

struct ConditionParameter
{
    ConditionParameter() :
    type(OBJ_TYPE_OBJECT),
    semantic(OP_SEMANTIC_TYPE_NONE),
    instance(NULL),
    attrib(ATTRIB_TYPE_NONE),
    value(0)
    {}

    ConditionParameter(const ConditionParameter& other)
    {
        attrib = other.attrib;
        instance = other.instance;
        semantic = other.semantic;
        type = other.type;
        value = other.value;
    }

    ConditionParameter& operator = (ConditionParameter& other)
    {
        attrib = other.attrib;
        instance = other.instance;
        semantic = other.semantic;
        type = other.type;
        value = other.value;
        return *this;
    }

    ObjectType          type;   

    OperandSemanticType semantic;
    Object*             instance;
    AttributeType       attrib;
    int                 value;
};
albizgil
  • 103
  • 6
  • 3
    "is only one new object" - not true. you can verify this by printing something in the constructor. – Karoly Horvath Oct 30 '13 at 18:17
  • m_params now points to the first ConditionParameter created. You can access other elements using m_params[index]. m_params == &(m_params[0]). – François Moisan Oct 30 '13 at 18:20
  • Could it be a problem with the Visual Studio debugger? I've been stepping in the program, and it only shows one object in the array after that line. – albizgil Oct 30 '13 at 18:20
  • 1
    @albizgil, The debugger does not know how m_params was created. He only knows it is a ConditionParamter* and will only interpret it as such. – François Moisan Oct 30 '13 at 18:22
  • 2
    Use a `std::vector` instead! – πάντα ῥεῖ Oct 30 '13 at 18:25
  • 2
    I can't rightly recall for sure, but I believe you can specify the array count to expand upon of a block-allocated sequence like this by using the `variable,len` syntax in the watch/var window. I.e. enter `m_params, N` where `N` is the number of elements you allocated. I'm probably wrong on the syntax, but I know the feature is there. – WhozCraig Oct 30 '13 at 18:26
  • 1
    @WhozCraig: You are right, see [related question](http://stackoverflow.com/questions/972511/view-array-in-visual-studio-debugger) for Visual Studio. – François Moisan Oct 30 '13 at 18:30
  • @FrançoisMoisan now what would be genuinely kick-arse is if `m_params,m_numParams` worked (in the OP's case). Making that second option variable on the running context would be h-a-n-d-y. I don't have a dev-env in front of me to try it, unfortunately, but it would be sweet if that works as well. – WhozCraig Oct 30 '13 at 18:52

2 Answers2

6

But the result of the above line is only one new object of type ConditionParameter, the address of which is stored in m_params.

No, the result is m_numParams instantiations of ConditionParameter -- the pointer to the first of which is returned by new.

new[] creates a contigious array of ConditionParameter objects. The first one is located at m_params. You can get to subsequent instantiations using the [] operator, as with:

ConditionParameter* secondParam = &m_params[1];

You can prove this to yourself with a little "sprintf debugging":

ConditionParameter() :
    type(OBJ_TYPE_OBJECT),
    semantic(OP_SEMANTIC_TYPE_NONE),
    instance(NULL),
    attrib(ATTRIB_TYPE_NONE),
    value(0)
    {
      cout << "Constructor\n";
    }
John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

Of course it is new returns a pointer to the first element of the allocated block of memory. That is why you assign the result of new to a variable of type ConditionParameter* which is in fact a pointer to ConditionParameter. However this does not mean that a single object has been allocated. Unless new returns null it will allocated as many objects as you told it to.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176