Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26
  1. #11
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    Quote Originally Posted by M4cFly
    I don't think I am overwritting memory that's not mine here! What's the point of declaring then?
    What if I initialize a variable to "lol" and the user input is "hello world". Isn't the variable now using more space than after initialization?

    When you declare the variable and initialize it right after, you are overwritting something anyway.

    What will be overwritten (garbage) is not my problem, the system decided I could use that specific memory space.
    Because you are dealing with pointers. All you did was declare a variable that points to some location in memory. You didn't actually create the area in memory that it's going to point to. So the "garbage" that you'll be writing over was never actually given to you by the system.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  2. #12
    Join Date
    Jun 2003
    Location
    Francistan
    Posts
    147
    OK I get it now (I think).
    So when using pointers it's important to allocate memory space using new and NOT by setting a value directly which would overwrite something that doesn't belong to me. Correct?

    So in the example of this thread, name should be declared like below, right?
    Code:
    char *name = new char;
    I'm dangerous Jerry, I've got the Kavorka!

    Portfolio

  3. #13
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    Almost, you need to do this:
    Code:
    char* name = new char[50];
    You want to create a new character array to store the data in, not just a single character (the 50 was just an arbitrary number that I chose).

    But yes, the pointer defaults to pointing to some area of memory. Of course, you can make it point to a different area of memory if you wanted to. You just need to remember that you may or may not actually have the "right" to write to that location. By "right" I mean that you actually own that memory.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  4. #14
    Join Date
    Jun 2003
    Location
    Francistan
    Posts
    147
    All right, cool.
    So if I want to declare an unbound array of chars I just type:
    Code:
    char *name = new char[];
    Thanks for your explanation owen.
    I'm dangerous Jerry, I've got the Kavorka!

    Portfolio

  5. #15
    Join Date
    Mar 2006
    Posts
    372
    Than what is the difference between using
    Code:
    char *str
    str = new char[50]
    and using char str[50] ?

    Since you are allocating a fixed length anyway.

  6. #16
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    Quote Originally Posted by Adryan
    Than what is the difference between using
    Code:
    char *str
    str = new char[50]
    and using char str[50] ?

    Since you are allocating a fixed length anyway.
    Well, one difference is that:
    Code:
    char *name = new char[50];
    This creates the memory on the heap, which means that you need to clean up the memory when you're done with it with:
    Code:
    delete [] name;
    .

    When you do this:
    Code:
    char[50] name;
    You are creating memory on the stack which is automatically freed for you.

    That's one of the major differences. Another is that you can dynamically create the size of the array that you want.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  7. #17
    Join Date
    Mar 2006
    Posts
    372
    Well, yes, I know. But you still create 50 chars, either on the heap or on the stack. The way I see it, the main advantage would be this :

    Another is that you can dynamically create the size of the array that you want.
    So you can use cin >> length; str = new char[length] , for example.

  8. #18
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    Quote Originally Posted by Adryan
    So you can use cin >> length; str = new char[length] , for example.
    Yes, this is one of the advantages.

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

  9. #19
    Join Date
    Mar 2006
    Posts
    372
    But isn't there a way of creating a variable that will have allocated exactly the number of bytes needed to hold a specific string?

    For example, the varchar data type in mySQL, as opposed to the char data type.

  10. #20
    Join Date
    Feb 2005
    Location
    Bellevue, WA
    Posts
    3,251
    There is an STL (standard template library) string class that creates the memory needed for string without you having to manually deal with the char* or char[].

    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." ~Rich Cook

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •