-1

While running the below program, I am getting segfault. But if I comment memset, I am not getting seg fault. Not able to figure out the reason. What we can do instead of memset, if I need to clear the structure contents(such as value)?

#include <iostream>
#include <cstring>
#include <map>
using namespace std;

struct test
{
    int value;
    map<int, int> m;
};

void print(test *ptr)
{
    cout << "map size() : " << ptr->m.size();
    for(auto itr = ptr->m.begin(); itr != ptr->m.end(); ++itr)
    {   
    }   
}

int main()
{
    test obj;
    memset(&obj, 0, sizeof(obj));
    print(&obj);
}
6
  • 1
    You should have segfault at printing it out. When size() will try to get any internal pointer and it will be zeroed.
    – Arkady
    Aug 6, 2018 at 10:59
  • Even though after commenting the printf() still getting seg fault.
    – suresh m
    Aug 6, 2018 at 11:00
  • 2
    Implement a clear() method in the struct, don't memset everything to zero. You don't know what internal state map has, it's probably a bad idea to raze it
    – GPhilo
    Aug 6, 2018 at 11:04
  • @sureshm cpp.sh check: cpp.sh/6sup6 it fails on print
    – Arkady
    Aug 6, 2018 at 11:05
  • 4
    @sureshm You don't need memset() to initialize obj. C++ isn't C. In fact you'll screw up the internal map member when doing so. Aug 6, 2018 at 11:05

3 Answers 3

7

std::map is not a POD structure, so it has internals (such as pointers and etc) which will be initialized in it's contructor.

When you call memset, you zeroed the entire structure, making any access to it undefined behavior.

And then you call function size(), which crash your code, because it tries to read any internal which is at zero address now.

Live Demo on cpp.sh

Instead of memset you should use specified method of std::map -> clear(), when you want to clear it.

6

Calling memset on a type that is not trivially copyable is undefined behaviour (https://en.cppreference.com/w/cpp/string/byte/memset). std::map is not trivially copyable, therefore your struct test also is not.

If you want to set value to 0, you can just call obj.value = 0, and if you want to clear m, you can call obj.m.clear().

2

std::memset is only intended for objects that are trivially copyable. For something like std::map (which is a component of test), that's undefined behavior.

While a default-initialized int is going to be an inderminate value, std::map has a default constructor and as will start with its own, correct bookkeeping. But that gets quashed by std::memset.

You pretty much don't need std::memset in C++.

One quick and dirty option is to value-initialize test. This zero-initializes values of non-class types such as int:

test obj{}

To reset it you'd do

obj = test{}

An even better option is to give test a default constructor which sets m to zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.