Where do the wierd numbers come from

This is a discussion on Where do the wierd numbers come from within the C Programming forums, part of the General Programming Boards category; hi, can anyone help me in clarifying where do the 'strange' numbers on position 21 and further in the array ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    176

    Where do the wierd numbers come from, and memory mgmnt

    hi,

    can anyone help me in clarifying where do the 'strange' numbers on position 21 and further in the array come from .... so the code is as follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MALLOC(num,type) (type *)allocate((num)*sizeof(type))
    
    /*Function declaration*/
    extern void *allocate(size_t size);
    
    /*Main*/
    
    int main (){
    
    int *array;
    int i, c, j=0, t, v;
    
    for (t=1;t<=2;t++){
      scanf("%d",&c);
      v= j+c;
      if (t == 1) array = MALLOC(c,int);
      if (t > 1) array = realloc(array, v);  
      while(j<=v){
        array[j]= j;
        j++;
      }
      printf("\n");
      for(i=0;i<=j+1;i++){
        printf("%d[%d],",i,array[i]);
      }
      j++;
      printf("\nThe Dynamic Array size is: %d, the 21st->%d\n",j, array[21]);
    }
    
    return 0;
    }
    
    
    /*Functions*/
    
    
    void *allocate(size_t size) {
      void *new_mem;
      new_mem = malloc(size);
      if (new_mem == NULL) exit(1);
      return new_mem;
    }
    the result i get for entering the 40 in th efirst run and 40 in the second run is :
    Code:
    40
    
    0[0],1[1],2[2],3[3],4[4],5[5],6[6],7[7],8[8],9[9],10[10],11[11],12[12],13[13],14[14],15[15],16[16],17[17],18[18],19[19],20[20],21[21],
    22[22],23[23],24[24],25[25],26[26],27[27],28[28],29[29],30[30],31[31],32[32],33[33],34[34],35[35],36[36],37[37],38[38],39[39],
    40[40],41[135001],42[0],
    
    The Dynamic Array size is: 42, the 21st->21
    40
    
    0[0],1[1],2[2],3[3],4[4],5[5],6[6],7[7],8[8],9[9],10[10],11[11],12[12],13[13],14[14],15[15],16[16],17[17],18[18],19[19],20[20],21[135081],
    22[22],23[23],24[24],25[25],26[26],27[27],28[28],29[29],30[30],31[31],32[32],33[33],34[34],35[35],36[36],37[37],38[38],39[39],40[40],
    41[135001],42[42],43[43],44[44],45[45],46[46],47[47],48[48],49[49],50[50],51[51],52[52],53[53],54[54],55[55],56[56],57[57],58[58],
    59[59],60[60],61[61],62[62],63[63],64[64],65[65],66[66],67[67],68[68],69[69],70[70],71[71],72[72],73[73],74[74],75[75],76[76],77[77],
    78[78],79[79],80[80],81[81],82[82],83[0],84[0],
    
    The Dynamic Array size is: 84, the 21st->135081
    Note that on the 21'st place in the array the number in the first run is 21 but in the second is 135081. Why ??????

    thnx
    baxy
    Last edited by baxy; 01-28-2011 at 10:49 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You keep walking over the end of your array, so you shouldn't expect to get anything worthwhile.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Your macro MALLOC really isn't doing you any favours.

    Also, your realloc makes memory smaller, not larger.
    It too needs to multiply by sizeof(type)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    ok,

    now i understan the strange numbers but still not figuring out the memory alocation... the code i changed in the following way:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /*Function declaration*/
    void *allocate(size_t size);
    
    /*Main*/
    
    int main (){
    
    char *array;
    int i, c, j=0, t,v;
    array = allocate(1 * sizeof(char));
    while((scanf("%d",&c)) != EOF){
      v = c+j;
      while(j<=v){
        array[j]= 'x';
        j++;
      }
      printf("\n");
      for(i=0;i<=v;i++){
        printf("%d[%c],",i,array[i]);
      }
      printf("\nThe Dynamic Array size is: %d\n",j);
    }
    
    return 0;
    }
    
    
    
    
    
    void *allocate(size_t size) {
      void *new_mem;
      new_mem = malloc(size);
      if (new_mem == NULL){
        printf("out of memory ??\n");
        exit(1);
      }
      return new_mem;
    }
    now it seams like i can enter any number of values into the function without resizing the memory... how this can be, can anyone explain me this a bit ?? also whe i try to add 500000 entries it returns: Segmentation fault

    thank you

    ps

    should i put this into another thread ?
    Last edited by baxy; 01-28-2011 at 10:52 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Well the crude (and no error checking) way is like so.
    Code:
    int *array = NULL;
    int count = 0;
    int value;
    while ( scanf("%d", &value) == 1 ) {
      array = realloc( count+1, sizeof(*array));
      array[count++] = value;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,639
    C doesn't prevent you from running off the end of an array or chunk of dynamically allocated memory. You're "fine" with smaller numbers because you have stayed within the limits of the memory segment where you allocated your array. While you are writing in memory you don't own, the entire segment still has read & write permissions, so the memory manager doesn't know you're doing anything illegal. The 500,000 "limit" is a coincidence. Somewhere around there, you overrun your array so far that you have gone into an invalid memory segment, hence the segmentation fault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matching numbers
    By kirksson in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 02:51 PM
  2. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 07:28 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21