Trying to avoid segmentation error, but my conclusion looks strange (C ++)

The homework project I'm working on uses static and dynamic arrays. However, I am not yet implementing dynamic arrays, and this is a strange mismatch, trying to get the length of my static arrays.

I used a series of cout statements to try to figure out what gave me the segmentation error, as the process seemed simple. I found that in my driver, it calculated the correct range and length, but as soon as I passed the array to the user-defined function of the class, the same operators executed in the same array had different results.

My driver function:

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);
}

When I start, I get this output from the driver:

Size of array: 6
Size of item in array: 1
Length of array: 6
Range of array5

My support file:

/* 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;
}

And here is the result that I get from the support file,

Size of array: 4
Size of item in array: 1
Length of array: 4
Range of array3

, , . , ( ). :

/******************************************************************************
   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

- . - , ; . - ?

+3
4

, , - , .

: String::String( const char Input[] ) String::String(const char *input).

, sizeof, char *, .

, . , , , . , , , - (, \0 byte) .

, C, , , , , , , .

+10

, ,

foo(char[])

, , , , 4 ( 32- )

. , std::vector

+4

, . :

String::String( const char Input[] )

. .

+1

, . .

const size_t SIZE = 6; //Or whatever other array size you want to pass. 
String::String(const char (&Input)[SIZE])
{
    //TODO
}

, :

template<size_t N>
String::String(const char (&Input)[N])
{
    //TODO
}

, size_t, , int, ( , - . ).

-

const char (&Input)[SIZE]

,

  • ( &)
  • Input
  • , const char (. & , : const char & Input[SIZE], const char& - , , , th3e)
  • SIZE, SIZE .

, template, , , :-)

All this will only work for true arrays for which size is a constant value that can be computed at compile time. Otherwise, this will not work, and you will need to use the value of the control point (for example, '\0'and dynamically calculate the length of your "array", sorting through the contents.

+1
source

Source: https://habr.com/ru/post/1773102/


All Articles