Re: please help (novice)

From: Dave Townsend (datownsend_at_comcast.net)
Date: 11/20/04


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?



Relevant Pages

  • Re: please help (novice)
    ... > 'void' as argument list a C'ism; ... > 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: please help (novice)
    ... > 'void' as argument list a C'ism; ... > 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)
  • Problem mit XML Serialisierung
    ... public sealed partial class Assignment { ... XmlSerializer serializer = new XmlSerializer); ... private string david; ... private Listdatabases; ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: OpenForm vs. Form menu double click.
    ... was a string and the data type of the where variable matched the syntax... ... In my situatiation the SSN on the input is text as well as the SSN field in ... A command button to add an assignment ... Private Sub Form_Open ...
    (microsoft.public.access.formscoding)
  • Re: Java or C++?
    ... Plus there are several toolkits and existing libraries available for Python. ... Take a string object for example. ... a copy constructor or assignment operator can be implemented when using C++. ... And if you want more control you can implement the default and copy constructors, destructor, and assignment operator, and tell them to do what you want. ...
    (comp.lang.python)