-1

Im trying to create this and add push back functionality. How can i do it? I came up with this code but im very confused for 2 days because it gives this output - | -842150451 | -842150451 | -842150451 | -842150451 | -842150451 | -842150451 | -842150451 | -842150451 I will be thankful if someone can tell me how this function push_back should look or at least point me in the right direction. Also i would be thankful if you tell me where this code is doing wrong?

 struct IntArray{
private:
    int k;

    int* first_cell;

    int size; // currently occupied elements
    int capacity = 8; // size of the allocated memory

public:
    void create() {
        first_cell = (int*)malloc(capacity * sizeof(int));
    }

    void random_numbers_fill(int limit) {

        srand(time(NULL));

        for (k = 0; k < capacity; k++) {

            first_cell[k] = rand() % limit;
        }

    }

    void push_back(int number) {

        if (size == capacity) {
            int* new_arr;
            capacity *= 2;
            new_arr = (int*)malloc(capacity * sizeof(int));
            for (k = 0; k < capacity; k++) {
                new_arr[k] = first_cell[k];
            }
            free(first_cell);
            first_cell = new_arr;
            first_cell[size] = number;
            size++;
        }

    }

    void print() {
        for (k = 0; k < capacity; k++) {
            printf("| %d ", first_cell[k]);
        }
    
Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
Varto
  • 51
  • 5
  • 1
    Please show a [mcve] including expected vs. actual output. Also read this: [ask] – Jabberwocky Jan 11 '21 at 20:34
  • @Jabberwocky Im asking for the push_back function how it should work because im confused how it should work. – Varto Jan 11 '21 at 20:37
  • `-842150451` looks like your compiler is trying to tell you something. That number is the same as `0xcdcdcdcd` which is uninitialized heap memory on Visual Studio: [https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – drescherjm Jan 11 '21 at 21:00
  • 1
    [Here's a handy list of common debugging codes.](https://en.wikipedia.org/wiki/Magic_number_(programming)#Debug_values). In general when you see weird-ass number, convert it to Hex. If you get something highly repetitious or a pattern that's unlikely to not be by design like a word or phrase, check to see if it means something special – user4581301 Jan 11 '21 at 21:28

2 Answers2

2

First, create() should be changed into an actual constructor. And you are missing a destructor to free the array, as well as copy/move constructors and copy/move assignment operators to manage the array (per the Rule of 3/5/0).

Second, you should be using new[] instead of malloc().

Third, regarding your push_back(), it is not a bad attempt, but it is buggy. You are not adding the number to the array at all if the current size is less than the current capacity (and your constructor is not initializing size at all). And when you do resize + copy the array, you are copying too many ints from the old array. The old array is size elements, but you are trying to copy the newly increased capacity elements from it.

With that said, try something more like this instead:

#include <iostream>
#include <algorithm>
#include <utility>

class IntArray{
private:
    int* first_cell = nullptr;
    int size = 0; // currently occupied elements
    int capacity = 8; // size of the allocated memory

public:
    IntArray()
    {
        first_cell = new int[capacity];
    }

    IntArray(const IntArray &src)
        : size(src.size), capacity(src.capacity)
    {
        first_cell = new int[capacity];
        std::copy_n(src.first_cell, size, first_cell);
    }

    IntArray(IntArray &&src)
        : first_cell(src.first_cell), size(src.size), capacity(src.capacity)
    {
        src.first_cell = nullptr;
        src.size = src.capacity = 0;
    }

    ~IntArray()
    {
        delete[] first_cell;
    }

    IntArray& operator=(IntArray rhs)
    {
        IntArray temp(std::move(rhs));
        std::swap(first_cell, temp.first_cell);
        std::swap(size, temp.size);
        std::swap(capacity, temp.capacity);
        return *this;
    }

    void push_back(int number)
    {
        if (size == capacity)
        {
            int new_cap = capacity * 2;
            int* new_arr = new int[new_cap];
            for (int k = 0; k < size; ++k) {
                new_arr[k] = first_cell[k];
            }
            delete[] first_cell;
            first_cell = new_arr;
            capacity = new_cap;
        }
        first_cell[size] = number;
        ++size;
    }

    void print() const
    {
        for (int k = 0; k < size; ++k) {
            std::cout << "| " << first_cell[k] << " ";
        }
    }
};

That being said, you should just get rid of your manual array and use std::vector<int> instead, as it handles all of these details for you, eg:

#include <iostream>
#include <vector>

class IntArray{
private:
    std::vector<int> vec;

public:
    IntArray()
    {
        vec.reserve(8);
    }

    void push_back(int number)
    {
        vec.push_back(number);
    }

    void print() const
    {
        for (int number : vec) {
            std::cout << "| " << number << " ";
        }
    }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

You never initialize size in create.

Your push_back has two problems. You don't copy the number that is pushed unless you grow the array (the last two statements should be outside the if block), and the condition in the for loop should compare with size, not capacity.

print also has the wrong condition in the for loop.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56