Results 1 to 13 of 13

Thread: create a dynamic structure array

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    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;

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  3. #3

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    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?

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  5. #5

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Thx HarryW.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Really? Cool, I never knew that What's placement new?
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  11. #11

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Thanks parksie, still not much experience in C/C++ programming. Although learn it during college life

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  13. #13
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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
  •  



Click Here to Expand Forum to Full Width