I just encountered a malloc trouble in my assignment //This is my header file
struct vertex_t {
int id;
char *label;
/* A list of vertices representing incoming edges */
List in;
/* A List of vertices representing outgoing edges */
List out;
};
struct graph_t {
/* Number of vertices */
int order;
/* Numb er of edges */
int size;
Vertex vertices;
};
//We are not allowed to change the header file above. In my main file, how can i malloc the vertices in the graph?
Graph new_graph(int order) {
Graph graph;
int i;
graph=NULL;
graph=(Graph)malloc(sizeof(Graph));
assert(graph);
graph->order=order;
graph->size=0;
graph->vertices=(Vertex*)malloc((order+100000)*sizeof(Vertex));//!!!!!!!!!!!!!!!!!!!this line!!!!
for(i=0;i<order;i++){
graph->vertices[i].label=(char*)malloc(MAX_LINE_LEN*sizeof(char));
graph->vertices[i].in=NULL;
graph->vertices[i].out=NULL;
}
return graph;
}
I only can add the extreme large number in the malloc to prevent memory leak.