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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:55:37+00:00 2026-05-29T07:55:37+00:00

Right now, I want to increase the size of the array using a function.

  • 0

Right now, I want to increase the size of the array using a function.

#include <iostream>
using namespace std;

void IncreaseArraySize(int* addr){
    int* temp = new int[20];
    for(int i=0;i<10;i++){
        temp[i] = addr[i];
    }
    for(int i=10;i<20;i++){
        temp[i] = i;
    }
    int* dummy = addr;
    addr = temp;
    delete[] dummy;
}

int main(){
    int* test = new int[10];
    for(int i=0;i<10;i++){
        test[i] = i;
    }
    IncreaseArraySize(test);
    for(int i=0;i<20;i++){
        cout<<"at index "<<i<<"we have"<<test[i]<<endl;
    }
    cout<<"ok!"<<endl;
    delete[] test;
}

I ran the code with:
valgrind –leak-check=full ./test 2>debug.txt

and this is what I got for the output:

at index 0we have0
at index 1we have1
at index 2we have2
at index 3we have3
at index 4we have4
at index 5we have5
at index 6we have6
at index 7we have7
at index 8we have8
at index 9we have9
at index 10we have0
at index 11we have0
at index 12we have0
at index 13we have0
at index 14we have0
at index 15we have0
at index 16we have0
at index 17we have0
at index 18we have112
at index 19we have0
ok!

and this is what I got at the debug.txt:

==4285== Memcheck, a memory error detector
==4285== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4285== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==4285== Command: ./test
==4285== 
==4285== Invalid read of size 4
==4285==    at 0x400997: main (test.cpp:24)
==4285==  Address 0x596f040 is 0 bytes inside a block of size 40 free'd
==4285==    at 0x4C27C6E: operator delete[](void*) (vg_replace_malloc.c:409)
==4285==    by 0x400931: IncreaseArraySize(int*) (test.cpp:14)
==4285==    by 0x400980: main (test.cpp:22)
==4285== 
==4285== Invalid free() / delete / delete[]
==4285==    at 0x4C27C6E: operator delete[](void*) (vg_replace_malloc.c:409)
==4285==    by 0x400A16: main (test.cpp:27)
==4285==  Address 0x596f040 is 0 bytes inside a block of size 40 free'd
==4285==    at 0x4C27C6E: operator delete[](void*) (vg_replace_malloc.c:409)
==4285==    by 0x400931: IncreaseArraySize(int*) (test.cpp:14)
==4285==    by 0x400980: main (test.cpp:22)
==4285== 
==4285== 
==4285== HEAP SUMMARY:
==4285==     in use at exit: 80 bytes in 1 blocks
==4285==   total heap usage: 2 allocs, 2 frees, 120 bytes allocated
==4285== 
==4285== 80 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4285==    at 0x4C2864B: operator new[](unsigned long) (vg_replace_malloc.c:305)
==4285==    by 0x4008A9: IncreaseArraySize(int*) (test.cpp:5)
==4285==    by 0x400980: main (test.cpp:22)
==4285== 
==4285== LEAK SUMMARY:
==4285==    definitely lost: 80 bytes in 1 blocks
==4285==    indirectly lost: 0 bytes in 0 blocks
==4285==      possibly lost: 0 bytes in 0 blocks
==4285==    still reachable: 0 bytes in 0 blocks
==4285==         suppressed: 0 bytes in 0 blocks
==4285== 
==4285== For counts of detected and suppressed errors, rerun with: -v
==4285== ERROR SUMMARY: 22 errors from 3 contexts (suppressed: 4 from 4)

Could you explain this in newbie terms?

  • 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-29T07:55:38+00:00Added an answer on May 29, 2026 at 7:55 am

    I believe that your problem is that because you are passing the pointer to the start of the array by value, once you’ve updated and reassigned it, the changes aren’t propagating to the caller. If you change the function so that it takes the pointer by reference, this should be fixed:

    void IncreaseArraySize(int*& addr){
    

    Right now, your bug is caused because when you call

    IncreaseArraySize(test);
    

    The test pointer back in main isn’t getting reassigned. As a result, once you delete[] it in IncreaseArraySize, it references garbage memory. Updating the parameter so that it’s passed by reference means that when, in IncreaseArraySize, you say

    addr = temp;
    

    This will update the test pointer in main, preventing the bug.

    Hope this helps!

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

Sidebar

Related Questions

I am making a site right now and I want to include the sidebar
so I'm in a little over my head right now and want to verify
I've created a custom view class and right now just want to draw an
Right now, if I want to run mysql, I have to do /Applications/MAMP/Library/bin/mysql -u
right now its set up to write to a file, but I want it
Right now, I have more than 25 vertices that form a model. I want
I'm trying to use regular expression right now and I'm really confuse. I want
I want to implement an automatic update system for a windows application. Right now
I want to create an SQL script that creates a database. Right now, I
I have a set where i want to find items in it. Right now

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.