I'm writing a program, where I have one class, inside that class ( or outside, hope it doesn't matter ) I have struct. In that class I need to make an array of struct elements ( I know I can use vector e.g. but in program it is only permitted to use simple dynamic arrays ).
I declare an array as T * arr[SIZE]
, where T
is my struct. The only problem is I don't know exact size of array and need to increase it size if necessary. So I wrote function to resize it:
if( some cond ){
T * tmpArr[newSIZE];
memcpy( tmp, db, newSIZE*sizeof(T));
delete [] arr;
arr = tmpArr;
}
But I'm getting a error that MyClass::T[....]
is incompatible with MyClass::T*[SIZE]
which responds to my arr = tmpArr
expression I guess.
Can you tell me what I'm doing wrong?
And how is better to declare T * arr[size]
or T * arr = new T[size]
and how do I resize ( and free memory from old one ) array in that case?
UPDATE:
Thanks for answers, I did accordingly in my program:
T * tmp = new T[newSIZE];
memcpy( tmp, db, newSIZE*sizeof(T) );
delete [] db;
db = tmp;
And now I'm getting weird things, after deleting db and assigning db
to tmp
I try to print all data contained in db ( or tmp ), that weird thing I get:
Smith2 Michigan ave▒ ACME, Ltd. One ACME roa▒
Smit▒ Michigan ave` ACME, Ltd. One ACME roa "
One ACME road#@"▒▒▒▒Michigan ave`▒▒▒▒Smit▒"
▒▒ ▒8 Ltd.#Michigan avenuemith4▒aF▒
If I print same data before deleting and assigning, I'll get normal text I want. And after in program ( as I didn't have before and maybe that's problem with my code, I get Segmentation fault ). By the way, I'm using struct
of std::string
and cygwin in my windows. Do you have any idea what's the problem here?