LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2010, 10:45 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 138

Rep: Reputation: 2
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.
 
Old 09-19-2010, 11:30 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by rahulvishwakarma View Post
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.
 
1 members found this post helpful.
Old 09-19-2010, 10:07 PM   #3
VincentCheng34
LQ Newbie
 
Registered: Aug 2010
Posts: 1

Rep: Reputation: 1
Quote:
Originally Posted by rahulvishwakarma View Post
how to solve pointer problem?
pch = "rahul";
Try memcpy(pch, "rahul", 5) instead of pch = "rahual".
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
solve my problem kamalkirat1 Linux - Software 2 12-03-2008 07:47 AM
trying to solve a problem... kiasta Linux - General 5 06-28-2006 06:36 PM
help..how i can solve this problem?// TAAN Linux - General 2 02-16-2005 07:53 AM
I don't know how to solve a problem in C nostrum Programming 5 07-20-2004 07:57 AM
No one can solve my problem Sundance Linux - Newbie 5 12-08-2003 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration