-
can any wan help me abt this?
Code:
typedef struct {
double X;
double Y;
}Map_UDT1;
UDT1 *pts;
void main()
{
UDT1 *pts = new UDT1 [21];
\\Others coding here
delete [] pts;
}
After I create the pts structure array, i did not get the size as i define when I check it out with sizeof(pts) and I only get the 4 bytes instead of 336 bytes :confused:
Also, how can I create a pts structure array with dynamic size as my program run.
like
Code:
long UserProvideSize;
//Where UserProvideSize is the data from the textbox control.
UDT1 *pts = new UDT1 [UserProvideSize];
\\Others coding here
delete [] pts;
-
The reason you're getting a size of 4 bytes is because sizeof(pts) is the size of the pointer pts. sizeof(*pts) should return the size of the array I think.
You can allocate memory for an array of dynamic size just as you have suggested. If you wish to resize your array, you would most likely have to create a new array of the new size and copy (using memcpy() or ASM for speed) the data from the old array over, if you wanted to preserve it. Then you would delete the old array. All fairly easily done with some pointers and a little thought.
Alternatively you could allocate and deallocate your memory with malloc() and free() (older C-style DMA functions), and use realloc() to reallocate the memory.
-
Thx HarryW, I'll try it out by tomorrow. by the way. is there any problem if I remain using the new and delete function in my dynamic array creation?
-
No the method you use doesn't matter at all, so long as you make sure that you only use delete where you've allocated with new, and only use free() where you've allocated with malloc() :)
-
-
And also take into consideration that in order to properly create objects, you must use new and delete, because they a) allocate memory, and b) initialise the object.
Code:
MyClass *x; // Not initialised
x = new MyClass;
// Implicitly allocates memory and calls
// x.MyClass() -- the constructor
delete MyClass;
// Implicitly calls
// x.~MyClass() -- the destructor
// then frees the memory
If you really need to use malloc and free for an object, you can use placement new, but I wouldn't suggest using it...if you want an example let me know.
-
Really? Cool, I never knew that :) What's placement new?
-
It's basically a version of new where you supply a pointer to a memory location that's already allocated and properly aligned. (And no, I don't totally get the last bit....I think it's not too much of a problem as long as it's on a 4-byte boundary).
-
Here's an example:
Code:
#include <iostream.h>
#include <new.h> // Must #include this to use "placement new"
#include <stdlib.h>
class Fred {
public:
void SayHello() {
cout << "Hello from Fred at " << this << endl;
}
};
void main() {
Fred *f = (Fred*)malloc(sizeof(Fred));
if(!f || (long)f%4) {
// Either NULL or not aligned :-(
cout << "Invalid pointer" << endl;
return;
} else {
cout << "Allocated memory at " << f << endl;
}
f = new(f) Fred(); // Placement new
f->SayHello();
f->~Fred(); // Explicitly destroy object;
free(f);
}
IMO, it's not a good idea to use it unless you want to wrap a class around some memory-mapped object.
-
Oh right... So you basically pass a pointer to the memory that's already allocated to new()? I've never seen anything like that before. Well not that surprising since I don't look at a whole lot of C++ code that isn't my own :rolleyes: I notice you also do something with the constructor... does that call it?
What's the point in all this anyway if you have to call the constructor and destructor yourself?
Oh yes, I think your example is missing something - if the memory is not aligned properly you should free() it before returning.
-
Thanks parksie, still not much experience in C/C++ programming. Although learn it during college life :)
-
Harry -- AFAIK there's only one situation where you need this, and that's if you have a memory-mapped hardware device like a timer. You can map a value onto that location. :confused:
You really shouldn't use it, though...I don't even know why it's there. :(
And yes....I probably should have free()d the memory if it wasn't aligned...however the MSDN docs said that it would always be properly aligned, so it won't die even if it's bad code :(
-
Okay, I see (pretty much), thanks a lot for the explanation :)