array

This is a discussion on array within the C++ Programming forums, part of the General Programming Boards category; If you have an array lets say..... char doh [100]; will that take up 100 bytes of memory?? or will ...

  1. #1
    Registered User doh!'s Avatar
    Join Date
    Apr 2002
    Posts
    2

    Question array

    If you have an array lets say.....


    char doh [100];

    will that take up 100 bytes of memory??
    or will it take up just what the character's amount to.....

  2. #2
    biz
    biz is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    14
    That would take up 100 bytes of memory. For information on dynamic memory, lookup the operators new amd delete.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    23

    Re: array

    Originally posted by doh!
    If you have an array lets say.....


    char doh [100];

    will that take up 100 bytes of memory??
    or will it take up just what the character's amount to.....
    like the post below said it will take up 100bytes of memory. This is why variable reuse is a good idea especially for temporary variables such as incremental counters ("i" is often used for that) etc. Dynamic memory allocation is also good and it uses and frees up memory efficently.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Re: array

    Originally posted by prophet
    Dynamic memory allocation is also good and it uses and frees up memory efficently.
    Uhhhh... only if you make proper use of delete...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    23

    Re: Re: Re: array

    Originally posted by Magos

    Uhhhh... only if you make proper use of delete...
    wanna see something messed up that me and a friend discovered? In g++ and bcc i know this error occurs...but in VC++ it does not.

    Code:
    void main()
    {
      int *arr = new int[10];
      arr[5] = 4;
      cout << "Before: " << arr[5] << endl;
      delete[] arr;
      cout << "After: " << arr[5];
    }
    Kinda makes you think how efficent delete[] really is -=)

    [edit]
    What i mean is that both the cout statements outputs the 4. Only VC++ outputs the 4 in the first part and on the second part it outputs garbage values like one would assume. I was just wondering is this normal(for g++ NOT to return garbage value for the second cout?) I was under the impression that it should be instantaneous...
    [/edit]
    Last edited by prophet; 04-04-2002 at 08:36 AM.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Re: Re: Re: array

    Originally posted by prophet
    Kinda makes you think how efficent delete[] really is -=)
    When you call delete[], the allocated memory is freed, but that doesn't mean that the data will be erased. The 4 will stay in the memory until another variable will be allocated over that exact spot. When you print arr[5] after delete, you still referense to that spot, and since no new variable has been allocated there, the 4 will be printed...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    But since the array is deleted, how does the program know what area in memory is referred to by arr[5]? At this point, shouldn't it be an undeclared variable or something?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But since the array is deleted, how does the program know
    >what area in memory is referred to by arr[5]?
    arr[5] still refers to the same spot in memory, your program just doesn't own it anymore. While you can still access the memory address, it's not guaranteed that the value will still be the same, which is why it's a good practice to assign NULL to pointers you just freed the memory for.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    is there really a difference between using delete[] and delete?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there really a difference between using delete[] and delete?
    Yes, delete will free memory allocated as a single object and delete [] will free memory allocated as an array of objects. So if you use
    char *p = new char[20];
    and
    delete p;

    Then you have a memory leak, only the first element was freed. If you used delete [] p; then the entire array would be freed.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    I guess I'm just dense. I understand that until it's overwritten, spot FFF070D, or wherever, in memory still contains the value 4, and that if somewhere you reference that spot, you'll get 4.
    What I don't get is why arr[5] still refers to that spot, or any spot, since if arr is delete[]'d, arr[5] shouldn't exist.
    It would be similar (in my muddled thinking) to using a variable out of scope. If x is declared and assigned in an inner block, it can't be used outside of that block. When the program hits the closing brace of the inner block, x is destroyed (at least that's how I learned it).
    So shouldn't arr be destroyed as well? Or does delete[] only free memory - break the lock arr[5] has on that location, but arr[5] still exists? Thanks.
    Last edited by salvelinus; 04-04-2002 at 12:04 PM.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I guess I'm just dense.
    Hardly, it takes a bit of thought to fully understand how memory addressing work in computers.

    >So shouldn't arr be destroyed as well?
    Not quite, arr is a variable that holds an address, that address is the location that the computer picked for arr[0] when you allocated memory for it. When you free the memory for arr, the variable still exists and still points to the same location in memory. The difference is that the computer is free to take that memory and use it for something else, it's not yours anymore. Since arr still points to the same address, adding 5 to the address will take you to the address where the value of arr[5] resided.

    That value may still be there and it may not since the computer can do whatever it wants with the address. Pointers can point to any arbitrary address and view the value that is there. You don't have to own the address the point to it. This is a part of pointers that people find confusing.

    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Then you have a memory leak, only the first element was freed. If you used delete [] p; then the entire array would be freed.
    is it compiler or OS dependent?

    Code:
    long y = 50000000L;
    char* x;
    x = new char [y];
    while(x)
    {
    delete x;
    x = new char[y];
    }
    this didn't increase the swap file (win98) on my computer except when i ran multiple copies, and then only by ~50megs for each one that required the swap file (because RAM was used up).

    But:
    Code:
    long y = 5000L;
    char* x;
    x = new char [y];
    while(x)
    {
    x = new char[y];
    }
    constantly increased my swap file as expected. I had over a 2gig swap file when i finally terminated the second code.

    where was the leek in the first code? wouldn't it constantly increase by 50meg-1byte?

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Syneris
    where was the leek in the first code?
    You delete (deallocate) the first element of x, not the rest. Use delete[] to delete it all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21