Hi,
I wonder if anyone can help me. I have the following structures defined
I have an array of Program structs, successfully populated, and the function for adding a program is listed below
and this works happily. The code is still a little wet, as I need to implement the else case.
The function I am having problems with is the one that adds a FirewallRule struct to the array of FirewallRule structs nested in the the program struct.
The code is listed below
This appears to allocate the memory ok, but when I comment the line back in
it blows up. I think from what I am reading in the debugger, it is suggesting that newRule points to nothing. However my printfs at the top of the function are working so it cannot be pointing to nothing.
So I am doing something wrong and cannot see what. I hope someone more experienced can guide me on best practice and point out the error of my ways.
Thanks
Charlie Benger-Stevenson
Hart Hill IT Ltd
I wonder if anyone can help me. I have the following structures defined
Code:
typedef struct _FirewallRule FirewallRule;
struct _FirewallRule{
char dnsValue[1024];
char ipAddress[14]; //xxx.xxx.xxx.xxx - 15 chars
int protocol;
char user [255];
char group [255];
int action;
int ruleStatus;
};
typedef struct _Program Program;
struct _Program{
int id;
char path[1024];
char name[255];
FirewallRule * firewallRules;
int firewallRulesCount;
};
I have an array of Program structs, successfully populated, and the function for adding a program is listed below
Code:
void addProgram(char * name, char * path)
{
Program newProgram;
printf("Adding program %d %s \n", lNumPrograms ,name);
newProgram.id=lNumPrograms;
strcpy(newProgram.name,name);
strcpy(newProgram.path,path);
newProgram.firewallRules = NULL;
newProgram.firewallRulesCount = 0;
void *temp = (Program*)realloc(programs, (lNumPrograms + 1) * (sizeof(Program)));
if ( temp != NULL ) {
programs = temp;
programs[lNumPrograms] = newProgram;
lNumPrograms++;
}
else
{
//Raise an error to the user, their system may be running low on memory
}
return;
}
and this works happily. The code is still a little wet, as I need to implement the else case.
The function I am having problems with is the one that adds a FirewallRule struct to the array of FirewallRule structs nested in the the program struct.
The code is listed below
Code:
void addRuleToProgram(const int id, FirewallRule * rule)
{
int rulesCount=0;
if (rule==NULL){return;}
FirewallRule newRule = *rule;
printf("newRule dnsValue = %s \n",newRule.dnsValue);
printf("program[%d].name = %s \n",id,programs[id].name);
//Allocate some more memory to the rules list on the program record
rulesCount = programs[id].firewallRulesCount;
void *temp = (Program*)realloc(programs[id].firewallRules, (rulesCount + 1) * (sizeof(Program)));
if (temp!=NULL)
{
programs[id].firewallRules = temp;
//programs[id].firewallRules[rulesCount]=newRule;
rulesCount++;
programs[id].firewallRulesCount = rulesCount;
}
}
This appears to allocate the memory ok, but when I comment the line back in
Code:
programs[id].firewallRules[rulesCount]=newRule;
it blows up. I think from what I am reading in the debugger, it is suggesting that newRule points to nothing. However my printfs at the top of the function are working so it cannot be pointing to nothing.
So I am doing something wrong and cannot see what. I hope someone more experienced can guide me on best practice and point out the error of my ways.
Thanks
Charlie Benger-Stevenson
Hart Hill IT Ltd