structure's memory layout in the function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Narendra

    #1

    structure's memory layout in the function?

    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(size of(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.

    }

  • Alf P. Steinbach

    #2
    Re: structure's memory layout in the function?

    * 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(size of(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?

    Comment

    • Stuart Redmann

      #3
      Re: structure's memory layout in the function?

      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

      Comment

      Working...