Thread: when resizing an array..

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    Question when resizing an array..

    why is p[20] valid and can hold values when :
    Code:
    int *p;
    p = (int *)calloc(10, sizeof(int));
    i mean its out of the allocation range
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It may be able to hold values, but it isn't valid. Accessing memory outside of your address space results in undefined behavior. That means that it could work like you expect, or it could fry your monitor. You just don't know.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    However, the tendancy towards the latter varies according to
    - the larger the program becomes (larger programs die more frequently than small programs)
    - the longer the program takes to run (long running programs die more frequently than short lived programs)
    - the amount of time left before the deadline / release date (the closer the deadline, the more likely failure will occur)

    This means that simple one-line tests of out of bound accesses to malloc'ed memory almost always succeed, which is a pity really because newbies don't get to see the real mistake until much later, when they've learnt some bad habit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >However, the tendancy towards the latter varies according to
    You forgot the most annoying one:

    - the time and place that the program is run (programs die more frequently during demos and reviews)
    My best code is written with the delete key.

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    to get this straight , i should always use allocated spaces even if unallocated spaces work right?? (just to make sure)
    Last edited by Brain Cell; 03-23-2004 at 10:12 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i should always use allocated spaces even if unallocated spaes work right??
    Yes.
    My best code is written with the delete key.

  7. #7
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Thanks for the quick help.

    Prelude : do you live on the board or something?? lol , you are everywhere..
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >do you live on the board or something??
    Sadly, it seems that way.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. resizing array pointed to by pointer
    By sys_x in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2008, 12:06 PM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM