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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:43:33+00:00 2026-05-19T02:43:33+00:00

I have an array of pointers that point to arrays of ints. I have

  • 0

I have an array of pointers that point to arrays of ints.
I have made a hard coded array of ints (check ‘array’ below) and I want to insert it into the array of pointers (check ‘bar’ below), as far as I can tell, it is still an array of pointers that point to arrays of ints.

It compiles fine; no warnings.
It runs fine; and closes fine;
Until at least, I introduce the currently commented out delete statement in the destructor.

I imagine my compiler would make a better destructor at this point, but I’m interested as to what I’m doing wrong. The terminal window just throws out a gigantic memory map; and googling and searching SO didn’t help much.

class foo {
    public:
    int **bar;
    int aSize;

    //CONSTRUCTOR
    foo(int aSize) {
        this->aSize = aSize;
        bar = new int*[aSize];
        for (int i=0;i<aSize;i++) {
            bar[i] = new int[aSize + i];
            for (int j=0;j<(aSize + i);j++) {
                bar[i][j] = 0;
            }
        }
        }
    void myfunc(int *pointer) {
        bar[0] = pointer;
    }
    ~foo() {
        for (int i=0;i<aSize;i++) {
            //delete[] bar[i];
        }
        delete[] bar;
    }
};
int main() {
    foo *obj = new foo(5);

    int array[] = {1,2,3,4};

    obj->myfunc(array);

    delete obj;
    return 0;
};

I know I’ve probably done something tragic; I just don’t know what it is yet.
There is a reason why I am not using STL or other templates, it is simply because I’m interested in learning this.
Maximum criticism appreciated.

  • 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-19T02:43:34+00:00Added an answer on May 19, 2026 at 2:43 am

    The myfunc function takes in a pointer, and then sets bar[0] to that memory address. In your example code, you pass it the address of array, which is an automatic (stack) variable. The destructor then attempts to delete[] bar[0], which points to a stack variable. This is completely undefined behavior, and the reason your program is crashing. You can’t delete[] a stack array. You can only delete[] an array which was allocated using new[].

    Also, the myfunc function is always going to leak memory, because bar[0] points to a heap-allocated array. When you set bar to a different memory address without first delete[]ing the previous address, you are leaking memory.

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

Sidebar

Related Questions

I have an array of pointers to a base class, so that I can
What if i want to have an array of pointers to a function and
The problem is like this: I have an array of 500 pointers which point
I have a C code that creates an array of char pointers as the
Say I have a group of struct's that I want to manage arrays of
I have a class (TExample) and I want to have an array of pointers
I have a 3x3 array that I'm trying to create a pointer to and
I have an array of pointers to Objective-C objects. These objects have a sort
I couldn't find anything similar to this anywhere. I have an array of pointers
I have an array of double pointers, but every time I try do print

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.