2010-07-06 138 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。但至少要做一個子程序! :)