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:
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;
}
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
- . - , ; . - ?