无法初始化结构数组

时间:2016-08-12 12:07:14

标签: c arrays struct

我尝试使用memset初始化struct类型的数组单元格。 该程序成功编译,但Valgrind对记忆这些细胞的相关内容并不满意。 注释掉的代码也不起作用。

// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

 bool myfunction (int i,int j) { return (i<j); }

struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

 int main () {
 int myints[] = {32,71,12,45,26,80,53,33};
    std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
   std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

         // using function as comp
    std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

    // using object as comp
      std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

    // print out content:
     std::cout << "myvector contains:";
      for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
      std::cout << ' ' << *it;
     std::cout << '\n';

    return 0;
  }

相关的Valgrind错误 -

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

#define MAX_DATA 512
#define MAX_ROWS 100



struct Address {
        int id;
        int set;
        char name[MAX_DATA];
        char email[MAX_DATA];
};

struct Database{
        struct Address rows[MAX_ROWS];
};

struct Connection{
        FILE* file;
        struct Database* db;
};


void die(const char* message)
{
        if(errno){
                perror(message);
        }else{
                printf("ERROR: %s\n", message);
        }
        exit(1);
}



void Database_load(struct Connection* conn)
{
        int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
        if (rc != 1)
        {
                die("Failed to load database.");
        }
}


struct Connection* Database_open(const char* filename, char mode)
 {
        struct Connection* conn = malloc(sizeof(struct Connection));
        if (!conn)
        {
                die("Memory error");
        }

        if (mode == 'c')
        {
                conn->file = fopen(filename, "w");
        }
        else
        {
                conn->file = fopen(filename, "r+");

                if(conn->file) {
                        Database_load(conn);
                }
        }

        if(!conn->file) die("Failed to open file");
        return conn;
}


void Database_create(struct Connection* conn)
{
        int i = 0;

        for(i = 0; i < MAX_ROWS; i++) {
                //struct Address addr = {.id = i, .set = 0};
                //conn->db->rows[i] = addr;
                memset(&(conn->db->rows[i]), 0, sizeof(struct Address));
                conn->db->rows[i].id = i;
        }
}




int main(int argc, char* argv[])
{
        if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");

        char* filename = argv[1];
        char action = argv[2][0];
        struct Connection* conn = Database_open(filename, action);
        int id = 0;
        if(argc > 3) id = atoi(argv[3]);
        if(id >= MAX_ROWS) die("There's not that many records.");


        Database_create(conn);

        return 0;
}                        

1 个答案:

答案 0 :(得分:1)

您的代码永远不会初始化conn->db

您需要以下内容:

conn->db = malloc(sizeof(struct Database));
if (!conn->db) 
{
    die("Memory error");
}

Database_open函数