Like Tree2Likes
  • 1 Post By AndiPersti
  • 1 Post By AndiPersti

Short Sorting Problem

This is a discussion on Short Sorting Problem within the C Programming forums, part of the General Programming Boards category; I am having trouble debugging my segmentation fault error in this short code! Code: void radixsort(unsigned long array[]){ int arraysize ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    Short Sorting Problem

    I am having trouble debugging my segmentation fault error in this short code!

    Code:
    void radixsort(unsigned long array[]){
      int arraysize = sizeof(array)/sizeof(*array);
      unsigned long move[arraysize];
      int count[256],map[256],mask,i,group=8,val,index=0; 
      while(index<4){
        for(i=0;i<arraysize;i++){
          val=array[i]>>(index*group);
          mask = val&0xff;
          count[mask]++;
        }
        for(i=1;i<256;i++){
          map[i]=map[i-1]+count[i-1];
        }
        for(i=0;i<arraysize;i++){
          val=array[i]>>(index*group);
          mask = val&0xff;
          move[map[mask]++]=array[i];
        }
        index++;
      }
      array = move;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You don't initialize "count" and "map".

    Bye, Andreas
    workisnotfun likes this.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    This may be a dumb question but is there an easy way to initialize without using a simple for loop and setting everything to 0?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    int main(void)
    {
        int array[5] = { 1,1,1,1,1 };
    
        return 0;
    }
    I think you see while the loops are so beloved by many (including me)

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by workisnotfun View Post
    This may be a dumb question but is there an easy way to initialize without using a simple for loop and setting everything to 0?
    If you want to set every element to 0 try
    Code:
    int count[256] = { 0 }, map[256] = { 0 };
    By default, all missing elements in an initializer list are set to 0 by the compiler.

    Bye, Andreas
    workisnotfun likes this.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    Thanks for the tip, the seg fault's fixed. On another note, is there something wrong with my sorting logic or is the parameter suppose to be a pointer to an array? When I print the values of my array before and after calling this function, the values are in the same place, unmoved.
    Code:
     unsigned long j[size];
      argv++;
      for(i=0;i<size;i++){
        j[i]=atoi(*argv++);
      }
    radixsort(j);

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by AndiPersti View Post
    By default, all missing elements in an initializer list are set to 0 by the compiler.
    Which is why I do the even shorter:
    Code:
    int count[256] = {}, map[256] = {};
    Also, you need a loop to copy the item back into the array they came from.
    Last edited by iMalc; 11-19-2012 at 12:15 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. short matrix problem
    By kiwi101 in forum C Programming
    Replies: 76
    Last Post: 10-04-2012, 02:31 PM
  2. Replies: 5
    Last Post: 10-01-2012, 11:57 PM
  3. How come short + int = int but short + uint = long?
    By y99q in forum C# Programming
    Replies: 2
    Last Post: 10-29-2011, 12:16 PM
  4. Please help! short while loop tracking problem.
    By matthayzon89 in forum C Programming
    Replies: 7
    Last Post: 04-22-2010, 01:29 PM
  5. Replies: 7
    Last Post: 02-08-2008, 06:31 PM

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