Unable to read memory when trying to increase size of an array
Im trying to understand pointers but im having trouble increasing the size of an array, what im trying to do is make an array that is one bigger than the standard array, copy over everything there, then delete the old pointer array, create a new pointer array that is the right size and move back everything from this temp array. For some reason I keep getting "Unable to read memory" and I have no clue why.
#include <iostream>
int main()
int number;
int arraySize = 3;
bool full;
int *ptr = new int[arraySize] 0;
do
full = false;
std::cout << "Input a number please: ";
std::cin >> number;
getchar();
for (int i = 0; i < arraySize; i++)
if (ptr[i] == 0)
ptr[i] = number;
i = arraySize;
else if(arraySize -1 == i)
full = true;
if (full == true)
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; x++ )
tempPtr[x] = ptr[x];
delete ptr;
arraySize++;
int *ptr = new int[arraySize];
for (int x = 0; x < arraySize; x++)
ptr[x] = tempPtr[x];
ptr[arraySize] = number;
while(number != -1);
for (int z = 0; z < arraySize; z++)
std::cout << ptr[z] << std::endl;
getchar();
return 0;
c++ arrays pointers
add a comment |
Im trying to understand pointers but im having trouble increasing the size of an array, what im trying to do is make an array that is one bigger than the standard array, copy over everything there, then delete the old pointer array, create a new pointer array that is the right size and move back everything from this temp array. For some reason I keep getting "Unable to read memory" and I have no clue why.
#include <iostream>
int main()
int number;
int arraySize = 3;
bool full;
int *ptr = new int[arraySize] 0;
do
full = false;
std::cout << "Input a number please: ";
std::cin >> number;
getchar();
for (int i = 0; i < arraySize; i++)
if (ptr[i] == 0)
ptr[i] = number;
i = arraySize;
else if(arraySize -1 == i)
full = true;
if (full == true)
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; x++ )
tempPtr[x] = ptr[x];
delete ptr;
arraySize++;
int *ptr = new int[arraySize];
for (int x = 0; x < arraySize; x++)
ptr[x] = tempPtr[x];
ptr[arraySize] = number;
while(number != -1);
for (int z = 0; z < arraySize; z++)
std::cout << ptr[z] << std::endl;
getchar();
return 0;
c++ arrays pointers
What is a debugger and how can it help me diagnose problems?
– Jesper Juhl
Nov 11 at 10:30
How to debug small programs
– Jesper Juhl
Nov 11 at 10:31
Every time you are doingint *ptr = new int[ArraySize]
the result is creating a different variable with the same name, which disappears as soon as it passes out of scope. Only do that once. After the first time, only assign toptr
rather than defining a new variable with that name i.e.ptr = new int[ArraySize]
.
– Peter
Nov 11 at 10:31
@Peter but I have to create one after I have deleted it?
– filipanton1
Nov 11 at 10:34
@filipanton1 - no you don't. Theptr = new int [ArraySize]
allocsates memory,delete ptr
releases the memory allocated using operatornew
. It doesn't destroy the pointerptr
itself.
– Peter
Nov 11 at 10:42
add a comment |
Im trying to understand pointers but im having trouble increasing the size of an array, what im trying to do is make an array that is one bigger than the standard array, copy over everything there, then delete the old pointer array, create a new pointer array that is the right size and move back everything from this temp array. For some reason I keep getting "Unable to read memory" and I have no clue why.
#include <iostream>
int main()
int number;
int arraySize = 3;
bool full;
int *ptr = new int[arraySize] 0;
do
full = false;
std::cout << "Input a number please: ";
std::cin >> number;
getchar();
for (int i = 0; i < arraySize; i++)
if (ptr[i] == 0)
ptr[i] = number;
i = arraySize;
else if(arraySize -1 == i)
full = true;
if (full == true)
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; x++ )
tempPtr[x] = ptr[x];
delete ptr;
arraySize++;
int *ptr = new int[arraySize];
for (int x = 0; x < arraySize; x++)
ptr[x] = tempPtr[x];
ptr[arraySize] = number;
while(number != -1);
for (int z = 0; z < arraySize; z++)
std::cout << ptr[z] << std::endl;
getchar();
return 0;
c++ arrays pointers
Im trying to understand pointers but im having trouble increasing the size of an array, what im trying to do is make an array that is one bigger than the standard array, copy over everything there, then delete the old pointer array, create a new pointer array that is the right size and move back everything from this temp array. For some reason I keep getting "Unable to read memory" and I have no clue why.
#include <iostream>
int main()
int number;
int arraySize = 3;
bool full;
int *ptr = new int[arraySize] 0;
do
full = false;
std::cout << "Input a number please: ";
std::cin >> number;
getchar();
for (int i = 0; i < arraySize; i++)
if (ptr[i] == 0)
ptr[i] = number;
i = arraySize;
else if(arraySize -1 == i)
full = true;
if (full == true)
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; x++ )
tempPtr[x] = ptr[x];
delete ptr;
arraySize++;
int *ptr = new int[arraySize];
for (int x = 0; x < arraySize; x++)
ptr[x] = tempPtr[x];
ptr[arraySize] = number;
while(number != -1);
for (int z = 0; z < arraySize; z++)
std::cout << ptr[z] << std::endl;
getchar();
return 0;
c++ arrays pointers
c++ arrays pointers
asked Nov 11 at 10:16
filipanton1
237
237
What is a debugger and how can it help me diagnose problems?
– Jesper Juhl
Nov 11 at 10:30
How to debug small programs
– Jesper Juhl
Nov 11 at 10:31
Every time you are doingint *ptr = new int[ArraySize]
the result is creating a different variable with the same name, which disappears as soon as it passes out of scope. Only do that once. After the first time, only assign toptr
rather than defining a new variable with that name i.e.ptr = new int[ArraySize]
.
– Peter
Nov 11 at 10:31
@Peter but I have to create one after I have deleted it?
– filipanton1
Nov 11 at 10:34
@filipanton1 - no you don't. Theptr = new int [ArraySize]
allocsates memory,delete ptr
releases the memory allocated using operatornew
. It doesn't destroy the pointerptr
itself.
– Peter
Nov 11 at 10:42
add a comment |
What is a debugger and how can it help me diagnose problems?
– Jesper Juhl
Nov 11 at 10:30
How to debug small programs
– Jesper Juhl
Nov 11 at 10:31
Every time you are doingint *ptr = new int[ArraySize]
the result is creating a different variable with the same name, which disappears as soon as it passes out of scope. Only do that once. After the first time, only assign toptr
rather than defining a new variable with that name i.e.ptr = new int[ArraySize]
.
– Peter
Nov 11 at 10:31
@Peter but I have to create one after I have deleted it?
– filipanton1
Nov 11 at 10:34
@filipanton1 - no you don't. Theptr = new int [ArraySize]
allocsates memory,delete ptr
releases the memory allocated using operatornew
. It doesn't destroy the pointerptr
itself.
– Peter
Nov 11 at 10:42
What is a debugger and how can it help me diagnose problems?
– Jesper Juhl
Nov 11 at 10:30
What is a debugger and how can it help me diagnose problems?
– Jesper Juhl
Nov 11 at 10:30
How to debug small programs
– Jesper Juhl
Nov 11 at 10:31
How to debug small programs
– Jesper Juhl
Nov 11 at 10:31
Every time you are doing
int *ptr = new int[ArraySize]
the result is creating a different variable with the same name, which disappears as soon as it passes out of scope. Only do that once. After the first time, only assign to ptr
rather than defining a new variable with that name i.e. ptr = new int[ArraySize]
.– Peter
Nov 11 at 10:31
Every time you are doing
int *ptr = new int[ArraySize]
the result is creating a different variable with the same name, which disappears as soon as it passes out of scope. Only do that once. After the first time, only assign to ptr
rather than defining a new variable with that name i.e. ptr = new int[ArraySize]
.– Peter
Nov 11 at 10:31
@Peter but I have to create one after I have deleted it?
– filipanton1
Nov 11 at 10:34
@Peter but I have to create one after I have deleted it?
– filipanton1
Nov 11 at 10:34
@filipanton1 - no you don't. The
ptr = new int [ArraySize]
allocsates memory, delete ptr
releases the memory allocated using operator new
. It doesn't destroy the pointer ptr
itself.– Peter
Nov 11 at 10:42
@filipanton1 - no you don't. The
ptr = new int [ArraySize]
allocsates memory, delete ptr
releases the memory allocated using operator new
. It doesn't destroy the pointer ptr
itself.– Peter
Nov 11 at 10:42
add a comment |
1 Answer
1
active
oldest
votes
int *ptr = new int[arraySize];
This will not be the same ptr
as the outer ptr
so it will leak when it goes out of scope. It should be
ptr = new int[arraySize];
Also, don't forget to delete ptr
before you exit the program. Here's a slightly modified version that is easier to read i.m.o.
#include <iostream>
int main()
int number;
int arraySize = 3;
int *ptr = new int[arraySize] 0;
int i=0;
do
std::cout << "Input a number please: ";
std::cin >> number;
std::cin.ignore();
if(number==-1) break; // no need to increase array
if(i>=arraySize)
// the array is full
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; ++x) tempPtr[x] = ptr[x];
delete ptr;
// just assign the address tempPtr is pointing at to ptr
ptr = tempPtr;
++arraySize;
// store the new number
ptr[i] = number;
++i;
while(true);
for (int z = 0; z < i; z++)
std::cout << ptr[z] << std::endl;
delete ptr;
getchar();
return 0;
Note that there are standard containers (std::vector etc) dealing with dynamic allocation more efficiently than this so you don't have to write it yourself.
Hmm I did that now but I still seem to have problem there, when I have entered 5 numbers it says "HEAP CORRUPTION DETECTED", do you know what that could mean?
– filipanton1
Nov 11 at 10:52
No idea... I didn't get that. I'll put my edited code in the answer.
– Ted Lyngmo
Nov 11 at 11:03
Generelly it's not necessary to delete memory before exiting a program. In the words of Raymond Chen: "The building is being demolished. Don't bother sweeping the floor and emptying the trash cans and erasing the whiteboards." (blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683)
– Geier
Nov 11 at 12:15
@Geier true, but in the context of trying to understand pointers as OP says, it's probably wise to have one delete for every new.
– Ted Lyngmo
Nov 11 at 12:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247734%2funable-to-read-memory-when-trying-to-increase-size-of-an-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
int *ptr = new int[arraySize];
This will not be the same ptr
as the outer ptr
so it will leak when it goes out of scope. It should be
ptr = new int[arraySize];
Also, don't forget to delete ptr
before you exit the program. Here's a slightly modified version that is easier to read i.m.o.
#include <iostream>
int main()
int number;
int arraySize = 3;
int *ptr = new int[arraySize] 0;
int i=0;
do
std::cout << "Input a number please: ";
std::cin >> number;
std::cin.ignore();
if(number==-1) break; // no need to increase array
if(i>=arraySize)
// the array is full
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; ++x) tempPtr[x] = ptr[x];
delete ptr;
// just assign the address tempPtr is pointing at to ptr
ptr = tempPtr;
++arraySize;
// store the new number
ptr[i] = number;
++i;
while(true);
for (int z = 0; z < i; z++)
std::cout << ptr[z] << std::endl;
delete ptr;
getchar();
return 0;
Note that there are standard containers (std::vector etc) dealing with dynamic allocation more efficiently than this so you don't have to write it yourself.
Hmm I did that now but I still seem to have problem there, when I have entered 5 numbers it says "HEAP CORRUPTION DETECTED", do you know what that could mean?
– filipanton1
Nov 11 at 10:52
No idea... I didn't get that. I'll put my edited code in the answer.
– Ted Lyngmo
Nov 11 at 11:03
Generelly it's not necessary to delete memory before exiting a program. In the words of Raymond Chen: "The building is being demolished. Don't bother sweeping the floor and emptying the trash cans and erasing the whiteboards." (blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683)
– Geier
Nov 11 at 12:15
@Geier true, but in the context of trying to understand pointers as OP says, it's probably wise to have one delete for every new.
– Ted Lyngmo
Nov 11 at 12:23
add a comment |
int *ptr = new int[arraySize];
This will not be the same ptr
as the outer ptr
so it will leak when it goes out of scope. It should be
ptr = new int[arraySize];
Also, don't forget to delete ptr
before you exit the program. Here's a slightly modified version that is easier to read i.m.o.
#include <iostream>
int main()
int number;
int arraySize = 3;
int *ptr = new int[arraySize] 0;
int i=0;
do
std::cout << "Input a number please: ";
std::cin >> number;
std::cin.ignore();
if(number==-1) break; // no need to increase array
if(i>=arraySize)
// the array is full
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; ++x) tempPtr[x] = ptr[x];
delete ptr;
// just assign the address tempPtr is pointing at to ptr
ptr = tempPtr;
++arraySize;
// store the new number
ptr[i] = number;
++i;
while(true);
for (int z = 0; z < i; z++)
std::cout << ptr[z] << std::endl;
delete ptr;
getchar();
return 0;
Note that there are standard containers (std::vector etc) dealing with dynamic allocation more efficiently than this so you don't have to write it yourself.
Hmm I did that now but I still seem to have problem there, when I have entered 5 numbers it says "HEAP CORRUPTION DETECTED", do you know what that could mean?
– filipanton1
Nov 11 at 10:52
No idea... I didn't get that. I'll put my edited code in the answer.
– Ted Lyngmo
Nov 11 at 11:03
Generelly it's not necessary to delete memory before exiting a program. In the words of Raymond Chen: "The building is being demolished. Don't bother sweeping the floor and emptying the trash cans and erasing the whiteboards." (blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683)
– Geier
Nov 11 at 12:15
@Geier true, but in the context of trying to understand pointers as OP says, it's probably wise to have one delete for every new.
– Ted Lyngmo
Nov 11 at 12:23
add a comment |
int *ptr = new int[arraySize];
This will not be the same ptr
as the outer ptr
so it will leak when it goes out of scope. It should be
ptr = new int[arraySize];
Also, don't forget to delete ptr
before you exit the program. Here's a slightly modified version that is easier to read i.m.o.
#include <iostream>
int main()
int number;
int arraySize = 3;
int *ptr = new int[arraySize] 0;
int i=0;
do
std::cout << "Input a number please: ";
std::cin >> number;
std::cin.ignore();
if(number==-1) break; // no need to increase array
if(i>=arraySize)
// the array is full
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; ++x) tempPtr[x] = ptr[x];
delete ptr;
// just assign the address tempPtr is pointing at to ptr
ptr = tempPtr;
++arraySize;
// store the new number
ptr[i] = number;
++i;
while(true);
for (int z = 0; z < i; z++)
std::cout << ptr[z] << std::endl;
delete ptr;
getchar();
return 0;
Note that there are standard containers (std::vector etc) dealing with dynamic allocation more efficiently than this so you don't have to write it yourself.
int *ptr = new int[arraySize];
This will not be the same ptr
as the outer ptr
so it will leak when it goes out of scope. It should be
ptr = new int[arraySize];
Also, don't forget to delete ptr
before you exit the program. Here's a slightly modified version that is easier to read i.m.o.
#include <iostream>
int main()
int number;
int arraySize = 3;
int *ptr = new int[arraySize] 0;
int i=0;
do
std::cout << "Input a number please: ";
std::cin >> number;
std::cin.ignore();
if(number==-1) break; // no need to increase array
if(i>=arraySize)
// the array is full
int *tempPtr = new int[arraySize+1];
for (int x = 0; x < arraySize; ++x) tempPtr[x] = ptr[x];
delete ptr;
// just assign the address tempPtr is pointing at to ptr
ptr = tempPtr;
++arraySize;
// store the new number
ptr[i] = number;
++i;
while(true);
for (int z = 0; z < i; z++)
std::cout << ptr[z] << std::endl;
delete ptr;
getchar();
return 0;
Note that there are standard containers (std::vector etc) dealing with dynamic allocation more efficiently than this so you don't have to write it yourself.
edited Nov 11 at 12:15
answered Nov 11 at 10:36
Ted Lyngmo
1,927315
1,927315
Hmm I did that now but I still seem to have problem there, when I have entered 5 numbers it says "HEAP CORRUPTION DETECTED", do you know what that could mean?
– filipanton1
Nov 11 at 10:52
No idea... I didn't get that. I'll put my edited code in the answer.
– Ted Lyngmo
Nov 11 at 11:03
Generelly it's not necessary to delete memory before exiting a program. In the words of Raymond Chen: "The building is being demolished. Don't bother sweeping the floor and emptying the trash cans and erasing the whiteboards." (blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683)
– Geier
Nov 11 at 12:15
@Geier true, but in the context of trying to understand pointers as OP says, it's probably wise to have one delete for every new.
– Ted Lyngmo
Nov 11 at 12:23
add a comment |
Hmm I did that now but I still seem to have problem there, when I have entered 5 numbers it says "HEAP CORRUPTION DETECTED", do you know what that could mean?
– filipanton1
Nov 11 at 10:52
No idea... I didn't get that. I'll put my edited code in the answer.
– Ted Lyngmo
Nov 11 at 11:03
Generelly it's not necessary to delete memory before exiting a program. In the words of Raymond Chen: "The building is being demolished. Don't bother sweeping the floor and emptying the trash cans and erasing the whiteboards." (blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683)
– Geier
Nov 11 at 12:15
@Geier true, but in the context of trying to understand pointers as OP says, it's probably wise to have one delete for every new.
– Ted Lyngmo
Nov 11 at 12:23
Hmm I did that now but I still seem to have problem there, when I have entered 5 numbers it says "HEAP CORRUPTION DETECTED", do you know what that could mean?
– filipanton1
Nov 11 at 10:52
Hmm I did that now but I still seem to have problem there, when I have entered 5 numbers it says "HEAP CORRUPTION DETECTED", do you know what that could mean?
– filipanton1
Nov 11 at 10:52
No idea... I didn't get that. I'll put my edited code in the answer.
– Ted Lyngmo
Nov 11 at 11:03
No idea... I didn't get that. I'll put my edited code in the answer.
– Ted Lyngmo
Nov 11 at 11:03
Generelly it's not necessary to delete memory before exiting a program. In the words of Raymond Chen: "The building is being demolished. Don't bother sweeping the floor and emptying the trash cans and erasing the whiteboards." (blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683)
– Geier
Nov 11 at 12:15
Generelly it's not necessary to delete memory before exiting a program. In the words of Raymond Chen: "The building is being demolished. Don't bother sweeping the floor and emptying the trash cans and erasing the whiteboards." (blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683)
– Geier
Nov 11 at 12:15
@Geier true, but in the context of trying to understand pointers as OP says, it's probably wise to have one delete for every new.
– Ted Lyngmo
Nov 11 at 12:23
@Geier true, but in the context of trying to understand pointers as OP says, it's probably wise to have one delete for every new.
– Ted Lyngmo
Nov 11 at 12:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247734%2funable-to-read-memory-when-trying-to-increase-size-of-an-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is a debugger and how can it help me diagnose problems?
– Jesper Juhl
Nov 11 at 10:30
How to debug small programs
– Jesper Juhl
Nov 11 at 10:31
Every time you are doing
int *ptr = new int[ArraySize]
the result is creating a different variable with the same name, which disappears as soon as it passes out of scope. Only do that once. After the first time, only assign toptr
rather than defining a new variable with that name i.e.ptr = new int[ArraySize]
.– Peter
Nov 11 at 10:31
@Peter but I have to create one after I have deleted it?
– filipanton1
Nov 11 at 10:34
@filipanton1 - no you don't. The
ptr = new int [ArraySize]
allocsates memory,delete ptr
releases the memory allocated using operatornew
. It doesn't destroy the pointerptr
itself.– Peter
Nov 11 at 10:42