Thread: a Question regarding Dynamic memory.

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    29

    a Question regarding Dynamic memory.

    i have been working on a certain program the other day when i noticed a wierd problem: strlen doesnt work with new, so
    Code:
    char * b;
    char * c;
    b = newchar (12);
    c = newchar (12);
    cout<<strlen(c)<<" -- "<<strlen(b)<<endl;
    cout<<strlen(c)<<" -- "<<strlen(b)<<endl;
    
    outputs 3 -- 3.
    i don't want to use strings, but i would like to know if there is a way to avoid this and make strlen work as expected.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >strlen doesnt work with new
    I wouldn't expect it to. Why don't you try posting code that actually compiles, then explain what you expect to happen as opposed to what's actually happening. With that information, we'll be in a better position to tell you why your expectations are unreasonable.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    this is the full code. it started as a small test, when i realised it did'nt work. it compiles, but gives unexpected results: 3 and 1. i hope i rest my case this time/

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >this is the full code.
    Just so you know, you can't pull that kind of thing here. Many of us are C++ experts and can tell you immediately whether something will compile or not. Without even attempting to compile the code you posted, I can see the following errors:

    1) Failure to include <iostream>
    2) Failure to include <cstring>
    3) Failure to qualify for namespace std
    4) newchar is an undeclared function
    5) No entry point
    6) Non-declarative operations outside of a function body

    >it compiles
    Not for me. newchar is not a standard function or operator. Assuming you made the same typo twice and meant "new char", strlen simply won't work (ie. undefined behavior) because you're allocating memory for a single character and specifying the constructed value as 12. If you made another two typos and meant "new char[12]", it's still wrong because you never set the '\0' character that strlen looks for. Once again, undefined behavior.

    I can sympathize with not wanting to use it, but if you have no idea how to create your own dynamic C-style strings, you really are better off with the string class. At least until you have more practice with the lower level solutions.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    I see what you mean. anyway, is there's any way to define the last character or to detect the end of a dynamicly allocated array?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is there's any way to define the last character or to detect the end of a dynamicly allocated array?
    You already know the end of a dynamically allocated array because you allocated it and the allocation requires a size. To use strlen, you just copy a '\0' character to the last element:
    Code:
    #include <cstring>
    #include <iostream>
    
    int main()
    {
      char *p = new char[11];
    
      // Fill the array
      for ( int i = 0; i < 10; i++ )
        p[i] = i + '0';
    
      // Tack a null character on the end
      p[10] = '\0';
    
      // Now it all works
      std::cout<< p <<" -- "<< std::strlen ( p ) <<'\n';
    }
    But you have to be very careful to only place '\0' after valid characters or you'll end up looking at indeterminate data. In other words, this is bad:
    Code:
    #include <cstring>
    #include <iostream>
    
    int main()
    {
      char *p = new char[11];
    
      // Tack a null character on the end
      p[10] = '\0';
    
      // Ack! Undefined behavior!
      std::cout<< p <<" -- "<< std::strlen ( p ) <<'\n';
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    I understand. thanks.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char * b;
    >b = newchar (12);
    If you're trying to allocate space for a string, you would need to use brackets instead:
    Code:
    char *b;
    
    //Allocate space for 11 characters plus the string terminator
    b = new char[12];
    
    //Store a string
    strcpy(b, "hamburger");
    
    //Output the string
    cout << b << endl;
    
    //Output the string's length
    cout << strlen(b) << endl;
    
    .
    .
    //Delete the memory
    delete []b;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 01:00 PM