c - 如何在c中的函数中动态分配内存?

标签 c memory malloc

我想调用这样的函数:

char* Seg(char* input, char **segs, int* tags)

其实input是真正的输入,segs tags是返回,现在return是错误信息。

我的程序是这样的:

#include <stdio.h>

char* Seg(char* input, char **segs, int* tags) {
    // dynamic malloc the memory here
    int count = strlen(input); // this count is according to input
    for (int i = 0; i < count; i++) {
        segs[i] = "abc";
    }
    for (int i = 0; i < count; i++) {
        tags[i] = i;
    }
    return NULL;
}

int main(int argc, char *argv[])
{
    char** segs = NULL;
    int* tags = NULL;
    Seg("input", segs, tags);

    return 0;
}

我问如何返回segstags中的值?

<小时/>

编辑

现在我将代码更改为:

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

/**
 * input is input params, segs and tags is results
 * return: error msg
*/
int Seg(char* input, char ***segs, int** tags) {
    int n = strlen(input);
    int *tags_ = malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) {
        tags_[i] = i;
    }
    tags = &tags_;
    char **segs_ = malloc(sizeof(char *) * n);
    for (int i = 0; i < n; i++) {
        segs_[i] = "haha";
    }
    segs = &segs_;

    return n;
}

int main(int argc, char *argv[])
{
    char** segs = NULL;
    int* tags = NULL;
    int n = Seg("hahahahah", &segs, &tags);
    printf("%p", tags);
    free(segs);
    free(tags);

    return 0;
}

为什么tags仍然为零?

最佳答案

如果我正确理解了您的意思,那么您需要如下所示的内容。

我对两个动态分配的数组使用了哨兵值。您可以使用自己的方法而不是使用标记值。

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

char * Seg( const char *input, char ***segs, int **tags ) 
{
    // dynamic malloc the memory here
    size_t count = strlen( input ); // this count is according to input

    *segs = malloc((count + 1) * sizeof(char *));
    *tags = malloc((count + 1) * sizeof(int));

    for ( size_t i = 0; i < count; i++ )
    {
        ( *segs )[i] = "abc";
    }

    (*segs)[count] = NULL;

    for ( size_t i = 0; i < count; i++ ) 
    {
        ( *tags )[i] = ( int )i;
    }
    (*tags)[count] = -1;

    return NULL;
}

int main( void )
{
    char **segs = NULL;
    int *tags = NULL;

    Seg( "input", &segs, &tags );

    for (char **p = segs; *p; ++p)
    {
        printf( "%s ", *p );
    }

    putchar('\n');

    for (int *p = tags; *p != -1; ++p)
    {
        printf("%d ", *p);
    }

    putchar('\n');

    free(segs);
    free(tags);

    return 0;
}

程序输出为

abc abc abc abc abc
0 1 2 3 4

更新帖子后,该功能也可以如下所示

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

size_t Seg( const char *input, char ***segs, int **tags ) 
{
    // dynamic malloc the memory here
    size_t count = strlen( input ); // this count is according to input

    *segs = malloc(count * sizeof(char *));
    *tags = malloc(count * sizeof(int));

    for ( size_t i = 0; i < count; i++ )
    {
        ( *segs )[i] = "abc";
    }

    for ( size_t i = 0; i < count; i++ ) 
    {
        ( *tags )[i] = ( int )i;
    }

    return count;
}

int main( void )
{
    char **segs = NULL;
    int *tags = NULL;

    size_t n = Seg( "input", &segs, &tags );

    for (size_t i = 0; i < n; i++)
    {
        printf( "%s ", segs[i] );
    }

    putchar('\n');

    for (size_t i = 0; i < n; i++)
    {
        printf("%d ", tags[i]);
    }

    putchar('\n');

    free(segs);
    free(tags);

    return 0;
}

您还可以在函数中添加代码来检查内存是否分配成功。

至于你的附加问题,那么这样的代码就像这样

int *tags_ = malloc(n * sizeof(int));
tags = &tags_;

改变int **类型的局部变量tags(函数参数为函数局部变量),而不是改变原来的指针tags通过引用作为参数传递给函数的类型 int *

关于c - 如何在c中的函数中动态分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46664685/

相关文章:

c - 如何使用 sscanf 单独读取字符串的子字符串

php - 如何在 PHP 中释放内存?

C++从内存中读取字符串

.NET 内存泄漏?

c - Malloc/免费自己的实现

c - 在动态内存分配器中保存已释放 block 列表时,使用 LIFO 顺序还是 FIFO 顺序更好?

c - 将指针值放入数组中

c - 将作为另一个结构成员的结构中的指针成员传递给接受双指针的函数

使用 Dynamic Prog 的加泰罗尼亚数字

c - ProC 在 10g 上编译并在 11g 上运行