char[] vs. char *

Y

Yang Song

HI, I am a little confused about char * and char[].

How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a
point and a value at the same time? I thought of a possible solution. However, I am not quite sure if I understood
char[] vs. char*. Any insight is greatly appreciated.

Here is the question:
----------------------
....

char * test() {
char[] buf = "abcdef";
return (char *)buf;
}

int main() {
char * p ;
p = test(); // I know that this call is useless. So I tried to modify the test() function to return the
// char * properly. I listed my solution down below. Please feel free to advise.
return 0;
}

----------------------

char * test (char * p) {
p = new char[20];
p = "abcdef";
return p;
}

int main() {
char * p;
char * value;
value = test(p);
cout << value << "\n";
}

This solution returns "abcdef" as the return value. However, p does not pertain the same address that was allocated in
test(). My question is:

1. Is this solution going to cause memory leak because I did not call "delete" to delete the memory allocated?
2. Why was not p's value pertained?
3. How should I change so that I can return a value as well as a char *?

Thanks.

Yang
 
K

Kristofer Pettijohn

Yang Song said:
HI, I am a little confused about char * and char[].

How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a
point and a value at the same time? I thought of a possible solution. However, I am not quite sure if I understood
char[] vs. char*. Any insight is greatly appreciated.

Here is the question:
----------------------
...

char * test() {
char[] buf = "abcdef";
return (char *)buf;
}

int main() {
char * p ;
p = test(); // I know that this call is useless. So I tried to modify the test() function to return the
// char * properly. I listed my solution down below. Please feel free to advise.
return 0;
}

----------------------

char * test (char * p) {
p = new char[20];
p = "abcdef";
return p;
}

int main() {
char * p;
char * value;
value = test(p);
cout << value << "\n";
}

This solution returns "abcdef" as the return value. However, p does not pertain the same address that was allocated in
test(). My question is:

1. Is this solution going to cause memory leak because I did not call "delete" to delete the memory allocated?
2. Why was not p's value pertained?
3. How should I change so that I can return a value as well as a char *?

Thanks.

Yang
 
J

Josephine Schafer

Yang Song said:
HI, I am a little confused about char * and char[].

How would I be able to return a char* created in a function? Is new() the
only way? How would I be able to return a
point and a value at the same time? I thought of a possible solution.
However, I am not quite sure if I understood
char[] vs. char*. Any insight is greatly appreciated.

Here is the question:
----------------------
...

char * test() {
char[] buf = "abcdef";
return (char *)buf;
}

int main() {
char * p ;
p = test(); // I know that this call is useless. So I tried to modify the test() function to return the
// char * properly. I listed my solution down below. Please feel free to advise.
return 0;
}

----------------------

char * test (char * p) {
p = new char[20];
p = "abcdef";
return p;
}

int main() {
char * p;
char * value;
value = test(p);
cout << value << "\n";
}

This solution returns "abcdef" as the return value. However, p does not
pertain the same address that was allocated in
test(). My question is:

1. Is this solution going to cause memory leak because I did not call
"delete" to delete the memory allocated?

Yes.
2. Why was not p's value pertained?
Pertained ..did you mean changed??
p = new char[20]; // makes p contain a valid heap address.
p = "abcdef"; //changes p point to a string literal now.
Haven't you changed what p contained ?

3. How should I change so that I can return a value as well as a char *?

Don't know for sure what you are trying still...

#include <iostream>
using namespace std;

char * test (char * p) {
p = new char[20]; // Not a very good idea..vulnerable to buffer overrun.
strcpy ( p, "abcdef");
return p;
}

int main() {
char * p = NULL;
char * value = NULL;
value = test(p);
cout << value << "\n";
delete p;
}

HTH,
J.Schafer
 
J

Josephine Schafer

#include <iostream>
using namespace std;

char * test (char * p) {
p = new char[20]; // Not a very good idea..vulnerable to buffer overrun.
strcpy ( p, "abcdef");
return p;
}

int main() {
char * p = NULL;
char * value = NULL;
value = test(p);
cout << value << "\n";
delete p;
Above stmt should be -
delete value;

Even delete p; won't crash your program because it's valid to delete a null
pointer as is in this case.
But then it's a memory leak ;-).
 
J

John Harrison

Yang Song said:
HI, I am a little confused about char * and char[].

How would I be able to return a char* created in a function? Is new() the
only way? How would I be able to return a
point and a value at the same time? I thought of a possible solution.
However, I am not quite sure if I understood
char[] vs. char*. Any insight is greatly appreciated.

Here is the question:
----------------------
...

char * test() {
char[] buf = "abcdef";
return (char *)buf;
}

This is incorrect. buf is an array which exists only in the function test.
You are returing a pointer to something which no longer exists.

This is OK because now you are returning a pointer to a string literal, and
string literal exists for the whole of the program.

char * test() {
char* buf = "abcdef";
return buf;
}
int main() {
char * p ;
p = test(); // I know that this call is useless. So I tried to modify the test() function to return the
// char * properly. I listed my solution down below. Please feel free to advise.
return 0;
}

----------------------

char * test (char * p) {
p = new char[20];
p = "abcdef";

strcpy(p, "abcdef");

Your code makes p point at the string, you want to copy the string to your
allocated memory. strcpy does that.
return p;
}

int main() {
char * p;
char * value;
value = test(p);
cout << value << "\n";
}

This solution returns "abcdef" as the return value. However, p does not
pertain the same address that was allocated in
test(). My question is:

1. Is this solution going to cause memory leak because I did not call
"delete" to delete the memory allocated?

Yes
2. Why was not p's value pertained?

Because you changed it.
3. How should I change so that I can return a value as well as a char *?

Like this?

struct MyData
{
int my_value;
char* my_string;
};

MyData test()
{
MyData d;
d.my_string = new char[20];
strcpy(d.my_string, "abcdef");
d.my_value = 123;
return d;
}

int main()
{
MyData x = test();
cout << x.my_string << x.my_value;
delete[] x.my_string;
}

There are other ways as well.
Thanks.

Yang

If string handling seems complicated to you (and it is very complicated)
then you should find out about the C++ string class, called std::string, it
will make things much easier for you because it does the memory allocation
for you.

E.g.

#include <string>

struct MyData
{
int my_value;
std::string my_string;
};

MyData test()
{
MyData d;
d.my_string = "abcdef";
d.my_value = 123;
return d;
}

int main()
{
MyData x = test();
cout << x.my_string << x.my_value;
}

Much simpler, and much more like your original code.

john
 
L

llewelly

[note: some lines reformatted to fit within 80 columns.]

HI, I am a little confused about char * and char[].

How would I be able to return a char* created in a function? Is
new() the only way? How would I be able to return a point and a
value at the same time? I thought of a possible solution. However, I
am not quite sure if I understood char[] vs. char*. Any insight is
greatly appreciated.

Here is the question:
----------------------
...

char * test() {
char[] buf = "abcdef";
return (char *)buf;
}

int main() {
char * p ;
p = test(); // I know that this call is useless. So I tried to
// modify the test() function to return the
// char * properly. I listed my solution down
// below. Please feel free to advise.
return 0;
}

----------------------

char * test (char * p) {
p = new char[20];
p = "abcdef";
return p;
}

int main() {
char * p;
char * value;
value = test(p);
cout << value << "\n";
}

Why not:

#include<string>
#include<ostream>
#include<iostream>

using namespace std;

string test()
{
return "abcdef";
}

int main()
{
string value;
value= test()
cout << value << endl;
}

?

Others have already answered your questions about char* vs char[] . I
am telling you how to avoid those issues altogether.
 
J

jeffc

John Harrison said:
If string handling seems complicated to you (and it is very complicated)
then you should find out about the C++ string class, called std::string, it
will make things much easier for you because it does the memory allocation
for you.

That is the truth. I can't understand why so many (more experienced) people
persist with char* string handling. It's probably the cause of more
programming bugs than any other single implementation element. Yes, I'm
aware of performance issues. I'm also aware that they used to use 2 digits
for the year to save space.
 
L

llewelly

[note: some lines reformatted to fit within 80 columns.]

HI, I am a little confused about char * and char[].

How would I be able to return a char* created in a function? Is
new() the only way? How would I be able to return a point and a
value at the same time? I thought of a possible solution. However, I
am not quite sure if I understood char[] vs. char*. Any insight is
greatly appreciated.

Here is the question:
----------------------
...

char * test() {
char[] buf = "abcdef";
return (char *)buf;
}

int main() {
char * p ;
p = test(); // I know that this call is useless. So I tried to
// modify the test() function to return the
// char * properly. I listed my solution down
// below. Please feel free to advise.
return 0;
}

----------------------

char * test (char * p) {
p = new char[20];
p = "abcdef";
return p;
}

int main() {
char * p;
char * value;
value = test(p);
cout << value << "\n";
}

Why not:

#include<string>
#include<ostream>
#include<iostream>

using namespace std;

string test()
{
return "abcdef";
}

int main()
{
string value;
value= test()
cout << value << endl;
}

?

Others have already answered your questions about char* vs char[] . I
am telling you how to avoid those issues altogether.
 

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

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top