2

i want to use struct to define a variable.
is it better to define a variable using struct as a pointer or no pointer?

for example it's my struct:

 struct mystruct {
    //data members
    double x, y, z; 
}

and i use that in my below code:

  void main() {
    mystruct s1;                    //form 1
    mystruct  * s2 = new mystruct;  //form 2
    //.....
}

which variable definition is better to use ? s1 or s2 ?

4 Answers4

1

I personally try to avoid pointers and dynamically allocated memory when at all possible.

  1. Pointers always have to be checked for null
  2. Dynamic memory can cause memory leaks
  3. Easy to accidentally assign a pointer rather then the value s2=.. rather *s2=

For a previous discussion on this see Are pointers bad?

Community
  • 1
  • 1
markshancock
  • 690
  • 2
  • 7
  • 25
1

It's always better to use an object, i.e. not a pointer, if that works.

  1. Using objects is faster than using a pointer and allocating memory from the heap.

  2. Using objects results in fewer and cleaner lines of code.

  3. Using objects avoids the issues of needing to deallocate memory when the memory is not necessary.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Unless you have a specific reason to use a pointer (for instance, if you're creating a bunch of instances of this struct and storing the pointers in a vector), it's usually better not to.

There are a few reasons for this:

  1. You have to remember to delete the thing the pointer is pointing to, or you'll end up with memory leaks.
  2. You have to dereference the pointer before you can use the thing it's pointing to.
  3. If you do something to change the pointer (like add a value to it, when you meant to add a value to a data member, instead), you'll get a pointer that's pointing to who knows what.

In general, I prefer to use objects on the stack instead of pointers to objects, if for no other reason than to make it easier for myself by not having to worry about these things.

Regarding your (apparently deleted) comment on class vs struct, they're essentially the exact same thing. They differ slightly syntactically (for example, members of a class are private by default, while members of a struct are public by default), but there's no real difference, as far as I know. In addition, a pointer doesn't care what sort of thing it's pointing to; it's in essence just two numbers: a location in memory, and the expected size of the thing at that location.

1
  void main() {
    mystruct s1;                    //form 1
    mystruct  * s2 = new mystruct;  //form 2
    //.....
}

which variable definition is better to use ? s1 or s2 ?

s1.

In C++, you use pointers and dynamic memory allocation only if you have to. If you don't gain any advantage from those features, then you are left with only their disadvantages.

Some of the disadvantages you will face with manual new/delete and smart pointers like std::unique_ptr or std::shared_ptr:

  • More indirection in your code.
  • Need for nullptr sanity checks in one form or another.
  • More verbosity in your code.

Special disadvantage of std::shared_ptr:

  • Can add noticeable synchronisation overhead in multi-threaded code even in situations where you would not need the internal synchronisation.

Some of the disadvantages you will only face with manual new/delete:

  • Easy to generate memory leaks because memory has to manually freed.
  • Tends to create the need for even more nullptr sanity checks everywhere in your code.
  • Easy to generate undefined behaviour by accidental multiple deletions.
  • Easy to generate undefined behaviour by accessing memory after it has been freed.
  • Hard to make exception-safe.

Some references:

  • You don’t need to use new to create an object if you also delete that object in the same scope; such an object should be a local variable.

    Source: https://isocpp.org/wiki/faq/freestore-mgmt#new-java

  • Always use the standard smart pointers, and non-owning raw pointers. Never use owning raw pointers and delete, except in rare cases when implementing your own low-level data structure (and even then keep that well encapsulated inside a class boundary).

    Source: http://herbsutter.com/elements-of-modern-c-style/


P.S.: void main is not legal C++. Make it int main.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62