2010-07-06 214 views
1

我目前正在尝试为双指针分配相同数量的内存。我接受一个char **,并希望对该char **使用冒泡排序。所以我创建了一个临时char **,现在我想知道如何正确分配足够的内存,以便我可以将该temp char **返回给另一个方法。双指针内存分配

我知道我现在分配的方式看起来不正确,它肯定无效......否则我不会问这个问题。如果有人能够回应一些有用的建议,我将不胜感激!

char** bubble_sort(char **filenames, int n) 
{ 
    int i; 
    char **new_list; 
    new_list = malloc(sizeof(filenames)); 
    for (i = 0; i < n; i++) 
    { 
     // malloc(file_list.size * sizeof(int)); 
     new_list[i] = filenames[i]; 
    } 
    for (i = 0; i < n; i++) 
    { 
     printf("%d: %s\n", i, new_list[i]); 
    } 

    int x; 
    int y; 
    for(x=0; x<n; x++) 
    { 
      for(y=0; y<n-1; y++) 
      { 
        if(new_list[y]>new_list[y+1]) 
        { 
          char *temp = new_list[y+1]; 
          new_list[y+1] = new_list[y]; 
          new_list[y] = temp; 
        } 
      } 
    } 
    for (i = 0; i < n; i++) 
     { 
      printf("%d: %s\n", i, new_list[i]); 
     } 
    return new_list; 
} 
+2

我唯一的建议是,你使用std :: vector的。在不使用单个断言检查的情况下围绕char **的并使用sizeof来玩是太频繁的组合,这几乎总是导致令人讨厌的错误。而且,“malloc”?根据我的经验,这在C++中几乎是不需要的。如果你确实需要分配原始数组而不是使用std :: vector,那么使用new []和delete [] - 这是你应该做的。 – 2010-07-06 14:51:58

+1

如果我必须返回char **,该怎么办? (在这种情况下,我确实......我正在使用SDK并且需要char **) – Brandon 2010-07-06 14:53:37

+1

使用STL容器进行所有操作,然后将结果复制到正确分配的char数组。例如:“char ** pp = new char [1000] [1000];”,但不能与malloc! – 2010-07-06 14:55:56

回答

1

下面是该程序的工作副本:

#include <cstdio> 
#include <cstdlib> 
#include <cstring> 

char** bubble_sort(const char **filenames, int n) 
{ 
    int i; 
    char **new_list; 
    new_list = (char**) malloc(sizeof(*new_list) * n); 
    for (i = 0; i < n; i++) 
    { 
     new_list[i] = (char*) filenames[i]; 
    } 

    printf("Initial list:\n"); 
    for (i = 0; i < n; i++) 
    { 
     printf("%d: %s\n", i, new_list[i]); 
    } 

    int x; 
    int y; 

    printf("List is sorted:\n"); 
    for(x=0; x<n; x++) 
    { 
      for(y=0; y<n-1; y++) 
      { 
        if(strcmp(new_list[y],new_list[y+1])>0) 
        { 
          char *temp = new_list[y+1]; 
          new_list[y+1] = new_list[y]; 
          new_list[y] = temp; 
        } 
      } 
    } 
    for (i = 0; i < n; i++) 
     { 
      printf("%d: %s\n", i, new_list[i]); 
     } 
    return new_list; 
} 

int main(){ 
    const char *ar[5]={ 
     "eee", "aaa", "bbb", "ccc", "ddd", 
    }; 
    bubble_sort(ar, 5); 
    return (0); 
} 

但是,请记住,您的编程风格更类似于到C比C++(这并不总是一件坏事)。

如果您想为您的数组元素分配新的字符串,你应该改变首先为这样的:

for (i = 0; i < n; i++) 
{ 
    //new_list[i] = (char*) filenames[i]; 
    new_list[i] = (char*) malloc(sizeof(**new_list) * (strlen(filenames[i]) + 1)); 
    strcpy(new_list[i], filenames[i]); 
} 

这是Ç版本(第一个是C++版本)。需要注意的是字符串数组有其所有元素新分配的,而不是使用初始字符串从输入参数:

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

char** bubble_sort(char **filenames, int n) 
{ 
    int i; 
    char **new_list; 
    new_list = malloc(sizeof(*new_list) * n); 
    for (i = 0; i < n; i++) 
    { 
     //new_list[i] = (char*) filenames[i]; 
     new_list[i] = malloc(sizeof(**new_list) * (strlen(filenames[i]) + 1)); 
     strcpy(new_list[i], filenames[i]); 
    } 

    printf("Initial list:\n"); 
    for (i = 0; i < n; i++) 
    { 
     printf("%d: %s\n", i, new_list[i]); 
    } 

    int x; 
    int y; 

    printf("List is sorted:\n"); 
    for(x=0; x<n; x++) 
    { 
      for(y=0; y<n-1; y++) 
      { 
        if(strcmp(new_list[y],new_list[y+1])>0) 
        { 
          char *temp = new_list[y+1]; 
          new_list[y+1] = new_list[y]; 
          new_list[y] = temp; 
        } 
      } 
    } 
    for (i = 0; i < n; i++) 
     { 
      printf("%d: %s\n", i, new_list[i]); 
     } 
    return new_list; 
} 

int main(){ 
    char *ar[5]={ 
     "eee", "aaa", "bbb", "ccc", "ddd", 
    }; 
    bubble_sort(ar, 5); 
    return (0); 
} 
+0

哎呀...是的,我正在编程的一切其他的C++,但忘记这是在C.我会尝试你的解决方案在一秒钟内 – Brandon 2010-07-06 15:16:13

+0

@Brandon这部分是在C?!如果是这样,请更改标签,我将删除我的所有评论,因为他们不' C语言专家应该说话 – 2010-07-06 15:18:35

+0

请注意,在C语言版本中你应该改变头文件,并且不需要在malloc之后进行类型转换哦,还有一点,比较C字符串时要小心。 strcmp函数from string.h(或cstring if C++)。 – 2010-07-06 15:19:48

1

filenames是一个指向字符指针,因此在这条线......

new_list = malloc(sizeof(filenames)); 

...你分配指针的大小的量(以指针)这不是你想要的。

您可能想要malloc(sizeof(filenames) * n);这将为您提供n指针的空间。

+0

这是给我“无效的操作数到二进制*(有'char **'和'int) – Brandon 2010-07-06 14:56:53

+0

对不起,我有一个错字 – 2010-07-06 14:58:05

2
char** bubble_sort(char **filenames, int n) 
{ 
    int i; 
    char **new_list; 
    new_list = malloc(sizeof(filenames)); 

此代码分配足够的空间来存储单个指针(sizeof(filenames)是最有可能4),并给出该指针new_list的地址。如果你想访问new_list指向的数组(我知道你这么做,因为你试图做到这一点),你需要为它的元素分配足够的空间。

0

这有些重复。请参阅:Creating `char ***data`?

在为char数组分配二维数组时,存在严重的内存性能问题。最好使用char *和索引方案。这样你就可以获得连续的内存块。你也可以用这样的方案使用一个std :: vector。

如果你必须分配一个char **,for循环就是你所能做的AFAIK。但至少要做一个子程序! :)