structure's memory layout in the function?

Discussion in 'C++' started by Narendra, Oct 19, 2006.

  1. Narendra

    Narendra Guest

    void function1(void *);
    void main()
    {
    int size_offset = 0;
    typedef struct
    {
    int a;
    int b;
    int c;
    char ch;
    }A;

    A *pst = NULL;
    pst = (A*)malloc(sizeof(A)*1);

    function1((void*)pst);
    }
    void function1(void *pStr)
    {
    // As i need to intialize the structure members. i want to access it's
    me
    mbers.
    // How can i know the structure's
    // memory layout in this function?

    //Conditions:
    //1. i do not want to declare the structure as global or static.
    //2. I will pass the structure pointer as void pointer.

    }
     
    Narendra, Oct 19, 2006
    #1
    1. Advertising

  2. * Narendra:
    > void function1(void *);


    Avoid void.


    > void main()


    Invalid in C and C++, and has never been valid.


    > {
    > int size_offset = 0;
    > typedef struct
    > {
    > int a;
    > int b;
    > int c;
    > char ch;
    > }A;


    No need to use a typedef in C++.

    Also, reserve all uppercase names for macros.


    >
    > A *pst = NULL;
    > pst = (A*)malloc(sizeof(A)*1);


    Use 'new', not 'malloc'.

    Don't use casts.


    > function1((void*)pst);


    Don't use casts.


    > }
    > void function1(void *pStr)
    > {
    > // As i need to intialize the structure members. i want to access it's
    > me
    > mbers.


    Use a constructor to initialize memebers.


    > // How can i know the structure's
    > // memory layout in this function?
    >
    > //Conditions:
    > //1. i do not want to declare the structure as global or static.
    > //2. I will pass the structure pointer as void pointer.


    Since you didn't even get 'main' right, it's doubtful that there are any
    good or even meaningful reasons for these conditions.

    >
    > }




    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 19, 2006
    #2
    1. Advertising

  3. Narendra wrote:

    > void function1(void *);
    > typedef struct
    > {
    > int a;
    > int b;
    > int c;
    > char ch;
    > }A;
    > // As i need to intialize the structure members. i want to access it's
    > // members.
    > // How can i know the structure's
    > // memory layout in this function?
    >
    > //Conditions:
    > //1. i do not want to declare the structure as global or static.
    > //2. I will pass the structure pointer as void pointer.
    >


    You can't. Since you only pass a void* pointer to your function, you
    cannot extract any information about the type that is pointed to. Note
    that unlike (for example) Java no run-time information about the types
    are available in C++. If the only task you have in mind is
    initialization, you can simply add a constructor to your struct:

    struct A
    {
    int a;
    int b;
    int c;
    char ch;
    A ()
    : a (0), b (0), c (0), ch (0)
    {}
    };

    So whenever we declare an variable of type A, its members will be
    initialized automatically.

    Regards,
    Stuart
     
    Stuart Redmann, Oct 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Spiewak
    Replies:
    3
    Views:
    3,177
    Rick Spiewak
    Aug 26, 2003
  2. RobertH
    Replies:
    1
    Views:
    738
    Steve C. Orr [MVP, MCSD]
    Nov 4, 2003
  3. NWx
    Replies:
    4
    Views:
    2,979
    Kevin Spencer
    Feb 19, 2004
  4. Replies:
    1
    Views:
    603
    John Timney \(MVP\)
    Jun 19, 2006
  5. Replies:
    2
    Views:
    619
Loading...

Share This Page