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

  • Home
  • SEARCH
  • 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 8646339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:46:55+00:00 2026-06-12T12:46:55+00:00

I defined a class with a template that defines a generic array type T

  • 0

I defined a class with a template that defines a generic array type T number of elements N. I have another class that has an instance of this array as a member. When I try using the setString function, the array I pass goes from 15 elements, to 4 elements, arbitrarily.

// testClassArraySize.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;

template<class T, int N>
class CArray {
public:
    T arr[N];
    CArray(void) {/*arr=(T *)malloc(sizeof(T)*N);*/
        if (arr == NULL) {
            cout << "allocation error\n";
        }
    }
    ;
    //CArray (int n) {arr=new T [n]; if(arr==NULL){exit(0); cout<<"allocation error\n";}};
    CArray operator=(const T *);
    T operator[](const int i) {
        return arr[i];
    }
    ;
};

template<class T, int N>
CArray<T, N> CArray<T, N>::operator=(const T *srce) {
    size_t x = sizeof(arr);
    size_t y = sizeof(srce);
    for (int j = 0; j < sizeof(arr); j++) {
        if (j > sizeof(srce)) {
            arr[j] = 0;
            break;
        }
        arr[j] = srce[j];
    }
    return *this;
}

class myTestClass {
private:
    CArray<char, 15> myString;
public:
    myTestClass setString(char set[15]) {
        myString = set;
        size_t x = sizeof(set);
        return *this;
    }
    ;
};

int main() {
    myTestClass myObject;
    myObject.setString("helloWorld");
    return 0;
}

Does anyone have any idea why?

  • 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-06-12T12:46:56+00:00Added an answer on June 12, 2026 at 12:46 pm

    This has a couple of issues, but the one you’ll probably see is the line

    CArray<T, N> CArray<T, N>::operator= (const T *srce)
    

    Note: const T* source is a pointer, therefore sizeof(srce) is the size of the pointer. I guess you’re using a 32 bit system?

    sizeof gives you the size of an object (i.e. a “region of storage”). For arrays, that is the size of the whole array in bytes. sizeof( int[10] ) == sizeof(int) * 10. A pointer is itself an object, with a size depending on the C++ implementation (OS, compiler and so on). On 32 bit systems, it typically is 4 byte. sizeof( char* ) therefore is 4 byte, not the length of the array you passed to the function, that is sizeof( (char*)(char[10]) ) still is 4 byte, not 10.

    Another issue you may see (but only by debugging / tracing) is that setString(char set[15]) is resolved to setString(char* set). Therefore x = sizeof(set) resolves to x = sizeof(char*) which is typically 4.

    You pass “helloWorld” to setString, which is expecting a 15-item char arraychar*; I would say this is not a good idea since “helloWorld” has type char const[10] (note the const).
    The correct syntax for taking a 15-char array is char (&set)[15].

    You can do this more elegantly if you add a template member function like:

    // in class CArray
    template < std::size_t length >
    CArray& operator= (const T (&srce)[length]);    // note I return a reference, so no copying
    

    This way, you’ll get the array size as a template argument. Note: since I used const T here in the assignment op, you’ll need to enforce const also in setString.

    template < class T, int N >
    template < std::size_t srce_length >
    CArray < T, N >& CArray < T, N > :: operator= (const T (&srce)[srce_length])
    {
        for (int j = 0; j < N; j++) {    // N is the own length
            if (j >= srce_length) {    // note the >= instead of >, and srce_length instead of sizeof
                arr[j] = 0;
                break;
            }
            arr[j] = srce[j];
        }
        return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I'm using a library that defines an interface: template<class desttype> void connect(desttype* pclass, void
I have a template class defined like so: template <class T> class Command {
I have the convenient object factory template that creates objects by their type id
I'm writing a generic wrapper class for a bunch of classes we have defined
I have an abstract generic view-model that I use as a base-class for several
I have a template class defined as follow : template <class T1, class T2>
I have a template matrix class class defined in a header called Matrix.h. Certain
I have an abstract class called 'Template' defined as: [DataContract] public abstract class Template
I have implemented a templated buffer class like this: template <class T, class Impl>

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.