Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   values being overwritten? (http://www.go4expert.com/forums/values-being-overwritten-t2835/)

cky1123 3Feb2007 01:18

values being overwritten?
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 ***

#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));


cky1123 3Feb2007 02:33

Re: values being overwritten?
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.

DaWei 4Feb2007 00:25

Re: values being overwritten?
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.

cky1123 4Feb2007 09:50

Re: values being overwritten?
Problem solved.. Thanx... :)

All times are GMT +5.5. The time now is 23:02.