c中函数内部的Malloc用法

c中函数内部的Malloc用法,c,C,我试图传递一个指向函数的指针。在这个函数中,我使用malloc来保留空间。问题是当我返回main时,程序没有响应。下面是我的代码: #include <stdio.h> #include <stdlib.h> #include <string.h> int def(char **B){ int i; B = malloc(3 * sizeof( char )); for(i = 0; i < 2 ; i++){ B

我试图传递一个指向函数的指针。在这个函数中,我使用malloc来保留空间。问题是当我返回main时,程序没有响应。下面是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int def(char **B){
    int i;
    B = malloc(3 * sizeof( char ));
    for(i = 0; i < 2 ; i++){
        B[i] = malloc(5 * sizeof(char));
    }
    for(i = 0; i < 2 ; i++){
        scanf("%s" , B[i]);
    }
    for(i = 0; i < 2 ; i++){
        printf("%s\n" , B[i]);
    }
    return 0;
}

int main(int argc, char *argv[]) {
    char **B;
    int i;
    def(B);
    for(i = 0; i < 2 ; i++){
        printf("%s\n" , B[i]);
    }
    return 0;
}
#包括
#包括
#包括
内部定义(字符**B){
int i;
B=malloc(3*sizeof(char));
对于(i=0;i<2;i++){
B[i]=malloc(5*sizeof(char));
}
对于(i=0;i<2;i++){
scanf(“%s”,B[i]);
}
对于(i=0;i<2;i++){
printf(“%s\n”,B[i]);
}
返回0;
}
int main(int argc,char*argv[]){
字符**B;
int i;
def(B);
对于(i=0;i<2;i++){
printf(“%s\n”,B[i]);
}
返回0;
}
应该是

char** def(char **B) 
return B; 
 /* else the memory allocated inside the function will be freed at
  * the end and by accessing it later you have undefined behavior for the 
  * rest of the program
  */ 
B = malloc(3 * sizeof( char*)); // you have two levels of indirection. so char* first
 for(i = 0; i < 3 ; i++) // you used 3 in the above step
B=def(B);
它的返回值应该是

char** def(char **B) 
return B; 
 /* else the memory allocated inside the function will be freed at
  * the end and by accessing it later you have undefined behavior for the 
  * rest of the program
  */ 
B = malloc(3 * sizeof( char*)); // you have two levels of indirection. so char* first
 for(i = 0; i < 3 ; i++) // you used 3 in the above step
B=def(B);

应该是

char** def(char **B) 
return B; 
 /* else the memory allocated inside the function will be freed at
  * the end and by accessing it later you have undefined behavior for the 
  * rest of the program
  */ 
B = malloc(3 * sizeof( char*)); // you have two levels of indirection. so char* first
 for(i = 0; i < 3 ; i++) // you used 3 in the above step
B=def(B);

应该是

char** def(char **B) 
return B; 
 /* else the memory allocated inside the function will be freed at
  * the end and by accessing it later you have undefined behavior for the 
  * rest of the program
  */ 
B = malloc(3 * sizeof( char*)); // you have two levels of indirection. so char* first
 for(i = 0; i < 3 ; i++) // you used 3 in the above step
B=def(B);
使用
free()
释放分配的内存是一种很好的做法,尽管它会在程序结束时自动释放

应该是

char** def(char **B) 
return B; 
 /* else the memory allocated inside the function will be freed at
  * the end and by accessing it later you have undefined behavior for the 
  * rest of the program
  */ 
B = malloc(3 * sizeof( char*)); // you have two levels of indirection. so char* first
 for(i = 0; i < 3 ; i++) // you used 3 in the above step
B=def(B);
它的返回值应该是

char** def(char **B) 
return B; 
 /* else the memory allocated inside the function will be freed at
  * the end and by accessing it later you have undefined behavior for the 
  * rest of the program
  */ 
B = malloc(3 * sizeof( char*)); // you have two levels of indirection. so char* first
 for(i = 0; i < 3 ; i++) // you used 3 in the above step
B=def(B);

应该是

char** def(char **B) 
return B; 
 /* else the memory allocated inside the function will be freed at
  * the end and by accessing it later you have undefined behavior for the 
  * rest of the program
  */ 
B = malloc(3 * sizeof( char*)); // you have two levels of indirection. so char* first
 for(i = 0; i < 3 ; i++) // you used 3 in the above step
B=def(B);

应该是

char** def(char **B) 
return B; 
 /* else the memory allocated inside the function will be freed at
  * the end and by accessing it later you have undefined behavior for the 
  * rest of the program
  */ 
B = malloc(3 * sizeof( char*)); // you have two levels of indirection. so char* first
 for(i = 0; i < 3 ; i++) // you used 3 in the above step
B=def(B);

使用
free()
释放分配的内存是一种很好的做法,尽管它会在程序结束时自动释放您似乎试图分配一个字符串列表,但您无法在
def
函数之外引用它。问题的第一部分是,您需要提供指向列表的指针:

int def(char ***B) {
另一个选项是返回已创建的指针:

char** def() {
第二,在第一个malloc中,为3个
char
分配空间,但需要
char
指针<代码>字符大小!=字符大小*

B = malloc(3 * sizeof( char ));
应该是:

*B = malloc(3 * sizeof char*);
您还应该使用
const int
#define
来定义大小常量,这样您就不会在不同的地方意外地得到错误的大小,例如

#define SIZE_OF_LIST 3
int def(char ***B){
    int i;
    B = malloc(SIZE_OF_LIST * sizeof( char ));
    for(i = 0; i < SIZE_OF_LIST ; i++){


您似乎试图分配一个字符串列表,但没有提供在
def
函数之外引用该列表的功能。问题的第一部分是,您需要提供指向列表的指针:

int def(char ***B) {
另一个选项是返回已创建的指针:

char** def() {
第二,在第一个malloc中,为3个
char
分配空间,但需要
char
指针<代码>字符大小!=字符大小*

B = malloc(3 * sizeof( char ));
应该是:

*B = malloc(3 * sizeof char*);
您还应该使用
const int
#define
来定义大小常量,这样您就不会在不同的地方意外地得到错误的大小,例如

#define SIZE_OF_LIST 3
int def(char ***B){
    int i;
    B = malloc(SIZE_OF_LIST * sizeof( char ));
    for(i = 0; i < SIZE_OF_LIST ; i++){


似乎您正在尝试malloc一些看起来像2D数组(或者更确切地说是字符串数组)的东西

但是,

def(B);
是按值调用,因此当函数返回时,
B
不会更改

如果您想更改
B
,您需要

def(&B);
然后你需要相应地修改函数签名,这样你就会成为一名三星级程序员

如果要纠正此错误,请阅读:


阅读@Lundin给出的答案——这就是解决问题的方法

似乎您正在尝试对看起来像2D数组(或者更确切地说是字符串数组)的东西进行malloc

但是,

def(B);
是按值调用,因此当函数返回时,
B
不会更改

如果您想更改
B
,您需要

def(&B);
然后你需要相应地修改函数签名,这样你就会成为一名三星级程序员

如果要纠正此错误,请阅读:


读一读@Lundin给出的答案——这就是解决问题的方法

事实上,比这更糟糕。所有C函数参数都是“按值”的,这意味着函数中的变量是调用时传入的变量的本地副本。因此,当你给一个参数赋值(你可以这样做,除非它被声明为const)[C++假设'const ''已经在C中采用了]),你就不会分配给调用中传递的变量。所以在这种情况下,声明

B = malloc(3 * sizeof(char *));
不改变main()中声明的指针B;它只是泄漏内存。假设您确实需要为此函数返回一个int,则需要添加一个间接级别:

int def(char ***B){
    *B = malloc(3 * sizeof(char *));
    for(i=0; i<2; i++){
        (*B)[i] = malloc(5 * sizeof(char));
    }
    ...
}

...

char **B;
...
def( &B );   /* Note the 'address-of' operator! */
intdef(字符***B){
*B=malloc(3*sizeof(char*);

对于(i=0;i<p>实际上,它比它更坏),所有的C函数参数都是“按值”,这意味着函数内的变量是调用中传递的变量的本地副本。因此,当您将一个值赋值给一个参数(除非它被声明为const)(除非现在C++中已经使用了C++),否则就允许您这样做。,您没有分配给调用时传入的变量

B = malloc(3 * sizeof(char *));
不会更改main()中声明的指针B;它只是泄漏内存。如果确实需要为此函数返回int,则需要添加一个间接级别:

int def(char ***B){
    *B = malloc(3 * sizeof(char *));
    for(i=0; i<2; i++){
        (*B)[i] = malloc(5 * sizeof(char));
    }
    ...
}

...

char **B;
...
def( &B );   /* Note the 'address-of' operator! */
intdef(字符***B){
*B=malloc(3*sizeof(char*);

对于(i=0;i您的问题是,当您调用
def(B)
时,您正在创建一个
char**
,它是
B
副本。如果您更改这个新的
B
,这些更改不会反映在主函数上,因为它们是对
B
的副本所做的

您应该使用<代码> char **/COD>并调用<代码> DEF(& b)< /代码>(我不认为这是一个好方法,THO),或者您可以在主代码中初始化<代码> B/<代码>,调用<代码> DEF(b)< /代码>进行分配并读取其<代码> CHAR*<代码>,或者您可以返回新<代码> B<代码> >


另外,检查其他答案中指出的问题。

您的问题是,当您调用
def(B)
时,您正在创建一个
char**
,它是
B
副本。如果您更改此新的
B
,则更改不会反映在主函数sinc上