memset on stl string

  1. #1
    cyberizen is offline Member cyberizen is an unknown quantity at this point (<10)
    Join Date
    Nov 2002
    Location
    Karachi Pakistan
    Posts
    53

    Question memset on stl string

    Hi

    I have used memset for initializing values to 0 for primitive type like integers floats chars but I have problems when I try to do the same with stl strings consider this


    Code:
    struct foo
    {
     public:
            int a;
            char c;
            string s;
            float f;  
    };
    class bar
    {
    protected:
           foo f;
    public:
           bar()
          {
                memset(&cdr,0,sizeof(f));
          }
    };
    int main()
    {
            bar *b = new bar();
            delete b;
            return 0;
    }
    The above program gives a segmentation fault. But if I comment the string it works just fine so what exactly is the problem here?

    Regards
    Ahmed Bhaila
  2. #2
    Bob Davis is offline Member + Bob Davis has a spectacular aura about (150+) Bob Davis has a spectacular aura about (150+)
    Join Date
    Jan 2001
    Posts
    588

    Re: memset on stl string

    Simply stated, you can't do that. You can't use C-style functions meant for primitive types on non-primitive types. Period. That's what constructors are for.
  3. #3
    Paul McKenzie is offline Elite Member Power Poster Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+)
    Join Date
    Apr 1999
    Posts
    26,306

    Re: memset on stl string

    Quote Originally Posted by  cyberizen
    Hi

    I have used memset for initializing values to 0 for primitive type like integers floats chars but I have problems when I try to do the same with stl strings
    Of course you have a problem. What do you think happens when you wipe out all of the std::string's internals with memset? You've messed up the std::string object, plain and simple. 

    Bottom line is that you can't use memset on non-POD classes. You need to code a legitimate constructor for the foo object once you introduce non-POD members such as std::string. 
    The above program gives a segmentation fault. But if I comment the string it works just fine so what exactly is the problem here?
    Read up on POD (Plain-Old-Data, i.e. C-compatible) types and non-POD types. A std::string is a non-POD type, and you must use it in accordance to its public interface. No 'C' functions such as memset(), memcpy() or anything like that that works on raw memory can be used on these types.

    Regards,

    Paul McKenzie
  4. #4
    cyberizen is offline Member cyberizen is an unknown quantity at this point (<10)
    Join Date
    Nov 2002
    Location
    Karachi Pakistan
    Posts
    53

    Re: memset on stl string

    ok so this simple means that If I have say like many integers and other primitive types and a single string the only way to initialize them is by equating every element to 0 one by one


    转载:http://forums.codeguru.com/showthread.php?364259-memset-on-stl-string


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值