Thread: dynamic allocation of structs can somebody help me get this thing straight...,

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    dynamic allocation of structs can somebody help me get this thing straight...,

    am making a dynamic struct.., something is wrong here..,

    Code:
    typedef struct tagASTRUCT
    {
        int m_data;
        void Create(int data)
        {
           m_data = data;
        }
    	{
    	}
    }*ASTRUCT
    
    ASTRUCT object;
    
    // now, i'd like to specify the number of structs that i'll be usingo n runtime.., 
    
    object = new tagASTRUCT[10]; // example 10 kinds of this,
    so how do i access the function in it? like this?

    object.Create(...);
    why is not like this?
    object->Create(...);

    thanks,

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    karen

    okey, probably it'll be better if you could give me a sample how to specify the size and at the same time use it on runtime?

    thanks a lot!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This would be a bit easier to follow and more useful if you choose to create a regular instance of the struct.
    Code:
    typedef struct tagASTRUCT
    {
      int m_data;
      void Create(int data)
      {
        m_data = data;
      }
    } ASTRUCT;
    
    ASTRUCT *object;
    >object = new tagASTRUCT[10];
    In an array you determine the element and access the item with the dot operator. You can use the -> operator, but only on the first element.
    Code:
    #include <iostream>
    
    typedef struct tagASTRUCT
    {
      int m_data;
      void Create(int data)
      {
        m_data = data;
      }
    } ASTRUCT;
    
    ASTRUCT *object;
    
    int main ( void )
    { 
      object = new tagASTRUCT[2];
      object->Create(10);
      object[1].Create(20);
      std::cout<< object->m_data <<"\n";
      std::cout<< object[1].m_data <<"\n";
      return 0;
    }
    Try changing those access methods around and see what errors you get.

    >give me a sample how to specify the size and at the same time use it on runtime?
    Use a variable.
    Code:
    cout<<"Enter the number of records: ";
    cin>>size;
    object = new tagASTRUCT[size];
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hiya prelude, thanks for the very fast response whew i've been strugling abou this for an hour already... anyway hope ye don't mind got some more questions...,

    >object = new tagASTRUCT[10];

    err., you could also use ASTRUCT right? or no? plz explain if no..,

    >ASTRUCT *object;

    but um., why is it not possible to put that with the struct just like i did? then declare like this,

    ASTRUCT object; // this is also the same as ASTRUCT * object right?

    and why did you say it will be more useful?
    >In an array you determine the element and access the item with the dot operator. You can use the -> operator, but only on the first element. <

    yeah, this thing got me confused, i kept on wondering how come the arrow operator doesn't want to work(using VC)? but why is it like that? can you explain plz?

    thanks a lot prelude!!!

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hi one more thing, about memcpy,

    is this right? i'd like to copy all the contents of object(10 of this)

    memcpy(buffer, object, sizeof(object));

    where buffer is declared as void *;


    thanks again,

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    I might be way of on this but I will blur it out anyway. My C compiler usally flags 'new' as non C and forces me to use malloc instead. Am I correct if I claim that new is C++ and not C?

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    yeah, when you're in C++, you use the new, and in c, malloc,

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Ops my bad. For some reason I read this post as posted on the C board. sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By schifers in forum C Programming
    Replies: 12
    Last Post: 05-14-2008, 01:49 PM
  2. Dynamic Allocation
    By carrotcake1029 in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 11:14 PM
  3. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 03:21 PM
  4. dynamic allocation class
    By Verdagon in forum C++ Programming
    Replies: 9
    Last Post: 08-14-2005, 12:19 PM
  5. Dynamic Allocation
    By Darkwraith in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-09-2003, 10:00 PM