values being overwritten?

Discussion in 'C' started by cky1123, Feb 2, 2007.

  1. cky1123

    cky1123 New Member

    Joined:
    Jan 17, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Feb 3, 2007
  2. cky1123

    cky1123 New Member

    Joined:
    Jan 17, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  4. cky1123

    cky1123 New Member

    Joined:
    Jan 17, 2007
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Problem solved.. Thanx... :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice