strncpy char string issue when deleting the array
I don't know, problem with this error. However, I think I should delete uName
before I delete the cpp
. How can I do for this?
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string.h>
using namespace std;
class cppUr
public:
void writeName(char nm);
char * readName();
private:
char uName[80];
;
void cppUr::writeName(char nm)
strncpy(uName, nm, 79);
char * cppUr::readName()
return uName;
The main is:
int main()
char name0[100];
char name1[100];
char name2[100];
char name3[100];
cppUr *cpp = new cppUr[3];
cout << "Input first name: "<<endl;
cin.getline(name0, 100);
cpp[0].writeName(name0);
cout << "Input second name: " << endl;
cin.getline(name1, 100);
cpp[1].writeName(name1);
cout << "Input third name: " << endl;
cin.getline(name2, 100);
cpp[2].writeName(name2);
cout << "Input fourth name: " << endl;
cin.getline(name3, 100);
cpp[3].writeName(name3);
for (int i = 0; i < 4; i++)
cpp[i].readName();
cout << "The "<<i<<" name " << cpp[i].readName() << endl;
delete cpp;
system("PAUSE");
return 0;
The error is:
HEAP CORRUPTION DETECTED: after Normal block(#148) at 0x0059E1E0.
CRT detected that the application wrote to memory after end of heap buffer
c++ arrays object strncpy
add a comment |
I don't know, problem with this error. However, I think I should delete uName
before I delete the cpp
. How can I do for this?
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string.h>
using namespace std;
class cppUr
public:
void writeName(char nm);
char * readName();
private:
char uName[80];
;
void cppUr::writeName(char nm)
strncpy(uName, nm, 79);
char * cppUr::readName()
return uName;
The main is:
int main()
char name0[100];
char name1[100];
char name2[100];
char name3[100];
cppUr *cpp = new cppUr[3];
cout << "Input first name: "<<endl;
cin.getline(name0, 100);
cpp[0].writeName(name0);
cout << "Input second name: " << endl;
cin.getline(name1, 100);
cpp[1].writeName(name1);
cout << "Input third name: " << endl;
cin.getline(name2, 100);
cpp[2].writeName(name2);
cout << "Input fourth name: " << endl;
cin.getline(name3, 100);
cpp[3].writeName(name3);
for (int i = 0; i < 4; i++)
cpp[i].readName();
cout << "The "<<i<<" name " << cpp[i].readName() << endl;
delete cpp;
system("PAUSE");
return 0;
The error is:
HEAP CORRUPTION DETECTED: after Normal block(#148) at 0x0059E1E0.
CRT detected that the application wrote to memory after end of heap buffer
c++ arrays object strncpy
2
You allocate 3cppUr
but then use 4.
– tkausl
Nov 13 '18 at 18:06
2
And you should not ever deleteuName
. It is statically allocated, not dynamically. There is also no reason to dynamically allocatecpp
and if you are learning C++, you should usestd::string
instead of all thechar
,char*
,strncpy
and manual dynamic memory management.
– user10605163
Nov 13 '18 at 18:11
2
This would be much simpler withstd::string
instead of C-style strings.
– Pete Becker
Nov 13 '18 at 18:19
1
Note thatstrncpy
is not a "safe" replacement forstrcpy
, for any reasonable definition of "safe". Read its documentation carefully.
– Pete Becker
Nov 13 '18 at 18:20
add a comment |
I don't know, problem with this error. However, I think I should delete uName
before I delete the cpp
. How can I do for this?
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string.h>
using namespace std;
class cppUr
public:
void writeName(char nm);
char * readName();
private:
char uName[80];
;
void cppUr::writeName(char nm)
strncpy(uName, nm, 79);
char * cppUr::readName()
return uName;
The main is:
int main()
char name0[100];
char name1[100];
char name2[100];
char name3[100];
cppUr *cpp = new cppUr[3];
cout << "Input first name: "<<endl;
cin.getline(name0, 100);
cpp[0].writeName(name0);
cout << "Input second name: " << endl;
cin.getline(name1, 100);
cpp[1].writeName(name1);
cout << "Input third name: " << endl;
cin.getline(name2, 100);
cpp[2].writeName(name2);
cout << "Input fourth name: " << endl;
cin.getline(name3, 100);
cpp[3].writeName(name3);
for (int i = 0; i < 4; i++)
cpp[i].readName();
cout << "The "<<i<<" name " << cpp[i].readName() << endl;
delete cpp;
system("PAUSE");
return 0;
The error is:
HEAP CORRUPTION DETECTED: after Normal block(#148) at 0x0059E1E0.
CRT detected that the application wrote to memory after end of heap buffer
c++ arrays object strncpy
I don't know, problem with this error. However, I think I should delete uName
before I delete the cpp
. How can I do for this?
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string.h>
using namespace std;
class cppUr
public:
void writeName(char nm);
char * readName();
private:
char uName[80];
;
void cppUr::writeName(char nm)
strncpy(uName, nm, 79);
char * cppUr::readName()
return uName;
The main is:
int main()
char name0[100];
char name1[100];
char name2[100];
char name3[100];
cppUr *cpp = new cppUr[3];
cout << "Input first name: "<<endl;
cin.getline(name0, 100);
cpp[0].writeName(name0);
cout << "Input second name: " << endl;
cin.getline(name1, 100);
cpp[1].writeName(name1);
cout << "Input third name: " << endl;
cin.getline(name2, 100);
cpp[2].writeName(name2);
cout << "Input fourth name: " << endl;
cin.getline(name3, 100);
cpp[3].writeName(name3);
for (int i = 0; i < 4; i++)
cpp[i].readName();
cout << "The "<<i<<" name " << cpp[i].readName() << endl;
delete cpp;
system("PAUSE");
return 0;
The error is:
HEAP CORRUPTION DETECTED: after Normal block(#148) at 0x0059E1E0.
CRT detected that the application wrote to memory after end of heap buffer
c++ arrays object strncpy
c++ arrays object strncpy
edited Nov 13 '18 at 20:02
Rajeev Atmakuri
6891819
6891819
asked Nov 13 '18 at 18:04
wong waimanwong waiman
81
81
2
You allocate 3cppUr
but then use 4.
– tkausl
Nov 13 '18 at 18:06
2
And you should not ever deleteuName
. It is statically allocated, not dynamically. There is also no reason to dynamically allocatecpp
and if you are learning C++, you should usestd::string
instead of all thechar
,char*
,strncpy
and manual dynamic memory management.
– user10605163
Nov 13 '18 at 18:11
2
This would be much simpler withstd::string
instead of C-style strings.
– Pete Becker
Nov 13 '18 at 18:19
1
Note thatstrncpy
is not a "safe" replacement forstrcpy
, for any reasonable definition of "safe". Read its documentation carefully.
– Pete Becker
Nov 13 '18 at 18:20
add a comment |
2
You allocate 3cppUr
but then use 4.
– tkausl
Nov 13 '18 at 18:06
2
And you should not ever deleteuName
. It is statically allocated, not dynamically. There is also no reason to dynamically allocatecpp
and if you are learning C++, you should usestd::string
instead of all thechar
,char*
,strncpy
and manual dynamic memory management.
– user10605163
Nov 13 '18 at 18:11
2
This would be much simpler withstd::string
instead of C-style strings.
– Pete Becker
Nov 13 '18 at 18:19
1
Note thatstrncpy
is not a "safe" replacement forstrcpy
, for any reasonable definition of "safe". Read its documentation carefully.
– Pete Becker
Nov 13 '18 at 18:20
2
2
You allocate 3
cppUr
but then use 4.– tkausl
Nov 13 '18 at 18:06
You allocate 3
cppUr
but then use 4.– tkausl
Nov 13 '18 at 18:06
2
2
And you should not ever delete
uName
. It is statically allocated, not dynamically. There is also no reason to dynamically allocate cpp
and if you are learning C++, you should use std::string
instead of all the char
, char*
, strncpy
and manual dynamic memory management.– user10605163
Nov 13 '18 at 18:11
And you should not ever delete
uName
. It is statically allocated, not dynamically. There is also no reason to dynamically allocate cpp
and if you are learning C++, you should use std::string
instead of all the char
, char*
, strncpy
and manual dynamic memory management.– user10605163
Nov 13 '18 at 18:11
2
2
This would be much simpler with
std::string
instead of C-style strings.– Pete Becker
Nov 13 '18 at 18:19
This would be much simpler with
std::string
instead of C-style strings.– Pete Becker
Nov 13 '18 at 18:19
1
1
Note that
strncpy
is not a "safe" replacement for strcpy
, for any reasonable definition of "safe". Read its documentation carefully.– Pete Becker
Nov 13 '18 at 18:20
Note that
strncpy
is not a "safe" replacement for strcpy
, for any reasonable definition of "safe". Read its documentation carefully.– Pete Becker
Nov 13 '18 at 18:20
add a comment |
1 Answer
1
active
oldest
votes
The error is very simple.
You create array from ccpUr with 3 elements, but after use 4 of them.
Of course the c++ is null based index language, but that means the last is not useable. That is why in your for cycle condition must be use the same number in like in array allocation which is the 3.
Think about it, when [3] is enough for 4 element then the [2] is enough for 3 element then [1] is enough for 2 element, and [0] is enough for 1 element? Do you feel it? The zero size array is not enough for nothing.
regards
But array is starting from 0? When I create array [3], it should be include [0], [1], [2],[3] and total 4 ?
– wong waiman
Nov 13 '18 at 20:08
no, it is not true. If you have right, the array[0] is still contains one element. when you define array[3] the index3 must be not used. in the for condition you have to use i < 3. It means the 3 will be not used.
– György Gulyás
Nov 14 '18 at 11:07
Hello wong. Is it clear now? If not, just ask it. tthx
– György Gulyás
Nov 16 '18 at 20:14
Thank you, I understand the logic
– wong waiman
Nov 17 '18 at 21:37
Welcome :) And please upvote my answer, thx :)
– György Gulyás
Nov 18 '18 at 13:12
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%2f53287027%2fstrncpy-char-string-issue-when-deleting-the-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
The error is very simple.
You create array from ccpUr with 3 elements, but after use 4 of them.
Of course the c++ is null based index language, but that means the last is not useable. That is why in your for cycle condition must be use the same number in like in array allocation which is the 3.
Think about it, when [3] is enough for 4 element then the [2] is enough for 3 element then [1] is enough for 2 element, and [0] is enough for 1 element? Do you feel it? The zero size array is not enough for nothing.
regards
But array is starting from 0? When I create array [3], it should be include [0], [1], [2],[3] and total 4 ?
– wong waiman
Nov 13 '18 at 20:08
no, it is not true. If you have right, the array[0] is still contains one element. when you define array[3] the index3 must be not used. in the for condition you have to use i < 3. It means the 3 will be not used.
– György Gulyás
Nov 14 '18 at 11:07
Hello wong. Is it clear now? If not, just ask it. tthx
– György Gulyás
Nov 16 '18 at 20:14
Thank you, I understand the logic
– wong waiman
Nov 17 '18 at 21:37
Welcome :) And please upvote my answer, thx :)
– György Gulyás
Nov 18 '18 at 13:12
add a comment |
The error is very simple.
You create array from ccpUr with 3 elements, but after use 4 of them.
Of course the c++ is null based index language, but that means the last is not useable. That is why in your for cycle condition must be use the same number in like in array allocation which is the 3.
Think about it, when [3] is enough for 4 element then the [2] is enough for 3 element then [1] is enough for 2 element, and [0] is enough for 1 element? Do you feel it? The zero size array is not enough for nothing.
regards
But array is starting from 0? When I create array [3], it should be include [0], [1], [2],[3] and total 4 ?
– wong waiman
Nov 13 '18 at 20:08
no, it is not true. If you have right, the array[0] is still contains one element. when you define array[3] the index3 must be not used. in the for condition you have to use i < 3. It means the 3 will be not used.
– György Gulyás
Nov 14 '18 at 11:07
Hello wong. Is it clear now? If not, just ask it. tthx
– György Gulyás
Nov 16 '18 at 20:14
Thank you, I understand the logic
– wong waiman
Nov 17 '18 at 21:37
Welcome :) And please upvote my answer, thx :)
– György Gulyás
Nov 18 '18 at 13:12
add a comment |
The error is very simple.
You create array from ccpUr with 3 elements, but after use 4 of them.
Of course the c++ is null based index language, but that means the last is not useable. That is why in your for cycle condition must be use the same number in like in array allocation which is the 3.
Think about it, when [3] is enough for 4 element then the [2] is enough for 3 element then [1] is enough for 2 element, and [0] is enough for 1 element? Do you feel it? The zero size array is not enough for nothing.
regards
The error is very simple.
You create array from ccpUr with 3 elements, but after use 4 of them.
Of course the c++ is null based index language, but that means the last is not useable. That is why in your for cycle condition must be use the same number in like in array allocation which is the 3.
Think about it, when [3] is enough for 4 element then the [2] is enough for 3 element then [1] is enough for 2 element, and [0] is enough for 1 element? Do you feel it? The zero size array is not enough for nothing.
regards
answered Nov 13 '18 at 18:20
György GulyásGyörgy Gulyás
554424
554424
But array is starting from 0? When I create array [3], it should be include [0], [1], [2],[3] and total 4 ?
– wong waiman
Nov 13 '18 at 20:08
no, it is not true. If you have right, the array[0] is still contains one element. when you define array[3] the index3 must be not used. in the for condition you have to use i < 3. It means the 3 will be not used.
– György Gulyás
Nov 14 '18 at 11:07
Hello wong. Is it clear now? If not, just ask it. tthx
– György Gulyás
Nov 16 '18 at 20:14
Thank you, I understand the logic
– wong waiman
Nov 17 '18 at 21:37
Welcome :) And please upvote my answer, thx :)
– György Gulyás
Nov 18 '18 at 13:12
add a comment |
But array is starting from 0? When I create array [3], it should be include [0], [1], [2],[3] and total 4 ?
– wong waiman
Nov 13 '18 at 20:08
no, it is not true. If you have right, the array[0] is still contains one element. when you define array[3] the index3 must be not used. in the for condition you have to use i < 3. It means the 3 will be not used.
– György Gulyás
Nov 14 '18 at 11:07
Hello wong. Is it clear now? If not, just ask it. tthx
– György Gulyás
Nov 16 '18 at 20:14
Thank you, I understand the logic
– wong waiman
Nov 17 '18 at 21:37
Welcome :) And please upvote my answer, thx :)
– György Gulyás
Nov 18 '18 at 13:12
But array is starting from 0? When I create array [3], it should be include [0], [1], [2],[3] and total 4 ?
– wong waiman
Nov 13 '18 at 20:08
But array is starting from 0? When I create array [3], it should be include [0], [1], [2],[3] and total 4 ?
– wong waiman
Nov 13 '18 at 20:08
no, it is not true. If you have right, the array[0] is still contains one element. when you define array[3] the index3 must be not used. in the for condition you have to use i < 3. It means the 3 will be not used.
– György Gulyás
Nov 14 '18 at 11:07
no, it is not true. If you have right, the array[0] is still contains one element. when you define array[3] the index3 must be not used. in the for condition you have to use i < 3. It means the 3 will be not used.
– György Gulyás
Nov 14 '18 at 11:07
Hello wong. Is it clear now? If not, just ask it. tthx
– György Gulyás
Nov 16 '18 at 20:14
Hello wong. Is it clear now? If not, just ask it. tthx
– György Gulyás
Nov 16 '18 at 20:14
Thank you, I understand the logic
– wong waiman
Nov 17 '18 at 21:37
Thank you, I understand the logic
– wong waiman
Nov 17 '18 at 21:37
Welcome :) And please upvote my answer, thx :)
– György Gulyás
Nov 18 '18 at 13:12
Welcome :) And please upvote my answer, thx :)
– György Gulyás
Nov 18 '18 at 13:12
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.
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%2f53287027%2fstrncpy-char-string-issue-when-deleting-the-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
2
You allocate 3
cppUr
but then use 4.– tkausl
Nov 13 '18 at 18:06
2
And you should not ever delete
uName
. It is statically allocated, not dynamically. There is also no reason to dynamically allocatecpp
and if you are learning C++, you should usestd::string
instead of all thechar
,char*
,strncpy
and manual dynamic memory management.– user10605163
Nov 13 '18 at 18:11
2
This would be much simpler with
std::string
instead of C-style strings.– Pete Becker
Nov 13 '18 at 18:19
1
Note that
strncpy
is not a "safe" replacement forstrcpy
, for any reasonable definition of "safe". Read its documentation carefully.– Pete Becker
Nov 13 '18 at 18:20