Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3331426
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:34:59+00:00 2026-05-17T23:34:59+00:00

A homework project I’m working on uses static and dynamic arrays. However, I’m not

  • 0

A homework project I’m working on uses static and dynamic arrays. However, I’m not implementing the dynamic arrays yet and this is a weird discrepancy trying to get the length of my static arrays.

I used a series of cout statements to try and find out what was giving me a segmentation fault, since the process seemed straightforward. What I found was that in my driver, it was computeing a correct range and length, but once I passed the array into a user-defined class function, the same statements performed on 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 run, 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 output I get from the support file,

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

which is obviously not right. If it helps, here is the header file (omitting unimplemented functions). It is unalterable:

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

My biggest question is why I am getting two separate outputs. The first one is the one I need to use while allocating memory; otherwise I get a segmentation fault. Can anyone offer advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:34:59+00:00Added an answer on May 17, 2026 at 11:34 pm

    Your function is actually doing something you don’t realize – it decays the array you pass into a pointer to the first element.

    Basically your function signature: String::String( const char Input[] ) would be equivalent to the signature String::String(const char *input).

    So when you do sizeof, you’re actually getting the size of an char *, not of the array.

    You cannot pass an array directly to the function and expect to be able to get the size of the array. If you must use raw arrays like this, you can alter your function so that you pass in the size of the array with it. If you can not modify the signature, then another thing you can do is put in a sentinel value (like \0 byte) and use a loop to count the size of the array.

    edit

    What I think you really want to learn about is C-style strings, rather than just using a character array that is not null-terminated. This does fit the model of what your assignment seems to want you to do, and allows you to avoid manipulating these immutable headers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one is a case of not doing your homework.:-) Apart from dynamic loading
First yes, this is a homework project for my Perl class. I am not
Disclaimer: This is for a homework assignment, but the question is not regarding the
I'm doing a homework project that requires this: Below you will find the code
This is currently a homework project that me and my teammate are stuck on.
I'm working on a homework project and i'm trying to store inventory data into
I'm working on a homework project and I'm supposed to perform a database query
This is homework, just so that's mentioned. I have project called AdventCalendar, which is
(This is no homework and no work issue. It's just my personal interest/occupation and
I'm working through some homework and a question on a previous exam paper asks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.