当我尝试从函数返回2d结构时,我该如何解决这个问题

时间:2017-04-21 18:57:16

标签: c struct

这是我如何声明这个结构

typedef struct cache{
int vaild;
char* tag;
char* data;
}cache;

这是我的主要部分,它调用了这个函数

struct cache **cacheA = createCache(Setnum,(int)pow(2,blocksize),cachesize);
struct cache **cacheB = createCache(Setnum,(int)pow(2,blocksize),cachesize);

现在这是我所谓的函​​数

struct cache ** createCache(int numset, int blocksize, int cachesize){
    int numcache = (int)((cachesize/blocksize)*numset);
    struct cache out[numset][numcache];
    int i,j;
    for (i=0; i < numset; i++){
        for (j=0; j < numcache; j++){
            out[i][j].tag = "0";
            out[i][j].vaild = 0;
            out[i][j].data ="0";
        }
    }
    return out;
}

当我尝试编译它时,它告诉我

return from incompatible pointer type 
function returns address of local variable 

(指向“退出;”行)

我不知道我的代码有什么问题,我的意思是函数返回的类型与我如何解析“out”相同,那么是什么导致了这个问题呢?

3 个答案:

答案 0 :(得分:0)

您的函数需要在堆上而不是堆栈上分配内存。您将需要在堆上为您的指针数组分配空间,以及它们指向的内容。

def firstwords(self,large_song_list): # method that takes the first word of each song title and creates a list containing that first word 
        all_first_words = [] # create an empty list 
        for track in large_song_list: 
            first_word = track.trackName.partition(' ')[0] # split the string at the first word, isolates the first word in the title 
            all_first_words.append(first_word)
        return all_first_words

PS:如果您使用struct cache ** createCache(int numset, int blocksize, int cachesize){ cache ** out; int numcache = (int)((cachesize/blocksize)*numset); size_t headerSize = sizeof(*out)*numset; size_t bodySize = sizeof(**out)*numcache; out = malloc(headerSize + (bodySize*numset)); if (out == NULL) { /* Should probably output some message about being * insufficient memory here. */ return NULL; } int i,j; for (i=0; i < numset; i++){ /* need to assign our point */ out[i] = (cache*)(((char*)out)+(headerSize+bodySize*i)); for (j=0; j < numcache; j++){ out[i][j].tag = "0"; out[i][j].vaild = 0; out[i][j].data ="0"; } } return out; } /* importantly, you want a way to free your allocated memory */ void destroyCache(cache ** ptr) { free(ptr); } 关键字引用结构,则不必typedef结构。

答案 1 :(得分:0)

您创建struct cache out[numset][numcache];
在函数原型中:struct cache ** createCache(...)
然后尝试返回out

这是因为struct cache [][]的输入方式与struct cache **不同,导致返回错误。

其他评论:
1)如果你真的想要一个指向struct的指针,那么需要在某些时候使用malloc或calloc来分配内存。
2)在分配值之前,还需要为结构的char *成员分配内存。如下图所示,它们更改为char []
3)使用=赋值运算符为字符串赋值不起作用。使用字符串函数,例如strcpysprintf等。
4)您使用与您创建的新类型相同的符号命名结构,即cache。在此应用程序中,名称缓存不是必需的。另外,纯粹为了风格,我在CAPS中展示了新类型。 这不是必需的,而只是我用来使代码中的新类型更易识别的样式。

考虑到上述评论,结构可以更改为以下内容:

typedef struct { /// don't need name here when it in this application
    int vaild;
    //char *tag;
    char tag[20];//for illustration, to avoid additional dynamic allocation of memory
    //char* data;
    char data[80];
}CACHE;//capitalization is style only, not a necessity here.

注意,没有名称,但创建了新类型CACHE。现在,您可以创建createCache函数:

CACHE ** createCache(int ncache, int nset)//note for simplicity of freeing this 
                                          //object later, simplify number of arguments
{
    CACHE **out;
    out = calloc(ncache, sizeof(CACHE *));//create array of pointers to CACHE
    if(!out) return NULL;
    int i;
    for (i=0; i < nset; i++)
    {
        out[i] = calloc(nset, sizeof(CACHE));//create space for each instance 
                                             //of CACHE pointed to by array pointers
    }
    return out;
}

随时在堆上创建内存,需要释放它。此方法将释放CACHE对象内存:

void freeCashe(CACHE **a, int nset)
{
    int i;
    for(i=0; i<nset; i++)
    {
        if(a[i])free(a[i]); 
    }
    if(a)free(a);
}

如下所示调用这些函数,将创建一个指针数组,每个指针都指向CACHE的实例,您可以按预期使用它们,然后在完成时释放所有内存:

int main(void)
{
    int cachesize = 20;
    int blocksize = 20;
    int numset   = 10;
    //move the calculation out of creation function
    //to simplify freeing object later.
    int numcache = (int)((cachesize/blocksize)*numset);

    CACHE **a = createCache(numcache, numset);
    /// use a, then free a
    freeCashe(a, numset);

    return 0;
}

答案 2 :(得分:0)

您希望返回指针指针类型,但为了做到这一点,您需要动态分配它。本地堆栈分配(即struct cache [x] [y])将不起作用。尝试使用2D阵列时,您将收到错误或程序将崩溃。

解决方案是预先分配空间并将其传递给函数或在函数本身中分配。

功能示例中的分配:

struct cache ** createCache(int numset, int blocksize, int cachesize){
    int numcache = (int)((cachesize/blocksize)*numset);

    struct cache **out = malloc(sizeof(struct cache *) * numset); // This line changed.
    int i,j;
    for (i=0; i < numset; i++){

        out[i] = malloc(sizeof(struct cache) * numcache); // This line added.

        for (j=0; j < numcache; j++){

            out[i][j].tag = malloc(sizeof(char)); // This line added.
            out[i][j].data = malloc(sizeof(char)); // This line added.

            strcpy(out[i][j].tag, "0");
            out[i][j].vaild = 0;
            strcpy(out[i][j].data, "0");
        }
    }
    return out;
}