试图避免分段错误,但我的输出看起来很奇怪(C++)

use*_*562 3 c++ arrays segmentation-fault

我正在做的家庭作业项目使用静态和动态数组.但是,我还没有实现动态数组,这是一个奇怪的差异,试图获得我的静态数组的长度.

我使用了一系列cout语句来尝试找出导致分段错误的原因,因为这个过程看起来很简单.我发现在我的驱动程序中,它计算的是正确的范围和长度,但是一旦我将数组传递给用户定义的类函数,在同一个数组上执行的相同语句就会产生不同的结果.

我的驱动功能:

using namespace std;

#include <iostream>
#include "/user/cse232/Projects/project07.string.h"

int main()
{
  const char string1[] = {'a', 'b', 'c', 'd', 'e', 'f'};
  String test1();
  cout << "Size of array: " << sizeof(string1) << endl;
  cout << "Size of item in array: " << sizeof(char) << endl;
  cout << "Length of array: " << (sizeof(string1)/sizeof(char)) << endl;
  cout << "Range of array" << (sizeof(string1)/sizeof(char))-1 << endl << endl;

  String test2(string1);
}
Run Code Online (Sandbox Code Playgroud)

运行时,我从驱动程序获取此输出:

Size of array: 6
Size of item in array: 1
Length of array: 6
Range of array5
Run Code Online (Sandbox Code Playgroud)

我的支持文件:

/* Implementation file for type "String" */

using namespace std;

#include <iostream>
#include "/user/cse232/Projects/project07.string.h"

String::String( const char Input[] )
{

  cout << "Size of array: " << sizeof(Input) << endl;
  cout << "Size of item in array: " << sizeof(char) << endl;
  cout << "Length of array: " << (sizeof(Input)/sizeof(char)) << endl;
  cout << "Range of array" << (sizeof(Input)/sizeof(char))-1 << endl << endl;

/*  Bunch of reallocation stuff that is commented out 
for the time being, unimportant*/
}

String::~String()
{
  Capacity = 0;
  Length = 0;
  Mem = NULL;
}
Run Code Online (Sandbox Code Playgroud)

这是我从支持文件中获得的输出,

Size of array: 4
Size of item in array: 1
Length of array: 4
Range of array3
Run Code Online (Sandbox Code Playgroud)

这显然是不对的.如果有帮助,这里是头文件(省略未实现的功能).这是不可改变的:

/******************************************************************************
   Project #7 -- Interface file for type "String"
******************************************************************************/

#ifndef STRING_
#define STRING_

using namespace std;

#include <iostream>

class String
{
  private:

    unsigned Capacity;  // Number of memory locations reserved
    unsigned Length;    // Number of memory locations in use
    char * Mem;         // Pointer to memory to hold characters

  public:

    // Construct empty string
    //
    String()
    {
      Capacity = 0;
      Length = 0;
      Mem = NULL;
    }

    // Destroy string
    //
    ~String();

    // Construct string by copying existing string
    //
    String( const String& );

    // Construct string by copying C-style character string
    //
    String( const char[] );

#endif
Run Code Online (Sandbox Code Playgroud)

我最大的问题是为什么我得到两个单独的输出.第一个是我在分配内存时需要使用的那个; 否则我会遇到分段错误.有人可以提供建议吗?

wkl*_*wkl 10

你的函数实际上正在做一些你没有意识到的事情 - 它会将你传入的数组衰减到指向第一个元素的指针.

基本上你的功能签名:String::String( const char Input[] )相当于签名String::String(const char *input).

所以当你这样做时sizeof,你实际上得到的是大小而char *不是数组.

您不能将数组直接传递给函数,并期望能够获得数组的大小.如果必须使用这样的原始数组,则可以更改函数,以便使用它传递数组的大小.如果你不能修改签名,那么你可以做的另一件事就是放入一个sentinel值(比如\0byte)并使用一个循环来计算数组的大小.

编辑

我认为你真正想要了解的是C风格的字符串,而不仅仅是使用非空终止的字符数组.这确实符合您的任务似乎希望您执行的操作的模型,并允许您避免操作这些不可变的标头.

  • 你正在搜索的词是"衰变". (2认同)