Initializing a struct

This is a discussion on Initializing a struct within the C++ Programming forums, part of the General Programming Boards category; How is a field function of a struct allocated in terms of a memory it occupies in a memory segment ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    Initializing a struct

    How is a field function of a struct allocated in terms of a memory it occupies in a memory segment that's allocated for a struct object?

    If I have a struct

    Code:
    Struct A
    {
        char* string;
        void Func();
    };
    and if I do

    Code:
    A a;
    memset(&a ,0, sizeof(A));
    a.Func();
    Would that break the program, because the function pointer stored within the memory space for the struct object is totally nuked by the memset cal?

    In other words, does the memory space allocated for a struct look anything like this?

    memory for the structure object a
    ====
    memory to store address string points to
    -------
    memory to store address a function pointer points to the function Func()
    ====

    How is doing
    A a = {};
    different from using memset for initialization?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    As you suggest, the memory that "string" points to is not a part of the struct in any way, shape, or form. It must be acquired separtely from the struct itself, and de-allocated separately from the struct itself.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    No, that would not break your program. In your code, struct A does not contain a function pointer. It just defines a member function that has no implementation. All that code will do is generate a link error (assuming that you haven't implemented Func() somewhere else).
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Sorry about the confusion, I thought that people would assume that the function implementation is available. Ok, then what would happen if the function implementation is available? Where does get a function pointer stored then? Is it going to be stored within the chunk of memory assiged to the struct object?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Where does get a function pointer stored then? Is it going to be stored within the chunk of memory assiged to the struct object?
    There is no function pointer. All you have is a member function. The code for this member function is stored in the code segment of your executable, which is nowhere near the memory allocated for the object itself. You can memset to your heart's content, and it will not change the member function. In your example, all the memset does it set the string pointer to zero (or NULL).
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    besides the fact that you have Struct with a capital, a struct and a class are the same thing in c++ save access controls. Does this make it clearer?

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Yes, it does. Thank you for your comments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. initializing char array inside a struct
    By panos in forum C Programming
    Replies: 6
    Last Post: 06-01-2007, 06:43 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21