LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to solve pointer problem? (https://www.linuxquestions.org/questions/programming-9/how-to-solve-pointer-problem-833197/)

rahulvishwakarma 09-19-2010 10:45 AM

how to solve pointer problem?
 
how to solve pointer problem?

In the following code :-

#include<iostream>
using namespace std;

main()
{
char*pch = new char[10];

pch = "rahul";
// this statement causees memory leak error when deleting pch
delete pch;

return 0;
}

what should I do so that we can avoid aboe error
BUT I want to use only pointer and don't want to use
in built C functions or C++ in built classes such as string.

Sergei Steshenko 09-19-2010 11:30 AM

Quote:

Originally Posted by rahulvishwakarma (Post 4102554)
how to solve pointer problem?

In the following code :-

#include<iostream>
using namespace std;

main()
{
char*pch = new char[10];

pch = "rahul";
// this statement causees memory leak error when deleting pch
delete pch;

return 0;
}

what should I do so that we can avoid aboe error
BUT I want to use only pointer and don't want to use
in built C functions or C++ in built classes such as string.

I think you should use 'delete pch[];' for such (array) cases.

VincentCheng34 09-19-2010 10:07 PM

Quote:

Originally Posted by rahulvishwakarma (Post 4102554)
how to solve pointer problem?
pch = "rahul";

Try memcpy(pch, "rahul", 5) instead of pch = "rahual".


All times are GMT -5. The time now is 09:17 PM.