4

Gives the following discrepancy when printing an array:

Output of last run, given array size of 10:

1054923524,536171146,1590310503,900411369,764853670,471563977,933417110,1800497411,544592671,135121
1054923524,536171146,1590310503,900411369,764853670,471563977,933417110,1800497411,544592671,0,

Any idea why does it prints "0" instead of what I thought? (the last digit of the array)

#include <stdio.h>
#include <stdlib.h>

main() {
  srandom(time(NULL));
  printf("Size:");
  int n;
  scanf("%d",&n);
  int *ints;
  ints=malloc(sizeof(int)*n);
  int i;
  for(i=1; i<n-1;i++) {
    int a=random();
    ints[i]=a;
    printf("%d,",a);
  }
  printf("%d\n",ints[n]);
  sort(&ints[0],&ints[n]);
  free(ints);
}

void sort(int *begin, int *end) {
  int i;
  for(i=0;&(begin[i])!=end;i++) {
    printf("%d,",begin[i]);
  }
}
Alejandro Galera
  • 3,445
  • 3
  • 24
  • 42

3 Answers3

3

You never stored anything into the last element of the array ints[n-1]. You print the wrong last element of the array, which invokes undefined behavior, since you are reading beyond the end of the allocated memory area.

Change your initialization loop to initialize all members, by looping while i < n. Remove your printf() following the loop, and instead, just print a newline character.

  for(i=0;i<n;i++) {
    int a=random();
    ints[i]=a;
    printf("%d,",a);
  }
  putchar('\n');
jxh
  • 69,070
  • 8
  • 110
  • 193
  • Thanks, didn't catch that. – user2452830 Aug 12 '13 at 18:39
  • @user2452830: The suggested changes I provided should be all you need to do to get the print loop in your `sort()` function to print the same numbers you print during initialization (except for the last newline). – jxh Aug 12 '13 at 18:50
  • I must not have read it completely the first time. That was very helpful. You're right; I created more problems by trying to change other things. This completely solves the problem. Thanks. – user2452830 Aug 12 '13 at 18:54
1

There are two mistakes:

  1. The element at index n - 1 is never initialized as your first loops only goes from 0 to n - 2.

  2. The element at index n is beyond the allocated memory.

So the last element you print has index n on the first run, and index n - 1 on the second run.

The fix could look like this:

#include <stdio.h>
#include <stdlib.h>

main() {
  srandom(time(NULL));
  printf("Input desired size of array:");
  int n;
  scanf("%d",&n);
  int *ints;
  ints=malloc(sizeof(int)*n);
  if(ints==NULL) {
    perror("malloc returned NULL");
    exit(1);
  }

  int i;
  for(i=0; i < n; i++) {
    int a=random();
    ints[i]=a;
    printf("%d,",a);
  }

  sort(&ints[0],&ints[n],0);
  free(ints);
}

void sort(int *begin, int *end, int ascending) {
  int i;
  for(i=0;&(begin[i])!=end;i++) {
    printf("%d,",begin[i]);
  }
}
Codo
  • 75,595
  • 17
  • 168
  • 206
  • Thanks. I'll change that and try again. – user2452830 Aug 12 '13 at 18:39
  • Stll some problem though; now outputs:1821250995,624166897,1892989986,204514006,2053074628,1250179355,964072650,1085680991,1771083365,1316936590,1316936590 1821250995,624166897,1892989986,204514006,2053074628,1250179355,964072650,1085680991,1771083365, – user2452830 Aug 12 '13 at 18:41
  • So you don't have to actually look at that: It printed 11 numbers first, then printed 9. – user2452830 Aug 12 '13 at 18:43
  • Changed the condition on the loop while storing elements to: i – user2452830 Aug 12 '13 at 18:44
  • Also, as pointed out by jxh, removed the printf after the for loop so the 11th number is no longer there. Still only get 9 numbers later though. – user2452830 Aug 12 '13 at 18:49
0

printf("%d\n",ints[n]);

That's an overflow, its undefined behavior. you want printf("%d\n",ints[n-1]);

Scotty Bauer
  • 1,277
  • 9
  • 14