Thread: `new' operator

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    `new' operator

    Code:
    #include "cstrf.hh"
    
    int main() {
    
      const char* s = "The quick brown fox";
      const char* t = "jumped over the lazy dogs";
      char* u = new char; // This works, but I don't know why
    
      strcp(s, u);
    
      std::cout<< "Length: " << strl(s) << '\n';
      std::cout<< "Copy: (" << u << ")\n";
    }
    
    int strl(const char* s) {
    
      int l = 0; //do local variables declared in functions have default initializers?
    
      while(*s++ != 0) { l++; }
      return l;
    
    }
    
    void strcp(const char* from, char* to) {
    
      while(*to++ = *from++);
    
    }
    
    int strcmp(const char* s1, const char* s2) {
    
      // -1 if s1 < s2, 0 if s1 == s2, +1 if s1 > s2
    
      int c1;
      int c2;
    
      while(c1 += static_cast<int>(*s1++));
      while(c2 += static_cast<int>(*s2++));
    
      switch(c1 - c2) {
      case 0:
        return 0;
      default:
        if(c1 < c2) return -1;
        else return 1;
      }
    }
    header file :

    Code:
    #include <iostream>
    
    int strl(const char*);
    void strcp(const char*, char*);
    int strcmp(const char*, const char*);
    Does `new' instantiate an object and allocate memory for it? I was having trouble with things like

    char* u = 0;

    and

    char u[] = "";

    My guess is that the size of `u' is unknown in either of these cases and so the program will crash when it tries to do anything with `u' at all (it segfaulted on my end).

    Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For new: In short yes. Read http://www.cprogramming.com/tutorial/lesson6.html.
    char* u = 0 <--- no memory allocated so you can't write or read from it.
    charu [] = "" <-- Size == 1. If you try to read (or write) from/to element 1 or higher, you can get an error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The tricky thing about pointers is that they let you do horrible things without ever complaining. It may seem to work. Then you add a line of code, and suddenly it crashes.

    Thus, you can never learn pointers just by trying out stuff. You actually have to understand them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM