473,418 Members | 2,338 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,418 software developers and data experts.

char[] vs. char *

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

Jul 19 '05 #1
7 3510

"Yang Song" <so******@blue.seas.upenn.edu> wrote in message
news:bj***********@netnews.upenn.edu...
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
Jul 19 '05 #2

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

Jul 19 '05 #3
Yang Song <so******@blue.seas.upenn.edu> wrote:
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


--
Kristofer Pettijohn
kr*******@cybernetik.net
Jul 19 '05 #4

"Yang Song" <so******@blue.seas.upenn.edu> wrote in message
news:bj***********@netnews.upenn.edu...
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
Jul 19 '05 #5

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

so******@blue.seas.upenn.edu (Yang Song) writes:
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.
Jul 19 '05 #6

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bj************@ID-196037.news.uni-berlin.de...


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.
Jul 19 '05 #7

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

so******@blue.seas.upenn.edu (Yang Song) writes:
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.
Jul 19 '05 #8

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

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.