简体   繁体   English

声明 int 变量时出现分段错误(核心转储)错误

[英]Getting segmentation fault (core dumped) error when declaring a int variable

I was trying to create vector like class.我试图创建像类一样的向量。 Here is my code这是我的代码

#include <bits/stdc++.h>
using namespace std;

template<class t>
class vec{
    t *start;
    int size=0;
    public:
      void add_value(t a){
            *(start+(sizeof(t)*size)) = a;
            cout << (start+(sizeof(t)*size))<< " = "<< *(start+(sizeof(t)*size))<<endl;
            size++;
            // cout << start<< endl;
        }

        void operator [](int i){
            cout << (start+(sizeof(t)*i))<< " = "<< *(start+(sizeof(t)*i))<<endl;
        }
        int length(){
            return size;
        }
};

int main(){
    vec<int> t;
    cout << t.length()<<endl;
    t.add_value(8);
    t.add_value(10);
    cout << t.length()<<endl;
    t[1];
}

This gives me correct output.这给了我正确的输出。

0
0x7fff0fe9b5d0 = 8
0x7fff0fe9b5e0 = 10
2
0x7fff0fe9b5e0 = 10

But when declare a int variable in main function like.但是当在 main 函数中声明一个 int 变量时。

int main(){
    int i=0;  //newline
    vec<int> t;
    cout << t.length()<<endl;
    t.add_value(8);
    t.add_value(10);
    cout << t.length()<<endl;
    t[1];
}

output.输出。

0
Segmentation fault (core dumped)

I also tried printing address of start variable and new int variable int those are different.我还尝试打印 start 变量和新 int 变量 int 的地址,它们是不同的。

You probably want something like this:你可能想要这样的东西:

#include <iostream>

using namespace std;

template<class t>
class vec {
  t* start = new t[100];   // initialize the pointer (fixed size of 100, to be improved)
  int size = 0;                                      
public:
  void add_value(t a) {
    *(start + size) = a;
    size++;
  }

  t operator [](int i) {   // should return a t instead of a void
    return *(start + i);
  }

  int length() {
    return size;
  }
};

int main() {
  vec<int> t;
  cout << "t.length() = " << t.length() << endl;

  t.add_value(8);
  t.add_value(10);

  cout << "t.length() = " << t.length() << endl;
  
  // display all values in t
  for (int i = 0; i < t.length(); i++)
  {
    cout << "t[" << i << "] = " << t[i] << endl;
  }
}

All multiplication by sizeof(t) have been removed, because pointer arithmetic already does this for you.所有乘以sizeof(t)乘法都已删除,因为指针算术已经为您执行了此操作。

This very poor and minimalist example works as you expect, but the maximum number of elements you can store in the class is 100. I leave the improvement as an exercise to you.这个非常糟糕且极简的示例按您的预期工作,但您可以在类中存储的最大元素数是 100。我将改进留给您作为练习。

BTW there are many other improvements you need to do, especially you need a destructor and possibly the rule of three顺便说一句,您还需要做许多其他改进,尤其是您需要一个析构函数和可能的三规则

Note:笔记:

You should replace all instances of *(start + x) with start[x] which does exactly the same thing but which is more idiomatic and more readable.你应该用start[x]替换*(start + x)所有实例,它做完全相同的事情,但更惯用,更易读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM