Re: please help (novice)

From: ranger (sgrine_at_msn.com)
Date: 11/21/04


Date: 21 Nov 2004 02:13:50 -0800

I really appreciate the fact you answered my question!

I'm very new in C++, I'm studying C++ wih numerical applications in my mind..

Still, thanks a lot!!

"Dave Townsend" <datownsend@comcast.net> wrote in message news:<dLudncepcJE5LQLcRVn-uw@comcast.com>...
> 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?



Relevant Pages

  • Re: please help (novice)
    ... > The reason your code fails is the following line.... ... what you have done is assign NULL to pt and loose the ... >> You also need a destructor to deallocate the string. ...
    (comp.lang.cpp)
  • Re: What is the array that contains all of $1, $2, $3 ...?
    ... SP> matching in list context to assign the results of the subexpressions ... Sherm meant to use the assignment operator rather than the binding ... That assumes that the string to be matched against is in $_. ... Sherm Pendley ...
    (comp.lang.perl.misc)
  • Re: please help (novice)
    ... The reason your code fails is the following line.... ... > Use an initializer list instead of assignment, ... > You also need a destructor to deallocate the string. ... > This is OK if, as seems likely, you don't intend to have a terminating ...
    (comp.lang.cpp)
  • Re: What is the array that contains all of $1, $2, $3 ...?
    ... SP> matching in list context to assign the results of the subexpressions ... Sherm meant to use the assignment operator rather than the binding ... That assumes that the string to be matched against is in $_. ...
    (comp.lang.perl.misc)
  • Re: operators
    ... > The /A switch specifies that the string to the right of the equal sign ... > enclose the expression string in quotes. ... > command script, then it displays the final value of the expression. ... > the assignment operator. ...
    (microsoft.public.win2000.cmdprompt.admin)