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 8516181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:21:11+00:00 2026-06-11T05:21:11+00:00

I am attempting to write a template/class that has a few functions, but I’m

  • 0

I am attempting to write a template/class that has a few functions, but I’m running into what seems like a rather newbie problem. I have a simple insert function and a display values function, however whenever I attempt to display the value, I always receive what looks like a memory address(but I have no idea), but I would like to receive the value stored (in this particular example, the int 2). I’m not sure how to dereference that to a value, or if I’m just completely messing up. I know that vectors are a better alternative, however I need to use an array in this implementation – and honestly I would like to gain a more thorough understanding of the code and what’s going on. Any help as to how to accomplish this task would be greatly appreciated.

Example Output (running the program in the same way every time):
003358C0

001A58C0

007158C0

Code:

#include <iostream>
using namespace std;

template <typename Comparable>
class Collection
{
public: Collection() {
    currentSize = 0;
    count = 0;
    }
    Comparable * values;
    int currentSize; // internal counter for the number of elements stored
    void insert(Comparable value) {
        currentSize++; 
                // temparray below is used as a way to increase the size of the 
                // values array each time the insert function is called
        Comparable * temparray = new Comparable[currentSize];
        memcpy(temparray,values,sizeof values);

                // Not sure if the commented section below is necessary, 
                // but either way it doesn't run the way I intended

        temparray[currentSize/* * (sizeof Comparable) */] = value; 
        values = temparray;
    }
    void displayValues() {
        for (int i = 0; i < currentSize; i++) {
            cout << values[i] << endl;
        }
    }
};

int main()
{
Collection<int> test;
int inserter = 2;
test.insert(inserter);
test.displayValues();
cin.get();
    return 0;
}
  • 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-11T05:21:12+00:00Added an answer on June 11, 2026 at 5:21 am

    Well, if you insist, you can write and debug your own limited version of std::vector.

    First, don’t memcpy from an uninitialized pointer. Set values to new Comparable[0] in the constructor.

    Second, memcpy the right number of bytes: (currentSize-1)*sizeof(Comparable).

    Third, don’t memcpy at all. That assumes that Comparable types can all be copied byte-by-byte, which is a severe limitation in C++. Instead:

    EDIT: changed uninitialized_copy to copy:

    std::copy(values, values + currentSize - 1, temparray);
    

    Fourth, delete the old array when it’s no longer in use:

    delete [] values;
    

    Fifth, unless the code is going to make very few insertions, expand the array by more than one. std::vector typically increases its size by a factor of 1.5.

    Sixth, don’t increment currentSize until the size changes. That will change all those currentSize-1s into currentSize, which is much less annoying. <g>

    Seventh, an array of size N has indices from 0 to N-1, so the top element of the new array is at currentSize - 1, not currentSize.

    Eighth, did I mention, you really should use std::vector.

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

Sidebar

Related Questions

When attempting to write/read cookies that have brackets in the name, it seems like
I am attempting to write a method that has an optional EventHandler Paramater. it
I am attempting to write a interpreter that will turn a string like: vector(random(0,
I'm attempting to write my first php/mysql page and am running into a SQL
I am curretnly attempting to write a script in python that allows me to
I have been attempting to write a VBA Script that can parse out other
i'm attempting to write the result of the $x = [System.Net.Dns]::GetHostAddresses($name) statement into my
I am attempting to write a time management tool with wxPython that is ideally
I am attempting to create a Tab Control Style that basically looks like buttons
I am attempting to write integration tests for a Grails service that does, among

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.