我的 config.h 中有这个 struct
和函数:
typedef struct {
char *key;
char *value;
} configParam;
void loadSettings(char fileName[], struct configParam *paramsReaded[], int *length, int *statusCode){
int i;
for(i=0; i< *length; i++){
//I try with strcpy(paramsReaded[i]->key,"key_from_txt"); and neither work :/
strcpy(paramsReaded[i].key,"key_from_txt"); // HERE DONT WORK
strcpy(paramsReaded[i].value,"value_from_txt"); // HERE DONT WORK
}
}
void initialization(configParam *paramsReaded){
paramsReaded->key = (char *)malloc(sizeof(char));
paramsReaded->value = (char *)malloc(sizeof(char));
}
在 main.c 中调用函数和变量:
int main()
{ //here parameters for loadsettings and the array type "configParam"
configParam *parametersReaded[];
inicializacion(¶metersReaded);
char ruta[] = "config.txt";
int length = 5;
int statusCodeee;
loadSettings(ruta,¶metersReaded,&length,&statusCodeee);
getchar();
return 0;
}
对于strcpy
我试过各种方法,现在我觉得可能是main.c初始化调用有问题。有什么想法吗?
最佳答案
您遇到的主要问题是您没有为 struct
本身分配空间,也没有为 key
和 value
分配足够的内存>。
此外,始终检查您的分配是否成功。否则,您将面临未定义行为写入未分配给您的结构的内存的风险。
最后,当您分配内存时,您有责任跟踪它并在完成后释放它。这是您的代码的一个简短示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* provide some minimum size that will hold keys & values */
#define MAXS 32
typedef struct {
char *key;
char *value;
} configParam;
void loadSettings(char *fileName, configParam *paramsReaded, int *length, int *statusCode)
{
strcpy (paramsReaded->key,"key_from_txt"); /* or just use strdup and do away */
strcpy (paramsReaded->value,"value_from_txt"); /* with your initialization */
}
void initialization(configParam *paramsReaded)
{
paramsReaded->key = malloc (MAXS * sizeof (char));
paramsReaded->value = malloc (MAXS * sizeof (char));
/* always check your allocation succeeded */
if (!paramsReaded->key || !paramsReaded->value) {
fprintf (stderr, "error: memory allocation failed.\n");
exit (EXIT_FAILURE);
}
}
int main()
{
configParam *parametersReaded = malloc (sizeof *parametersReaded); /* allocate at least 1 */
/* always check your allocation succeeded */
if (!parametersReaded) {
fprintf (stderr, "error: memory allocation failed.\n");
return 1;
}
initialization (parametersReaded);
char ruta[] = "config.txt";
int length = 5;
int statusCodeee = 0;
loadSettings (ruta, parametersReaded,&length,&statusCodeee);
// getchar();
printf ("\n key : %s\n value: %s\n\n", parametersReaded->key, parametersReaded->value);
/* free allocated memory (note: checks are not required
if you insure your pointers have not been freed earlier
in your code.) A simple free (pointer) will suffice. */
if (parametersReaded->key) free (parametersReaded->key);
if (parametersReaded->value) free (parametersReaded->value);
if (parametersReaded) free (parametersReaded);
return 0;
}
输出
$ ./bin/initstruct
key : key_from_txt
value: value_from_txt
注意:不要转换 malloc
的结果。它只会让人很难发现错误。 paramsReaded->key = malloc (MAXS * sizeof (char));
就足够了。
检查内存泄漏/错误
如果您刚刚开始动态分配内存,请确保使用内存检查器(例如 valgrind
或 Windows 上的类似工具)确认您的内存使用情况。它们使用简单,只需通过它们运行您的代码即可。他们将确认您的内存读取和写入不涉及错误(写入超出您分配的空间)并确认您已充分释放您分配的所有内存:
$ valgrind ./bin/initstruct
==6475== Memcheck, a memory error detector
==6475== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==6475== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==6475== Command: ./bin/initstruct
==6475==
key : key_from_txt
value: value_from_txt
==6475==
==6475== HEAP SUMMARY:
==6475== in use at exit: 0 bytes in 0 blocks
==6475== total heap usage: 3 allocs, 3 frees, 80 bytes allocated
==6475==
==6475== All heap blocks were freed -- no leaks are possible
==6475==
==6475== For counts of detected and suppressed errors, rerun with: -v
==6475== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
关于c - 为什么我的结构和数组不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29619793/