In the below program i am allocating memory for pointer to pointer properly and then allocating individual pointers and setting values properly, even though i am getting the garbage value to one of the structure member, i dont understand where exactly i am going wrong.

Request for any expert help.
Thanks in advance.

the sample output of below program is:

***CK: H2 nSupport: 0

CK: H2 nSupport: 1303643608
CK: FR2 nSupport: 0

CK: H2 nSupport: 1303643608
CK: FR2 nSupport: 0
***CK: SP2 nSupport: 0

I dont understand how am i getting the value 1303643608 , though it is set properly at the beginning.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NOK 15
#define ML 100

typedef struct sample_test_for_ties_t
{
    char sender[NOK];
    char receiver[NOK];
    char message[ML];
}sample_test_for_ties;

typedef struct CUOfS_t{
    char cK[NOK];
    char eK[NOK];
    char nK[NOK];
    char AL[ML];
    int nSupport;
}CUOfS;

CUOfS **Btenders = NULL;

sample_test_for_ties test_ties[] = {
                                    {"H2","ICY", "fmaabghijklmmcdenoopqrstuvwxyz"},
                                    {"FR2","AIY", "fmaabghijklmmcdenoopqrstuvwxyz"},
                                    {"SP2","LAY", "fmaabghijklmmcdenoopqrstuvwxyz"},
                                    {"H30","ICY", "fmaabghijklmmcdenoopqrstuvwxyz"},
                                    {"F30","AIY", "fmaabghijklmmcdenoopqrstuvwxyz"},
                                    {"W30","LAY", "fmaabghijklmmcdenoopqrstuvwxyz"},
                                   };

void InitBtenders(int numOfBCtenders)
{
    int count =0;
    if(!Btenders) 
    {
        if(Btenders = (CUOfS **)malloc(sizeof (**Btenders) * numOfBCtenders))
        {
            while(count < numOfBCtenders)
            {
                Btenders[count] = NULL;
                count++;
            }
        }
        else
        {
            printf("Malloc failed\n");
        }
    }
}

void freeBtenders(int numOfBCtenders)
{
    int count =0;

    if(Btenders)
    {
        while(count<numOfBCtenders)
       {
             if(Btenders[count]) {
                    free(Btenders[count]);
                    Btenders[count] = NULL;
             }
             count++;
       }
        free(Btenders);
        Btenders = NULL;
    }
}
void UpdateBtendersInfo(char *aContenders)
{
    static int counter =0;
    if(Btenders)
    {
        if(Btenders[counter] == NULL) {
            Btenders[counter] = (CUOfS *)malloc(sizeof (Btenders[counter]));
            if(Btenders[counter])
            {
                strcpy(Btenders[counter]->cK,aContenders);
                strcpy(Btenders[counter]->eK,"\0");
                strcpy(Btenders[counter]->nK,"\0");
                memset(Btenders[counter]->AL,0,sizeof(Btenders[counter]->AL));
                Btenders[counter]->nSupport = 0;
                counter++;
            }
            else
            {
                printf("Insufficient memory for Btender\n");
            }
       }
    }
    else
    {
        printf("Looks like memory not allocated for Btenders\n");
    }
    int count =0;
    while(count <counter && Btenders[count])
    {
        printf("***CK: %s nSupport: %d\n",Btenders[count]->cK,Btenders[count]->nSupport);
        count++;
    }
    printf("\n");
}

int main()
{
    int numOfBCtenders = 3;
    int noc =0;
    InitBtenders(numOfBCtenders);
    while(noc < numOfBCtenders)
    {
        UpdateBtendersInfo(test_ties[noc].sender);
        noc++;
    }
    freeBtenders(numOfBCtenders);

    return 0;
}

Got to know the problem!!!!,the malloc's have not been used properly....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.