473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help , why not return a true array?

The output is:
1234
After getline: 1234
After renew: 1234
After retnp: İİİİİİİİİİİİİİİİG
After getp: İİİİİİİİİİİİİİİİG
-----What happen after renew/retnp call?-------
Why not return a true array?
-----The code is as followed-------------------
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

void print(char *,const char *);
void renew(char *);
void retnp(char *);
char* getp(char *);
int main(int argc, char* argv[])
{
char *p = new char[5];
cin.getline(p,5);
print(p,"getline");
renew(p);
print(p,"renew");
retnp(p);
print(p,"retnp");
char * p1;
p1 = getp(p);
print(p1,"getp");
return 0;
}
//print
void print(char *p,const char *st)
{
cout<<"After "<<st<<": "<<p<<endl;
}
//renew
void renew(char *p)
{
char *p1 = new char[strlen(p)+1];
strcpy(p1,p);
delete [] p;
p = new char[10];
strcpy(p,p1);
}
//retnp
void retnp(char *p)
{
char *p1 = new char[strlen(p)+1];
strcpy(p1,p);
delete [] p;
p = p1;
p1 = NULL;
delete p1;
}
//getp
char* getp(char *p)
{
char *p1 = new char[strlen(p)+1];
strcpy(p1,p);

return p1;
}
Apr 6 '07 #1
4 2264
ajk
On Fri, 6 Apr 2007 11:21:37 +0800, "<John Wu>" <jo*****@hotmail.com>
wrote:
>//renew
void renew(char *p)
{
char *p1 = new char[strlen(p)+1];
strcpy(p1,p);
delete [] p;
p = new char[10];
strcpy(p,p1);
}
in this function you are deleting p i.e. the memory that p is pointing
to. then you set p to point to another memory block however this is
not returned from the function, p is still pointing to the orignal -
now - deleted memory block.

you need to pass ptr to ptr of p if you want to change where it points
to:

void renew( char **p )
{
...

*p = new char[10];
strcpy( *p, p1 );
}

Apr 6 '07 #2
<John Wuwrote:
The output is:
1234
After getline: 1234
After renew: 1234
After retnp: İİİİİİİİİİİİİİİİG
After getp: İİİİİİİİİİİİİİİİG
-----What happen after renew/retnp call?-------
Why not return a true array?
I'm not sure what you are trying to do, but your renew and retnp both
delete the pointer passed in and then do stuff with the *local* copy of
the pointer.

So the net effect is to delete p several times and in the case of renew,
leek ten bytes. p inside the functions is a *copy* of main's p passed
as a function parameter. If you want to change main's p, you have to
pass its address. For this, your function signatures would be

void renew(char*&);
void retnp(char*&);

--
Ian Collins.
Apr 6 '07 #3
"<John Wu>" <jo*****@hotmail.comwrote in message
news:ev**********@news.cn99.com...
The output is:
1234
After getline: 1234
After renew: 1234
After retnp: İİİİİİİİİİİİİİİİG
After getp: İİİİİİİİİİİİİİİİG
-----What happen after renew/retnp call?-------
Why not return a true array?
-----The code is as followed-------------------
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

void print(char *,const char *);
void renew(char *);
void retnp(char *);
char* getp(char *);
int main(int argc, char* argv[])
{
char *p = new char[5];
cin.getline(p,5);
print(p,"getline");
renew(p);
print(p,"renew");
retnp(p);
For explanation what is wrong with retnp see the comments in the function
down below. At this point p points to invalid memory (deleted memory, see
comments in retnp down below) which is why getp is failling.
print(p,"retnp");
char * p1;
p1 = getp(p);
print(p1,"getp");
return 0;
}
//print
void print(char *p,const char *st)
{
cout<<"After "<<st<<": "<<p<<endl;
}
//renew
void renew(char *p)
{
char *p1 = new char[strlen(p)+1];
strcpy(p1,p);
delete [] p;
p = new char[10];
strcpy(p,p1);
}
//retnp
void retnp(char *p)
p is a local char* that is passed to the function by value.
{
char *p1 = new char[strlen(p)+1];
You point p1 to some memory returned by new.
strcpy(p1,p);
You copy from the memory pointed in into your memory allocated with new.
delete [] p;
You delete the memory that p was pointing to. This is also the same memory
location that was passed in, so effects the memory that the passed in
variable contains (p in mainline).
p = p1;
You assign the *local* p the address of p1
p1 = NULL;
You assign toe p1 a NULL pointer
delete p1;
This has no effect on a null pointer.
}
You return, the local variable p is now destroyed. The memory allocated by
new is lost, you no longer have a ponter to it.

If you want to change the actual passed in pointer itself, rather than a
copy of the pointer, then you need to pass either a pointer to the pointer,
or a reference the pointer. A reference makes more sense.

Change the function to
void retnp( char*& p )

and it should work as you expect, since it is a reference, changing this
reference to p is the same as changing the original p.
//getp
char* getp(char *p)
{
char *p1 = new char[strlen(p)+1];
strcpy(p1,p);

return p1;
}
Same with this one.
Apr 6 '07 #4
Thanks to ajkÏÈÉú¡¢Ian CollinsÏÈÉú¡¢Jim LangstonÏÈÉú£¬thank you very much.
Jim LangstonÏÈÉú explain in detail the way pointer p and *local* p work ,and
suggest me to pass either a pointer to the pointer,
or a reference the pointer will work fine.
I finally try a way to pass the address of the pointer p.It goes well.
Thank u£¡
Apr 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: wrrn | last post by:
Hi - I'm beginning work on an existing web site which was created with Adobe Golive. The page in question has CSACTIONS which is part of Golive's CyberStudio. Anyway, the code below has this...
4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
2
by: Martin Ho | last post by:
Hi Everyone, I have this code of Mersenne twister, which produces the random numbers, one of the fastest codes as far as I know to produce random numbers. Anyways, it's writen in c# and I need to...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
7
by: gubbachchi | last post by:
Hi all, In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.