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;
};
8
  • 3
    "is only one new object" - not true. you can verify this by printing something in the constructor. Commented Oct 30, 2013 at 18:17
  • 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. Commented Oct 30, 2013 at 18:22
  • 2
    Use a std::vector instead! Commented Oct 30, 2013 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. Commented Oct 30, 2013 at 18:26
  • 1
    @WhozCraig: You are right, see related question for Visual Studio. Commented Oct 30, 2013 at 18:30

2 Answers 2

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";
    }
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

Yes, but shouldn't it create m_numParams objects? It only creates one object.
it creates the requested number of objects but returns a pointer to the first of them.
@albizgil What makes you think it only creates one object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.