有没有办法让我的函数返回一个动态数组?

[英]Is there any way to make my function return a dynamic array?


So at the moment I have a function I made returning a static array, is there any way to make it return a dynamic array for the sake of efficiency?

所以目前我有一个函数我返回一个静态数组,有没有办法让它返回一个动态数组为了效率?

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

int *charpos(char *str, char ch)
{
    int *bff, bc, ec, i, strln;
    static int ret[255];
    bc = 0;
    ec = 0;

    for(i = 0; str[i] != '\0'; i++)
        ;

    strln = i;
    for(i = 0; i <= strln; i++)
    {
        if(str[i] == ch)
            ec++;
    }

    bff = malloc(sizeof(int)*ec);
    if(sizeof(bff) > sizeof(ret))
    {
        free(bff);
        return 0;
    }

    for(i = 0; i <= 255; i++) ret[i] = '\0';
    for(i = 0; i <= strln; i++)
    {
        if(str[i] == ch)
        {
            ret[bc] = i;
            bc++;
        }
    }

    free(bff);
    return ret;
}

2 个解决方案

#1


5  

Functions cannot return arrays, period. You can of course a pointer or take a pointer to a block of memory that has been allocated by the caller. So, in your case...

函数无法返回数组,句点。您当然可以使用指针或指向已由调用者分配的内存块。所以,在你的情况下......

int *ret = malloc(255 * sizeof int);  // caller must deallocate!

This does change the semantics of your code however. The caller of your function is now responsible for calling free() on the returned pointer. If they do not you will leak memory, so this adds some amount of complexity that did not exist before. I would prefer something like this instead:

但这确实会改变代码的语义。函数的调用者现在负责在返回的指针上调用free()。如果它们不存在,您将泄漏内存,因此这增加了之前不存在的一些复杂性。我更喜欢这样的东西:

void charpos(int *p, size_t size, const char *str, char ch) {
    // initialize the memory 
    memset(p, 0, size * sizeof int);

    // your other code here...

    size_t len = strlen(str);
    // fill the caller's memory
    for(i = 0; i < len; ++i)
    {
        if(str[i] == ch)
            p[bc++] = i;
    }
}

If I didn't answer your question you need to elaborate for me (us). You aren't returning an array right now; you're returning a pointer to int which referes to the first element of a statically allocated array.

如果我没有回答你的问题,你需要为我(我们)详细说明。你现在没有返回阵列;你正在返回一个指向int的指针,它指向一个静态分配的数组的第一个元素。

#2


0  

You can actually allocate more space then you need with static int and not have to worry about dynamics. Here is how I solved it:

实际上,您可以使用static int分配更多空间,而不必担心动态。这是我解决它的方式:

//indefinite reads, actually only up to 20
int * readDataAD7142(int addr, int numRegs){
  static int data[20];
  int i = 0;

  //Do something with this data array. Something that requires less then 20 elements

  return data;  
}

Here is the code that calls it

这是调用它的代码

 int *p;
 int i;
 p = readDataAD7142(0x0000, 6);

 for(i=0; i<6; i++){
  Serial.println(p[i], HEX);
 }

Perfect and easy if you have more memory then you need and little time (you need to be a bit lazy too).

如果你有更多的记忆然后你需要和很少的时间(你需要有点懒惰)完美和容易。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/07/31/d8b3713f3843c7591c133bfa08aac480.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告