Help , why not return a true array?

G

Guest

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;
}
 
A

ajk

//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 );
}
 
I

Ian Collins

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*&);
 
J

Jim Langston

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

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

Guest

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top