Thread: realloc() ????????

  1. #1
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68

    realloc() ????????

    hi all,

    Can realloc increase ( or decrease) the size of a dynamically allocated array ONLY ?. ( Because I tired with a normal array (one which is defined at compile time) and it won't compile).
    If it is true, why can't it change the size of a array that is not dynamically allocated ?

  2. #2
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Can realloc increase ( or decrease) the size of a dynamically allocated array ONLY ?.
    yes.
    If it is true, why can't it change the size of a array that is not dynamically allocated ?
    Arrays are a CONSTANT size that is detemined when you declare the array. You can change the contents but you can't change the size.
    Someone else might be able to give you a better explanation but I hope this helps.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> why can't it change the size of a array that is not dynamically allocated ?
    realloc() only deals with memory allocated on the heap (a particular location in computer memory). Statically sized arrays are generated on the stack, with the size being known and fixed at compile time.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    I thought the static array will be created in the data segment of the program. What if you have an array of 10000 or more elements. Wouldn't this cause frequent stack overflows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM