473,216 Members | 1,277 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,216 software developers and data experts.

include string?

Hello.

I thought that strlen was only available after adding
#include <string>
to my c++ program, but apperently not, or the compiler is
clever enough to find it by it self, or what?
The program compiles and runs fine on my Linx box, with
g++ version 2.95.4
Second question. Wouldn't the method, Set, result in that the char* text
will point to a newly allocated char array that is one char too short?
strlen ignores the final nullchar, and then the final null char will be
omitted in the copy procedure?

Regards.
----------------------

#include <iostream>
class Problem {
private:
char* text;
public:
Problem() {text=new char[1]; text='\0';}
void prt() {cout<<text;}
void Set(char* str) {delete text; text=new char[strlen(str)];
strcpy(text,str);}
};
int main() {
Problem a;
Problem b;
a.Set("Gandalf");
a.Set("Bigger Longer Uncut text");
a.prt();
}

Jul 19 '05 #1
5 7266
"Gandalf" <ga*****@gunix.dk> wrote in message
news:nO*******************@newsb.telia.net...
Hello.

I thought that strlen was only available after adding
#include <string>
<cstring>

to my c++ program, but apperently not, or the compiler is
clever enough to find it by it self, or what?
The program compiles and runs fine on my Linx box, with
g++ version 2.95.4

You were lucky.
Second question. Wouldn't the method, Set, result in that the char* text
will point to a newly allocated char array that is one char too short?
strlen ignores the final nullchar, and then the final null char will be
omitted in the copy procedure?

Regards.
----------------------

#include <iostream>
class Problem {
private:
char* text;
public:
Problem() {text=new char[1]; text='\0';}

text=new char;

void prt() {cout<<text;}
void Set(char* str) {delete text; text=new char[strlen(str)];

text=new char[strlen(str)+1];


--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net

Jul 19 '05 #2
Ioannis Vranos wrote:
"Gandalf" <ga*****@gunix.dk> wrote in message
news:nO*******************@newsb.telia.net...

private:
char* text;
public:
Problem() {text=new char[1]; text='\0';}


text=new char;


No. Dont mix new and new[]. Leave it as it is.
void prt() {cout<<text;}
void Set(char* str) {delete text; text=new char[strlen(str)];


text=new char[strlen(str)+1];


Yes. And even better:

delete [] text; text=new char[strlen(str) + 1];
Christoph

Jul 19 '05 #3
Gandalf wrote:
Hello.

I thought that strlen was only available after adding
#include <string>
strlen() has nothing to do with <string>. strlen() is declared in
<string.h> or <cstring>. <string> is for the std::basic_string template
(from which std::string is created).
to my c++ program, but apperently not, or the compiler is
clever enough to find it by it self, or what?
If you want to use a function like strlen, you should #include the
appropriate header. Sometimes the correct header will be indirectly
#included through a different header, but you can't count on this.
The program compiles and runs fine on my Linx box, with
g++ version 2.95.4

Old version. Why are you using it?

Second question. Wouldn't the method, Set, result in that the char* text
will point to a newly allocated char array that is one char too short?
strlen ignores the final nullchar, and then the final null char will be
omitted in the copy procedure?
I'm not sure I understand that paragraph.

Regards.
----------------------

#include <iostream>
You need <string.h> or <cstring> here.
class Problem {
private:
char* text;
public:
Problem() {text=new char[1]; text='\0';}
This is not doing what you want it to do. First, you allocate a
character, then you immediately overwrite the pointer with 0, thus
leaking the allocated memory.
void prt() {cout<<text;}
It's called 'std::cout'.
void Set(char* str) {delete text; text=new char[strlen(str)];
strcpy(text,str);}
This is broken. You need another character to store the null terminator.
Your destination string is not large enough for the strcpy. The result
is undefined behavior - your program may crash, data may be corrupted,
it could behave erratically... anything could happen.

Also, you are deleting incorrectly. The memory 'text' points to was
allocated via new[], therefore it must be deallocated using delete[].
};
int main() {
Problem a;
Problem b;
Two memory leaks.
a.Set("Gandalf");
a.Set("Bigger Longer Uncut text");
Two cases of undefined behavior.
a.prt();
}


Other problems with your code:

* No destructor means you leak memory when an instance of your class
goes out of scope.

* The default copy constructor and copy assignment operator will cause a
new copy to have its 'text' pointer pointing to the exact same memory as
the original. When that happens, then one of the copies deletes the
memory 'text' pointed to, the other copy will point to memory that is no
longer valid, and will have no way of knowing that it's no longer valid.
Essentially anything you do with it at that point causes undefined behavior.

You should have avoided all these problems by using std::string instead
of C-style strings represented by char arrays.

I recommend you read the FAQ a few more times. You've obviously missed
several important points.

http://www.parashift.com/c++-faq-lite/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

"Christoph Rabel" <od**@hal9000.vc-graz.ac.at> wrote in message news:3f58f315$0$29344
public:
Problem() {text=new char[1]; text='\0';}


text=new char;


No. Dont mix new and new[]. Leave it as it is.

But the text = '\0'; is wrong. You're leaking memory
*text = '\0'; most likely what you want.

Jul 19 '05 #5
>> >>public:
>> Problem() {text=new char[1]; text='\0';}
>
> text=new char;


No. Dont mix new and new[]. Leave it as it is.

But the text = '\0'; is wrong. You're leaking memory
*text = '\0'; most likely what you want.

Oh, did I miss that too....dang.
Jul 19 '05 #6

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

Similar topics

5
by: Danny Anderson | last post by:
Hola! I am working on a program where I am including a library that came with my numerical methods textbook. The "util.h" simply includes a large number of files. I had to change the util.h...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
2
by: puzzlecracker | last post by:
after reading some of the post I found out something rather radical to my previous understanding: that when you do #include<iostream> #include<string> #include<vector> etc compiler puts...
0
by: Patrick Kearney | last post by:
Hi All, I have seen this type of question raised in various groups but no one has supplied a definitive answer. I am trying to load a dataset schema that has an xs:include. Project policy is to...
9
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
5
by: Jonathan Ng | last post by:
Hi, I was wondering if there was a way to include the white spaces in a string. Currently, I am using: scanf("%s", &input); However, this doesn't include the 'space' character or any other...
6
by: tshad | last post by:
In my User control, I tried to do this: *************************************************************************** <Script runat="server"> Public ClientName As String = "<!-- #include file =...
6
by: Jordi | last post by:
I'm having a problem which I think has to do with the circular use of inlcuding header files. The short version of my code would look a bit like this, I think: GameEngine.h: #ifndef GAME_ENGINE...
8
by: The Cool Giraffe | last post by:
One thing i do know for sure. When one creates a CPP file, one needs to include the H file. Now, having said that, i wonder if there are some general hints, requirements or standard guide lines on...
4
by: Travis | last post by:
So here's something I've always wondered. Typically if I want to use a string I simply using namespace std::string; string myString; But when do I actually need to include <string>. What...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.