Re: please help (novice)
From: Dave Townsend (datownsend_at_comcast.net)
Date: 11/20/04
- Next message: Chaos Master: "Re: Help needed : Remote Shutting and Starting Windows 98SE"
- Previous message: Jonathan Turkanis: "Re: Unique Integer Types -- a useful C++ trick"
- In reply to: Alf P. Steinbach: "Re: please help (novice)"
- Next in thread: Alf P. Steinbach: "Re: please help (novice)"
- Reply: Alf P. Steinbach: "Re: please help (novice)"
- Reply: ranger: "Re: please help (novice)"
- Reply: ranger: "Re: please help (novice)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 20 Nov 2004 13:01:20 -0800
The reason your code fails is the following line....
> if (pt = NULL){
In C and C++ you should use "==" not "=" to compare two
items, what you have done is assign NULL to pt and loose the
value of the address of the memory you alloaced.
dave
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:419f9cc5.1626738406@news.individual.net...
> * ranger:
> > Hi,
> >
> > I'm a beginner with C++, studying on my own from a book and I've
> > encoutered quite some problems.. If possible, someone out there might
> > help me out..
> >
> > The problem is the following.. I've tried to create a couple of string
> > class/functions.. and they do compile.. but when I run them.. windows
> > suddenly reports an error.. As I'm just a beginner in C++, with noone
> > around me to help.. I'd appreciate if someone might help me out!
> >
> > The simple (reduced) code is the following:
> >
> >
> > #include <iostream.h>
>
> #include <iostream>
>
> <iostream.h> is not a standard C++ header.
>
>
>
> > #include <stdio.h>
> > #include <stdlib.h> // for EXIT_SUCCESS
> > #include <string.h>
>
> #include <cstdio>
> #include <cstdlib>
> #include <cstring>
>
> is the preferred style for these three headers.
>
>
>
> > class string{
> > public:
> > string(void){characters = 0; pt = NULL;}
>
> 'void' as argument list a C'ism; don't use it.
>
> Use an initializer list instead of assignment,
>
> string(): characters(0), pt(NULL) {}
>
>
> > string(char *s);
>
> Argument should be const:
>
> string( char const* s );
>
> You also need a destructor to deallocate the string.
>
> And you need an assignment operator to copy the string, and
> a copy constructor.
>
>
> > private:
> > int characters;
> > char *pt;
> > };
> >
> > string::string(char *s){
>
> string::string( char const* s )
>
> > characters = strlen(s);
> > pt = new char[characters];
> > if (pt = NULL){
>
> In standard C++ this test will always fail. Operator 'new' reports
> failure by means of std::bad_alloc exception, not by returning NULL,
> so the test would always fail even if it correctly reflected what
> you intended to write. But it doesn't even that: it's an assignment,
> not a comparision.
>
>
> > cerr << "Failed to allocate string.\n";
>
> Here you need to qualify using the 'std' namespace:
>
> std::cerr << "Failed to allocate string.\n";
>
> But it's not a good idea to do i/o to report failure to the user;
> instead use exceptions to report the failure up to the caller.
>
>
> > exit(EXIT_FAILURE);
> > }
> > memcpy(pt, s, characters);
>
> This is OK if, as seems likely, you don't intend to have a terminating
> zero byte in your string representation. However, this is where you
> get a crash. That's due to the earlier error noted above.
>
> > }
> >
> > void main()
>
> 'main' must have return type 'int'.
>
> > {
> > string s1("My first string");
> > }
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?
- Next message: Chaos Master: "Re: Help needed : Remote Shutting and Starting Windows 98SE"
- Previous message: Jonathan Turkanis: "Re: Unique Integer Types -- a useful C++ trick"
- In reply to: Alf P. Steinbach: "Re: please help (novice)"
- Next in thread: Alf P. Steinbach: "Re: please help (novice)"
- Reply: Alf P. Steinbach: "Re: please help (novice)"
- Reply: ranger: "Re: please help (novice)"
- Reply: ranger: "Re: please help (novice)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|