Thread: pointers better than non-pointers?

  1. #1
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161

    pointers better than non-pointers?

    will this clear the memory when it terminates:

    Code:
    // type 1
    int main(void)
    {
    char x = 'x';
    return 0;
    }
    compared to this? :

    Code:
    / type 2
    int main(void)
    {
    char *x = new char[1];
    x = 'x';
    delete [] x;
    which one is more memory efficient? for type 1, will the memory be automatically cleared after execution or will type 2 do a better job or are they both the same? will type 2 cost less memory?
    think only with code.
    write only with source.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    23
    type 2 is actually better.. Type 1 holds all that memory until the end of the program...type 2 can actually free up that memory on that spot.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    This kind of question is hard to answer because it depends on what you are creating.

    The first type is good, coz you dont have to worry about memory management. Also, the memory that stores the char variable is stored on the stack, so yes, in this case the char will remain there til the end of the program because its' in the main function. But that's not going to be the case for every function....

    the second type actually uses more memory than the first... because you have a pointer (which is 32 bits on PCs these days), which points to a char (which is usually 8 bits), so there are a total of 40 bits. or 5 bytes. the first is just a char being stored on the stack, which is just the one byte...

    Also, another thing to keep in mind is that even though you may only declare a single char, which gets put on the stack when the function is called (as in type 1), 2 or 4 bytes may actually be allocated to keep the stack WORD or DWORD aligned. but that's a whole different story.

    but ultimately it's harder to say which is better, it really depends on the circumstance.

    hope that helps.
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    >Type 1 holds all that memory until the end of the program

    well, to be specific, until the end of the datas scope at least...
    hasafraggin shizigishin oppashigger...

  5. #5
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    Two simple things you want to think about...
    SIZE OVER SPEED or SPEED OVER SIZE..

    well type 1 is faster, yet the memory management is not as efficient.

    Type2 is slower, yet memory management is more efficient.

    if you want to read up on a good memory management and when to use Dynamic over Static goto the MSDN code website and search through there, i read one couple months ago which was 25 pages and i thought it was good.
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM