Unable to read memory when trying to increase size of an array









up vote
0
down vote

favorite












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;











share|improve this question





















  • 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 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










  • @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














up vote
0
down vote

favorite












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;











share|improve this question





















  • 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 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










  • @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












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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;











share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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










  • @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
















  • 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 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










  • @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















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












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer






















  • 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










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',
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
);



);













draft saved

draft discarded


















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








up vote
0
down vote













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.






share|improve this answer






















  • 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














up vote
0
down vote













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.






share|improve this answer






















  • 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












up vote
0
down vote










up vote
0
down vote









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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 12:15

























answered Nov 11 at 10:36









Ted Lyngmo

1,261214




1,261214











  • 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










  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto