Thread: realloc

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    realloc

    How can we allocate more memory for an array?
    I have a ar[10] and I want to grow it to ar[100] in runtime.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You can't do this with an array, you need to use a dynamically allocated pointer using malloc and realloc. Arrays can never change in size.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    As I guessed, tnx.
    But I thought it maybe possible, because arrays are pointers in theory.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But I thought it maybe possible, because arrays are pointers in theory.
    No, they're not.
    http://c-faq.com/aryptr/index.html

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I read the faq and used a pointer instead of an array it worked fine. But I read something there that really confused me:

    Quote Originally Posted by FAQ
    It is useful to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location ``p'', fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In other words, a[3] is three places past (the start of) the object named a, while p[3] is three places past the object pointed to by p. In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently. (The essential difference is that the values of an array like a and a pointer like p are computed differently whenever they appear in expressions, whether or not they are being subscripted, as explained further in question 6.3.) See also question 1.32.
    If so why this code prints out "y b", as I expected ??
    Code:
    int main()
    {
    	char* cp="xyz";
    	char ca[]="abcd";
    	 printf( "%s, %s\n", cp+1, ca+1 ); ;
    
    	return 0;
    }
    Last edited by siavoshkc; 01-28-2006 at 04:55 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    FYI, this is the C board, not the C++ board.

    Is
    cout<< *(cp+1)<<" "<< ca[1] <<endl;
    equally confusing?

    Read the FAQ again, especially the bit about equivalence. Note in particular that "equivalence" does NOT mean "the same as". Once you get past "pointer arithmetic vs. subscripts", the differences become apparent.

    Here's something else
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
      char *cp  ="this is a long string";
      char  ca[]="this is a long string";
    
      printf( "%s, %s\n", cp, ca );                 /* array decays to pointer-to-first element */
      printf( "%s, %s\n", cp+10, ca+10 );           /* either can use arithmetic */
      printf( "%s, %s\n", &cp[10], &ca[10] );       /* either can use subscripts */
      printf( "%u %u\n", sizeof(ca), strlen(ca) );  /* sizeof does NOT decay array to a pointer */
      printf( "%u %u\n", sizeof(cp), strlen(cp) );  /* sizeof on a pointer is different */
    
      return 0;
    }

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I thought the only difference is that compiler knows that array points to a static size block of memory but ptr can points to anywhere.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Arrays are arrays, they don't point somewhere else.

    char ch; // a single ch
    char ch[2]; // two chars, in successive memory locations

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    >> it emits code to start at the location ``a'', move three past it <<from FAQ
    Three past of it? Why? When we can notice?
    Why there should be difference between ptr and array?
    Code:
    char arr[]="abcd";
    Array is a sequence of bytes, the first one is addressed in arr, but compiler keeps in mind that this pointer points to an array so if you put a bigger number in subscription it will go to a illegal place (ex. arr[7]).
    Look at this code
    Code:
    int main()
    {
    	char ca[]="abcd";
    	char* cp=ca;
    	
    	printf( "%s, %s\n", cp+1, ca+1 );           
    
    	return 0;
    }
    OUTPUT IS THE SAME!!!
    It means ca acts exactly like a pointer, so what is the difference?
    Last edited by siavoshkc; 01-28-2006 at 09:15 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Apparently, you didn't run my code to discover the difference in the value of sizeof()

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by siavoshkc
    When we can notice?
    When the output generated from the code is examined. Some pseudo-assembly might be something like this.
    Code:
       load value,[array+3]
    Code:
       load a,pointer
       load value,[a+3]
    Quote Originally Posted by siavoshkc
    Why there should be difference between ptr and array?
    Because an array is an array and a pointer is a pointer. Why should there be a difference between a car and a truck if they go can get you to the same place? Because they do different things.

    Try to keep reading that FAQ until it makes sense.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by Salem
    Apparently, you didn't run my code to discover the difference in the value of sizeof()
    I knew they differ. But I think the difference is because compiler knows where the ca points to(an array) and looks at the definition of the array then returns a value for size.
    Last edited by siavoshkc; 01-28-2006 at 10:41 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Is this right:
    array is not a pointer. when we write
    Code:
    printf( "%s, %s\n", cp+1, ca+1 );
    ca actaully is the first element of the array(not a pointer to it). But cp returns the address of the element it points to. I mean there is no other place in memory to keep the address of an array. For example if we use an array[3] of char, we will have |____|____|____| ==3 bytes.
    But if we use pointer we will have |____|____|____|____| ==32bit address for the pointer
    and |____|____|____| ==3 bytes.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    By jove, I think he's got it!

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    What about two dimentional arrays?
    Code:
    int ar[x][z];
    ar[] is pointer, ar[][] is value?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

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