values being overwritten?

cky1123's Avatar, Join Date: Jan 2007
Go4Expert Member
Hi all,

I am trying to set up a struct with some char inside and dynamically allocating these structs. However, I find that I am getting some funny char before i even put anything in my array.. Can someone plz help and tell me what is wrong?

I can't seem to get rid of those values.. I assume it might be because I'm not freeing the memory properly? I tried "free(rules)" but get error msg..

*** glibc detected *** free(): invalid pointer: 0xbfbca220 ***
Code:
==========================================
#define MAX_RULENAME 500
#define MAX_CATNAME 33
#define MAX_SID 10

struct ruleInfo {
    char name[MAX_RULENAME];
    char catgName[MAX_CATNAME];
    char sid [MAX_SID];
}
...
//say ruleCounter is retrieved and the value is 6000
ruleCounter = 6000;
struct ruleInfo *rules[ruleCounter];
for (i=0; i<ruleCounter; i++){
    rules[i]= (struct ruleInfo *) malloc (sizeof (struct ruleInfo));
    printf("rules[%i]->catgName=%s\n",i,rules[i]->catgName);
    printf("rules[%i]->sid=%s\n",i,rules[i]->sid);
}
.... 
free(*rules);
================

Last edited by shabbir; 3Feb2007 at 07:11.. Reason: Code formating.
0
cky1123's Avatar, Join Date: Jan 2007
Go4Expert Member
Sorry everyone, I just realized malloc will just return a memory space for me to use, but it may contain things previously used. So, if i want to make sure it is cleared.. i could use memset(), correct ? But how would i use memset on an array of struct? any help or suggestions would be greatly appreciated. Thanks in advance.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Malloc has no idea what you want the memory for. It simply allots you the number of bytes that you asked for. You can memset that same number of bytes. You may also use calloc, which initializes the bytes to zero.
0
cky1123's Avatar, Join Date: Jan 2007
Go4Expert Member
Problem solved.. Thanx...