Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Stupid struct question

Options
  • 27-06-2006 7:11pm
    #1
    Registered Users Posts: 304 ✭✭


    Hey,

    Just a very quick question. I'm doing some stuff in C, and I'm currently muddling my way through this (i.e. learning as I go, and gleaning of as many tutorials as I can find).

    The thing is, the tutorials aren't answering one pretty big question for me. How do I drop a structure once I'm finished with it? Do I just call free(whatever) ?

    Also, if I'm doing this in C++, and still using structs for some odd reason (like I'm working on a project with mixed code, and I'm using a function that returns a struct) could I also use delete, or would it be best to stick with free or some other solution?

    Thanks,
    Aoife


Comments

  • Closed Accounts Posts: 80 ✭✭Torak


    Hey,

    Just a very quick question. I'm doing some stuff in C, and I'm currently muddling my way through this (i.e. learning as I go, and gleaning of as many tutorials as I can find).

    The thing is, the tutorials aren't answering one pretty big question for me. How do I drop a structure once I'm finished with it? Do I just call free(whatever) ?

    Also, if I'm doing this in C++, and still using structs for some odd reason (like I'm working on a project with mixed code, and I'm using a function that returns a struct) could I also use delete, or would it be best to stick with free or some other solution?

    Thanks,
    Aoife


    AFAIR struct and class are interchangable in C++ except that in struct members default to public instead of private...

    the term struct was only retained for compatibility with C and essentially means a class which members default to public.. you can even declare constructors and destructors!


  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    In C you use malloc or calloc to allocate space for your structure, and use free to return that piece of memory to free space. In C++ you're free to use delete or free, but it's good practice to pair new with delete and c/malloc with free.


  • Closed Accounts Posts: 2,039 ✭✭✭rmacm


    As bpmurray said use malloc in C to allocate space for structures and then use free to return the memory to the available free space. The way we were taught in college using C++ was to use the new and delete keywords and make sure you pair them as mentioned.

    Torak structs and classes are not the same thing. Structs are data structures well at least as far as we were shown in college. Structs don't contain any methods on the data they contain which would make them different to classes.

    Cheers
    Rory


  • Closed Accounts Posts: 80 ✭✭Torak


    rmacm wrote:
    Torak structs and classes are not the same thing. Structs are data structures well at least as far as we were shown in college. Structs don't contain any methods on the data they contain which would make them different to classes.

    Cheers
    Rory

    http://www.research.att.com/~bs/glossary.html
    Stroustrup wrote:
    struct - class with members public by default. Most often used for data structures without member functions or class invariants, as in C-style programming. TC++PL 5.7, 10.2.8, D&E 3.5.1.

    Considering he invented the language I will take his word over yours..
    :cool:


  • Closed Accounts Posts: 3,285 ✭✭✭Smellyirishman


    That's basically what rmacm said.


  • Advertisement
  • Closed Accounts Posts: 80 ✭✭Torak


    That's basically what rmacm said.

    :eek:

    read it again..

    "most often" does not mean "don't"

    idioms and usage patterns are not syntactical rules

    The definition given by stroustrup is identical to the information originally furnished by me and questioned by rmacm.

    :cool:


Advertisement