c 指针到指针的内存分配
我是一个 c 菜鸟,我的这段代码无法正常工作,因为我为 char** 指针做了一些错误的内存分配。你能帮忙吗?提前谢谢很多。 代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node_t {
struct tuple_t *tuple; //Entrada na lista
struct node_t *next; //o node seguinte da lista
};
struct tuple_t {
long timestamp; /* instante de criacao do tuplo*/
int n_fields; /* numero de campos deste tuplo */
char **fields; /* array com campos do tuplo */
/* 4 + 4 + 4 bytes?? */
};
char ** split_str(char [], char **, const char *);
struct node_t *node_create(void *node_data){
struct node_t *node = NULL;
node = (struct node_t *)malloc(sizeof(struct node_t));
if(!node){
printf("Erro ao criar um node!\n");
return NULL;
}
node->tuple = (struct tuple_t *)malloc(sizeof(struct tuple_t));
if(!node->tuple){printf("Erro ao criar o node->tuple\n"); free(node); return NULL;}
node->tuple->fields = (char ** )malloc(strlen((char *) node_data) * sizeof(char *));
if(!node->tuple->fields){ printf("Erro ao criar o node->tuple->node_fields\n"); free(node->tuple); free(node); return NULL; }
char **array;
const char *sep=" ";
char *s = (char *)node_data;
char arr[strlen(s)];
int i = 0;
while(arr[i++]=s[i]);
array = split_str(arr,array, sep);
i = 0;
while(array[i]){
node->tuple->fields[i] = (char *)malloc((strlen(array[i])) * sizeof(char));
if(!node->tuple->fields[i]){
printf("Erro ao alocar memoria em node_create() para node->tuple->fields[i]\n");
return NULL;
}
node->tuple->fields[i] = array[i];
// printf("array[i]=%s\n",array[i]);
// printf("node->tuple->fields[i]=%s\n",node->tuple->fields[i]);
i++;
}
node->tuple->n_fields = i;
node->tuple->timestamp = 0L;
node->next = NULL;
return node;
}
char** split_str(char writablestring[],char **array, const char *sep ){
array = malloc(strlen(writablestring) + 1);
if(! array){printf("Erro ao alocar memoria para o array em split\n"); return NULL;}
char *token = strtok(writablestring, sep);
int i=0;
while(token != NULL)
{
array[i] = malloc(strlen(token)+1);
if(!array[i])
return NULL;
array[i] = token;
token = strtok(NULL, " ");
i++;
}
return array;
}
int main(int argc, char** argv)
{
void * n_data = "hello 123 ploc";
struct node_t * node = node_create(n_data);
printf("node->num_fields=%d\n", node->tuple->n_fields);
int i=0;
while( node->tuple->fields[i] ){
printf("node->tuple->fields[%d]=%s\n",i,node->tuple->fields[i]);
i++;
}
return 0;
}
结束代码。
I am a noob in c, and i have this code that doesnt work properly because some bad memory allcoation i make for a char** pointer. Could you please help? Thx a lot in advance.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node_t {
struct tuple_t *tuple; //Entrada na lista
struct node_t *next; //o node seguinte da lista
};
struct tuple_t {
long timestamp; /* instante de criacao do tuplo*/
int n_fields; /* numero de campos deste tuplo */
char **fields; /* array com campos do tuplo */
/* 4 + 4 + 4 bytes?? */
};
char ** split_str(char [], char **, const char *);
struct node_t *node_create(void *node_data){
struct node_t *node = NULL;
node = (struct node_t *)malloc(sizeof(struct node_t));
if(!node){
printf("Erro ao criar um node!\n");
return NULL;
}
node->tuple = (struct tuple_t *)malloc(sizeof(struct tuple_t));
if(!node->tuple){printf("Erro ao criar o node->tuple\n"); free(node); return NULL;}
node->tuple->fields = (char ** )malloc(strlen((char *) node_data) * sizeof(char *));
if(!node->tuple->fields){ printf("Erro ao criar o node->tuple->node_fields\n"); free(node->tuple); free(node); return NULL; }
char **array;
const char *sep=" ";
char *s = (char *)node_data;
char arr[strlen(s)];
int i = 0;
while(arr[i++]=s[i]);
array = split_str(arr,array, sep);
i = 0;
while(array[i]){
node->tuple->fields[i] = (char *)malloc((strlen(array[i])) * sizeof(char));
if(!node->tuple->fields[i]){
printf("Erro ao alocar memoria em node_create() para node->tuple->fields[i]\n");
return NULL;
}
node->tuple->fields[i] = array[i];
// printf("array[i]=%s\n",array[i]);
// printf("node->tuple->fields[i]=%s\n",node->tuple->fields[i]);
i++;
}
node->tuple->n_fields = i;
node->tuple->timestamp = 0L;
node->next = NULL;
return node;
}
char** split_str(char writablestring[],char **array, const char *sep ){
array = malloc(strlen(writablestring) + 1);
if(! array){printf("Erro ao alocar memoria para o array em split\n"); return NULL;}
char *token = strtok(writablestring, sep);
int i=0;
while(token != NULL)
{
array[i] = malloc(strlen(token)+1);
if(!array[i])
return NULL;
array[i] = token;
token = strtok(NULL, " ");
i++;
}
return array;
}
int main(int argc, char** argv)
{
void * n_data = "hello 123 ploc";
struct node_t * node = node_create(n_data);
printf("node->num_fields=%d\n", node->tuple->n_fields);
int i=0;
while( node->tuple->fields[i] ){
printf("node->tuple->fields[%d]=%s\n",i,node->tuple->fields[i]);
i++;
}
return 0;
}
End code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
split_str()
函数返回指向writablestring
的指针,它是node_create()
中的数组arr
。然后,您将这些指针复制到node->tuple->fields[i]
中 - 但arr
数组在node_create()< 之后将不存在/code> 函数退出 - 因此这些指针将不再有效。相反,您需要将返回的字符串复制到您分配的内存中(这也展示了如何使用
for()
循环代替while()
,并且您还需要释放在split_str()
中分配的内存):此外,您的代码假定
split_str()
返回的数组code> 将由NULL
终止,但该函数不能确保这一点。该函数还有许多其他问题(传递给malloc()
的大小不正确、不必要的malloc()
导致的内存泄漏) - 因此您也需要修复它:(注意该
array
不需要作为参数传递 - 它会立即被覆盖,所以我把它变成了一个局部变量)。完成此操作后,您可能会注意到实际上没有理由在
split_str()
中分配array
,而只是将其内容复制到node->; tuple->fields
然后释放它。您也可以将数组node->tuple->fields
传递给split_str()
并让它直接写入其中。然后它可以返回分配的字符串数量 - 如下所示:Your
split_str()
function returns pointers intowritablestring
, which is the arrayarr
innode_create()
. You then copy these pointers intonode->tuple->fields[i]
- but thearr
array won't exist after thenode_create()
function exits - so those pointers will no longer be valid. Instead, you need to copy the returned string into the memory that you have allocated (this also shows how you can use afor()
loop in place of yourwhile()
, and you also need to free the memory that was allocated insplit_str()
):Additionally, your code assumes that the array returned by
split_str()
will be terminated by aNULL
, but the function does not ensure this. The function has numerous other problems (incorrect size passed tomalloc()
, memory leak caused by unnecessarymalloc()
) - so you need to fix it, too:(Note that
array
does not need to be passed as a parameter - it's being immediately overwritten anyway, so I turned it into a local variable).Once you've done this, you might notice that there's really no reason to allocate
array
insplit_str()
, only to copy its contents tonode->tuple->fields
and then free it. You might as well pass the arraynode->tuple->fields
tosplit_str()
and have it write directly into it. It could then return the number of strings allocated - that would look like:尝试这样的事情:
Try something like this instead: