Thread: array pointer changes value when printed out two times after each other, why?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    30

    array pointer changes value when printed out two times after each other, why?

    The code:
    Code:
    #include <stdio.h>
    main () {
         int size = 5;
         int *pointer;
         int list[size];
         int count = 0;
         pointer = list;
    
         for (count = 0; count<25; count++ ) {
              if (count == size) {
                   int previoussize = size;
                   int counter;
                   size = size +5;
                   int newlist[size];
                   printf("\nlist extended\n\n");
                   pointer = newlist;
              }
    
              *(pointer+count) = count;
              printf("%d  :  %d\n", count, *(pointer+count));
         }
    
         printf("\n\n");
         int count2 = 0;
         for (count2 = 0; count2<25; count2++) {
              printf("%d  :  %d\n", count2, *(pointer+count2));
         }
    }
    the output:
    Code:
    0  :  0
    1  :  1
    2  :  2
    3  :  3
    4  :  4
    
    list extended
    
    5  :  5
    6  :  6
    7  :  7
    8  :  8
    9  :  9
    
    list extended
    
    10  :  10
    11  :  11
    12  :  12
    13  :  13
    14  :  14
    
    list extended
    
    15  :  15
    16  :  16
    17  :  17
    18  :  18
    19  :  19
    
    list extended
    
    20  :  20
    21  :  21
    22  :  22
    23  :  23
    24  :  24
    
    
    0  :  1997992286
    1  :  0
    2  :  1
    3  :  1998596384
    4  :  2686460
    5  :  1997972546
    6  :  1998587072
    7  :  2686472
    8  :  1997992444
    9  :  17
    10  :  2686536
    11  :  1998046803
    12  :  1
    13  :  1998596384
    14  :  1998046752
    15  :  -461084764
    16  :  2686592
    17  :  0
    18  :  2686544
    19  :  15
    20  :  2686492
    21  :  21
    22  :  2686916
    23  :  1998097621
    24  :  -1816436796
    What's happening here?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be easier if you pointed directly at what you found puzzling instead of just dumping the output with no explanation.

    Looking at your code, I notice that pointer is set to point to the first character of an array that goes out of scope by the time you access pointer in the last for loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, most of your arrays end up uninitialized anyway (you only set five values out of 25 in the last array, for instance).

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Doesn't the 'newlist' array technically go out of scope once you leave the if block? I'm not sure of the scoping rules for this one.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    laserlight:

    Sorry, I assumed it was obvious, my fault. But let me clear it out then:

    I wonder why the the second part of the output looks at it does? I'm trying to extend an array by using a pointer that changes which array it point's to. In other words, I create the list-array and let the pointer point to it. Then I extend it when it is filled. And then again, and again.

    When I leave that scoope, it would be nice to be able to reach all elements in the array.

    I'm basically trying to create a dynamically extended array using pointers. I'm aware this could be done with realloc somehow, or I could just use a linked-list structure. I am however curious about this way of solving it and why it does not work?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you've got why it doesn't work: 1) You're never filling the arrays in the first place and 2) the variable doesn't exist any more when you try to access it. Variable-length arrays are not resizable, so I guess that's the end of that.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hear.Me.ROAR
    I'm trying to extend an array by using a pointer that changes which array it point's to. In other words, I create the list-array and let the pointer point to it. Then I extend it when it is filled. And then again, and again.
    I believe that your idea is fundamentally flawed: a variable length array has a length that can vary at the point of creation, but once created, its length is fixed. Therefore, you cannot expand a variable length array.

    Quote Originally Posted by Hear.Me.ROAR
    When I leave that scoope, it would be nice to be able to reach all elements in the array.
    It would be nice, but by going out of scope, the array has been destroyed, so attempting to access any element of the array results in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    So, for the scope resolution thing. I found this in C-99 6.2.1.4
    If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block.
    So in the case of the OP's code would the block be the for-loop or the if-block he defined?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AndrewHunter
    So in the case of the OP's code would the block be the for-loop or the if-block he defined?
    The if statement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Thanks Laser.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    So, it seems the other options are the only ones, right? I mean the linked list or using realloc?

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That is really going to come down to what you want to do. Beyond academia, linked lists are rarely used. The main point here is the way you were originally going about it does not work for the reasons explained above.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    I want to be able to extend an array at run-time. Meaning that if it would have 10 fields I'd like to extend it to 20, then 30, and so on...

    Here is another piece when I try to do this. This compiles for all n but only runs without error for n up to 12. When n becomes greater then 12 it crashes during execution.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main () {
         int size = 5;
         int* list;
         list  = (int*) malloc (size * sizeof(int));
         int count = 0;
         int n = 12;
    
         for (count = 0; count<n; count++) {
              list[count] = count;
              printf("%d\n", *list+count);
              if (count == size) {
                   size = size+5;
                   list = (int*) realloc (list, size* sizeof(int));
                   printf("\nMemory extended to %d\n\n", size);
              }
         }
         printf("\nfor-lopp done\n\n");
         int i = 0;
         for (i = 0; i<size; i++) {
              printf ("%d  :  %d\n", i, *list+i);
         }
    }

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure that *list+count or *list+i is what you want? (Hint: They are not equivalent to list[count] or list[i].)

    What happens if realloc fails? Bad things, that's what.

    Your first for loop goes over the bounds of your malloc'ed memory every time you need to extend (you put the [5] element in before you get room for the [5] element, etc.) so you're probably trashing either what realloc needs to do its job or some other variable.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    30
    tabstop: Strange, In my book it said they are equivalent. And I do get the same answers, would you mind telling me the difference?

    However, when I use list[count] or list[i] the program crashes even earlier, at n = 11 and above. I assume the crash is cause I wrote over some piece of memory etc.

    Anyway, how will I get an array I can extend when needs be in C? How would you formulate that piece?
    Last edited by Hear.Me.ROAR; 08-09-2011 at 08:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 05-21-2011, 09:22 AM
  2. Minimizing array lookup times using effective indexing...?
    By The Dog in forum Game Programming
    Replies: 5
    Last Post: 02-08-2011, 07:11 AM
  3. Replies: 25
    Last Post: 10-31-2009, 01:45 AM
  4. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  5. Times Table with Two-Dimensional Array
    By jmb272 in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 02:22 AM