|
-
Feb 9th, 2001, 02:37 AM
#1
Thread Starter
PowerPoster
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 
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;
-
Feb 9th, 2001, 09:38 AM
#2
Frenzied Member
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.
Harry.
"From one thing, know ten thousand things."
-
Feb 9th, 2001, 10:02 AM
#3
Thread Starter
PowerPoster
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?
-
Feb 9th, 2001, 10:05 AM
#4
Frenzied Member
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()
Harry.
"From one thing, know ten thousand things."
-
Feb 9th, 2001, 10:10 AM
#5
Thread Starter
PowerPoster
-
Feb 11th, 2001, 01:15 PM
#6
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 11th, 2001, 05:31 PM
#7
Frenzied Member
Really? Cool, I never knew that What's placement new?
Harry.
"From one thing, know ten thousand things."
-
Feb 11th, 2001, 05:34 PM
#8
Monday Morning Lunatic
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).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 11th, 2001, 05:45 PM
#9
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 11th, 2001, 06:53 PM
#10
Frenzied Member
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 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.
Harry.
"From one thing, know ten thousand things."
-
Feb 11th, 2001, 08:14 PM
#11
Thread Starter
PowerPoster
Thanks parksie, still not much experience in C/C++ programming. Although learn it during college life
-
Feb 12th, 2001, 07:13 AM
#12
Monday Morning Lunatic
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. 
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
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 12th, 2001, 09:57 AM
#13
Frenzied Member
Okay, I see (pretty much), thanks a lot for the explanation
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|