Saturday, July 6, 2013

baffled by 'new' operator with array type.

Newsgroup: comp.lang.c++

Subject: baffled by 'new' operator with array type.

From: Ray Dillinger <bear@...>

Date: Sat, 06 Jul 2013 02:08:25 -0700





I have the following type declarations, which I thought were quite simple;



const uint16_t SUBTREES = 64;

typedef struct stringnode *strpt; // a strpt is a pointer to a struct stringnode.

typedef strpt branchtype [SUBTREES]; // a branchtype is an array of SUBTREES strpt.





and the following function, which also seems simple. (I have added the line numbers

at the front so you can see which lines the error messages refer to).



132: // allocate and return a subtree, copied from argument within the (modular) range specified

133: branchtype *branchcopy(const branchtype * const subtrees, const int start, const int end){

134: branchtype *newbranch = new(branchtype);

135: int count;

136: for (count = start; count != end; count = (count + 1) % SUBTREES)

137: newbranch[count] = subtrees[count];

138: return(newbranch);

139: }



This function is supposed to allocate a new branchtype (via a pointer to it which is

named newbranch) and then copy a subrange of an existing branchtype (whose address

it gets via its argument subtrees) to it. The subrange copied is intended to be

'modular' in that if 'end' is less than 'start' it copies a subrange from 'start'

to the end of the array, then continues from the beginning of the array to 'end'.



When compiling this function, I get the following errors and I don't understand

what I've done wrong.



string3.cpp:133:41: error: cannot convert ?stringnode**? to ?stringnode* (*)[64]? in initialization

string3.cpp:136:38: error: invalid array assignment





I think that the first error message is baffling because it seems to imply that

new(branchtype) is returning something other than a pointer to branchtype. (ie,

in this case a pointer to an array of pointers at struct stringnodes). I can't

figure out how that could happen after reading the documentation of 'new'. I

could, but wouldn't like, to simply drop 'new' and use 'calloc' and static cast

instead.



The second error message is baffling because subtrees is a (const) pointer to (const)

branchtype and newbranch is very explicitly declared to be a pointer to branchtype,

so they are clearly the same type, and I'm trying to modify the contents of the non-

const one, not the const one. The error message is documented to mean I have

attempted to assign to an array (in violation of standard, I know), but AFAIK

there isn't anything wrong with assigning to a single element of an array. If

I understand my declarations correctly, the elements of branchtype are pointers,

not arrays. Why does it think I'm making an assignment to an array here instead

of an assignment to a pointer (which happens to be one element of an array)?



Was there something wrong with my declaration? Do these declarations not mean what

I think they mean?



Thank you for any help. I know that my style here is very C'ish and not idiomatic

C++. This code is intended to be a library that will eventually get rolled into

a 'ropes' object type (representing Unicode strings as broad, shallow trees) in C++,

but right now I'm still trying to get some fundamentals working correctly with

an absolute minimum overhead (and backporting to make a plain C library is also a

goal).



Bear









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?36767-baffled-by-new-operator-with-array-type&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment