0

In C we set data to 0 with memset() function (Not only for the initialization).

My data could be changed by another method and I want to reuse it and then I want to set it to 0 before reusing it

Are there another way to set data to 0 in C++ other than memset()?

class MYClass : public NameSpace::ParentClass {

    private:
        struct mystruct data

    public:
        void method();
};

void MYClass::method() {
    memset(&data, 0, sizeof(data)); // it's the C way. Are there another way in C++
}
Deanie
  • 2,316
  • 2
  • 19
  • 35
MOHAMED
  • 41,599
  • 58
  • 163
  • 268

3 Answers3

11

In C++ you would invoke the constructor of an object, or the initialiser for primitive constructor-less objects.

Re-initialisation should actually happen very rarely (don’t reuse variables, create new ones! It makes the program flow clearer) – but when you need it you can just re-assign a new object:

data = mystruct();
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
3

It is ok to use memset on POD types, otherwise initialization must be done using constructor.

Community
  • 1
  • 1
alexrider
  • 4,449
  • 1
  • 17
  • 27
  • You *can* certainly use `std::memset` but in general why would you? After all, who would write `int i; memset(&i, 0 sizeof i);`? Nobody, because it’s completely unreadable. So why would you use the same convoluted operation for other types? – Konrad Rudolph Apr 18 '13 at 12:43
  • Also be aware there are some POD types (e.g. pointers, floating-point types, pointer-to-member) where it's not guaranteed by the standard that all-bits-0 is a valid representation. In practice you're unlikely to encounter this for pointers or floats, I'm not quite so confident about pointer-to-member. – Steve Jessop Apr 18 '13 at 13:20
0

The C++ way of zeroing should look like the following:

struct mystruct
{
    int i;

    void Reset();
}

class MYClass : public NameSpace::ParentClass 
{
private:
    mystruct data

public:
    void method();
};

void mystruct::Reset()
{
    i = 0;
}

void MYClass::method() 
{
    data.Reset();
}
Spook
  • 25,318
  • 18
  • 90
  • 167
  • 2
    Having a `reset` method is almost always an anti-pattern. – Konrad Rudolph Apr 18 '13 at 12:50
  • Let's then assume, that in case of OP, this is an 'almost' case :) – Spook Apr 18 '13 at 12:56
  • @KonradRudolph: is it OK if it's called `clear()` instead? ;-) – Steve Jessop Apr 18 '13 at 13:22
  • @Steve Not particularly, no – but containers are a special beast anyway, since most languages, including C++, treat them as inherently mutable rather than values. However, this is **not** a good rule to follow for most types that you create yourself. – Konrad Rudolph Apr 18 '13 at 13:24
  • The best rationale for `.clear()` is spelling out the alternative: `myMap = std::map();` - should I be repeating the type? Or `myMap = decltype(myMap)` - should I be repeating the name? Any syntax similar to `myMap = FOO` is troublesome. – MSalters Apr 18 '13 at 15:42
  • @MSalters I tend to disagree, in particular since I believe that resetting isn’t (or rather, shouldn’t) be a common use-case at all. – Konrad Rudolph Apr 18 '13 at 17:26