双指针内存分配

时间:2010-07-06 14:49:19

标签: c++ pointers memory-management

我目前正在尝试为双指针分配相同数量的内存。我接受了一个char **并希望在该char **上使用冒泡排序。所以我创建了一个临时char **,现在我想知道如何正确分配足够的内存,以便我可以将该临时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;
}

4 个答案:

答案 0 :(得分: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指向的数组(我知道你这样做,因为你试图在下面这样做),你需要为它的元素分配足够的空间。

答案 1 :(得分:1)

filenames是指向char的指针,因此在这一行......

new_list = malloc(sizeof(filenames));

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

您可能希望malloc(sizeof(filenames) * n);n指针提供空间。

答案 2 :(得分: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 版本(第一个版本是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);
}

答案 3 :(得分:0)

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

在char **中为2D数组分配存在严重的内存性能问题。最好使用char *和索引方案。通过这种方式,您可以获得连续的内存块。您也可以使用带有这种方案的one-st std :: vector。

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

相关问题