QSorting malloc'd结构数组?

时间:2011-03-08 04:03:17

标签: c pointers struct qsort

我在C中的qsort有这个比较器功能,但无论我尝试什么,我似乎都会遇到分段错误...

int textCompare ( const void * a, const void * b ){
    const char **x =(const char**)a;
    const char **y =(const char**)b;
    return strcmp(*x, *y);
}

这是我的qsort调用:其中message** mList = malloc(INITIAL_CAPACITY * sizeof(message));count是一个整数,跟踪最后一个元素。 message只是一个typedef结构,它包含一个int和一个指向char的指针。我67%肯定我正确地调用了qsort,有人能指出我正确的方向吗?

qsort (*mList, count, sizeof(message), textCompare);

[编辑] 我声明消息***而不是消息*的原因是因为我正在尝试初始化指向结构的指针的“数组”;除非我以错误的方式解决这个问题?

3 个答案:

答案 0 :(得分:4)

如果你确实想要对指向消息结构的指针数组进行排序,那么你需要使用这个

message **mlist = (message **)malloc(INITIAL_CAPACITY * sizeof(message *));

然后你必须为数组中指针指向的每条消息分配内存。

for(int i=0; i<INITIAL_CAPACITY; i++) {
  mlist[i] = (message *)malloc(sizeof(message));
  /*  initialize the members here  */
  mlist[i]->int = get_int();
  mlist[i]->char = get_char();  
  count++
  if(count >= NUM_TO_FILL_RIGHT_NOW)
   break;   
} 

现在你可以对指针数组进行排序,而不是结构本身。

int textCompare( const void *a, const void *b ) {
  message *m1 = *(message **)a;
  message *m2 = *(message **)b;
  return strcmp(m1->char, m2->char);
}

现在在指针数组上调用qsort

qsort( mlist, count, sizeof(message *), textCompare );  

使用此方法,指针位于连续的内存中(理论上),但结构本身根据需要单独进行malloced。此外,由于要复制的对象的大小,排序指针通常比排序结构更快。指针在64位机器上是8个字节,在32位机器上是4个字节,并且您的结构实际上可能比这小,但是典型的结构将比指针大。

答案 1 :(得分:1)

我不确定你是否调用malloc()为你想要使用的内容分配正确类型的内存。你正在声明一个指针数组,然后指向一些(可能的)指针数组,但似乎你是为结构本身的实际大小保留内存,即你为数组保留内存消息,而不是消息指针数组。例如,如果取消引用mlist,那么结果指针指向什么?它是否指向单个消息结构或消息结构数组(即,您有一些二维消息结构数组,其中mlist [0]到mlist [n]每个都表示指向消息数组的指针在堆上有一些任意长度的大小)?如果它不是指向任何东西,而应该是一个实际的消息结构,那么你的调用看起来应该更像:

message* mlist = malloc(INITIAL_CAPACITY * sizeof(message));

而不是mlist属于message**类型。

希望这有帮助。

答案 2 :(得分:1)

@Jason和@Tim都给出了正确的答案。这是一个正确的,有效的程序,可以展示他们的答案。

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

typedef struct message {
  int i;
  char *p;
} message;

int textCompare(const void *a, const void *b) {
  const message *ma = a;
  const message *mb = b;
  return strcmp(ma->p, mb->p);
}

int main()
{
  int i;
  message* mList = malloc(10 * sizeof(message));
  mList[0].i = 0; mList[0].p = "zero";
  mList[1].i = 1; mList[1].p = "one";
  mList[2].i = 2; mList[2].p = "two";

  qsort(mList, 3, sizeof(message), textCompare);

  for(i = 0; i < 3; i++) {
    printf("%d %s\n", mList[i].i, mList[i].p);
  }
  return 0;
}

请注意malloc来电的模式。要分配类型为T的数组:

T* var = malloc(array_size * sizeof(T));

查看声明中只有一个*,而sizeof()运算符比那个少一个星。有些人更喜欢这种形式:

T* var = malloc(array_size * sizeof *var);

这样你只需要指定一次类型。这对于复杂类型特别有用。

另请注意,malloc的结果没有强制转换。

另外,请注意qsort()的第一个arg的类型如何与compare函数中转换的参数的类型相匹配。在这种特定情况下,它们都是message*

您可能会发现此qsort调用更易于理解和维护:

qsort(mList, 3, sizeof(*mList), textCompare);
相关问题