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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:33:34+00:00 2026-06-11T15:33:34+00:00

This has been destroying me for a while. I’m sure there’s a reason for

  • 0

This has been destroying me for a while. I’m sure there’s a reason for this:

chain operator+(chain c)
{
    chain<Object> result;
    for (int i = 0; i < length(); i++)
    {
        result.insert(*(Object*)(memory+(i*sizeof(Object))));
    }
    for (int i = 0; i < c.length(); i++)
    {
        result.insert(c[i]);
    }
    for (int i = 0; i < result.length(); i++) // This for loop successfully shows all objects in result
    {
        cout << result[i];
    }
    return result;
}

When the value is returned, ie:

chain<int> a;
cin >> a; // enter "5 6 7 8"
chain<int> b;
cin >> b; // enter "9 10 11 12"
chain <int> c = a+b;

cout << c; // Returns "0 0 7 8 9 10 11 12"

The first two numbers are always 0. I can’t figure out why. This only happens when adding two chains together; if I cout a or b, I get all of the values.

I would really appreciate it if anyone has any info to share 🙂

EDIT**

Full Source

#ifndef CHAIN_H
#define CHAIN_H

#include <iostream>
#include <stdlib.h>

using namespace std;

template <class Object>
class chain
{
    public:
            chain(){
                    memorySize = 8;
                    memory = calloc(memorySize, sizeof(Object));
                    count = 0;
            }
            chain(Object item){
                    memorySize = 8;
                    memory = calloc(memorySize, sizeof(Object));
                    count = 0;
                    insert(item);
            }
            chain(chain & original){
                    memorySize = 8;
                    memory = calloc(memorySize, sizeof(Object));
                    count = 0;
                    for (int i = 0; i < original.length(); i++)
                    {
                            insert(original[i]);
                    }
            }
            ~chain(){
                    free(memory);
            }
                chain operator+(chain c){
                    chain<Object> result;
                    for (int i = 0; i < length(); i++)
                    {
                            result.insert(this->operator[](i));
                    }
                    for (int i = 0; i < c.length(); i++)
                    {
                            result.insert(c[i]);
                    }
                    for (int i = 0; i < result.length(); i++)
                    {
                            cout << result[i];
                    }
                    return result;
            }
            Object & operator[](int pos){
                    return *(Object*)(memory+(pos*sizeof(Object)));
            }
            int length(){
                    return count;
            }
            void insert(Object item){
                    if (count == memorySize)
                    {
                            doubleMemory();
                    }
                    this->operator[](count) = item;
                    count++;
            }
    private:
            int count;
            int memorySize;
            void * memory;
            void doubleMemory(){
                    memorySize *= 2;
                    memory = realloc(memory, (memorySize*sizeof(Object)));
            }

};
template <class Object>
ostream& operator<<(ostream& out, chain<Object>& c){
    for (int i = 0; i < c.length(); i++)
    {
            out << c[i] << " ";
    }
}
template <class Object>
istream& operator>>(istream& in, chain<Object>& c){
    char ch;
    int number = 0;
    int sign;
    while(ch != '\n')
    {
            ch = in.get();
            if (ch == '-')
            {
                    sign = 1;
            }
            else if (ch >= '0' && ch <= '9')
            {
                    number *= 10;
                    number += (ch-48);
            }
            else if (ch == ' ' || ch == '\n')
            {
                    number = sign == 1? 0 - number : number;
                    c.insert(number);
                    sign = 0;
                    number = 0;
            }
    }
}
#endif

Here’s the code I’m testing against:

#include "chain.h"

using namespace std;

int main(){

    chain<int> a, b, c;
    chain<int> d(10);
    chain<int> e(d);
    cin >> a;
    cout << endl;
    cout << endl;
    c = a+d;
    cout << c;
}

~

  • 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-11T15:33:36+00:00Added an answer on June 11, 2026 at 3:33 pm

    The code you’ve shown isn’t the problem. The real problem is probably either in the copy constructor or the destructor of chain – maybe also in the insert method (or, on C++11, in the move constructor).

    (It could also be in Object’s copy constructor but I think that’s unlikely.)


    EDIT: Oh my. Don’t write such code in C++. It’s unsafe left, right and center. As long as Object is a POD you should be fine but if it isn’t this code yields undefined behaviour. In particular, it doesn’t call the proper constructors and destructors for the objects you store in your chain.

    Furthermore, your copy constructor should take an argument of type chain const& since you’re not modifying the passed chain. This in turn requires that you make your class const correct by providing an appropriate const overload of operator [].

    Finally and most glaringly, you violate the rule of three because you don’t implement operator = for your chain. Trying to assign one chain to another will consequently result in double frees.

    Generally avoid calloc and free and use a standard container instead, or, if that’s not an option, use new[] plus a smart pointer like boost::shared_array to manage memory (but do not use delete[]).

    Another thing, never use using namespace in a header file, it will pollute the namespace and lead to name conflicts in the weirdest places.

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

Sidebar

Related Questions

This has been bothering me for a while, and I'm wondering if there's any
This has been frustrating me for a while now. I started developing a site
This has been troubling me for a while. It goes to the heart of
This has been bugging me for a while, so I asked a coworker if
I am sure this has been answered, but I am no programmer and couldn't
this has been an ongoing problem with me, ive been trying to make a
This has been a massive headache. We use Ning as a our platform for
This has been a rather problematic issue on numerous occasions. We have alot of
This has been driving me crazy for the past few minutes I have a
This has been one of the biggest obstacles in teaching new people ColdFusion. When

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.