如何在C函数中动态分配malloc内存? [英] How to dynamic malloc memory in function in c?
问题描述
我想调用这样的函数:
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;
}
我在问如何返回segs
和tags
中的值?
修改
现在我将代码更改为此:
#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
(函数参数是函数局部变量),而不是更改通过引用作为参数传递给函数的类型int *
的原始指针tags
.>
I want to call a function like this:
char* Seg(char* input, char **segs, int* tags)
in fact input
is the real input, segs
tags
is the return, and now return is the error message.
my program like this:
#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;
}
I am asking how can I return the value in segs
and tags
?
Edit
Now I change code to this:
#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;
}
Why tags
is still nil?
If I have understood you correctly then you need something like the following.
I used sentinel values for the both dynamically allocated arrays. You can use your own approach instead of using sentinel values.
#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;
}
The program output is
abc abc abc abc abc
0 1 2 3 4
After you updated your post then the function can look also the following way
#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;
}
You can also add code to the function that checks whether the memory was allocated successfully.
As for your additional question then such a code like for example this
int *tags_ = malloc(n * sizeof(int));
tags = &tags_;
changes local variable tags
of the type int **
(function parameters are function local variables) instead of changing the original pointer tags
of the type int *
passed to the function by reference as an argument.
这篇关于如何在C函数中动态分配malloc内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!