0

In the program below, 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 don't understand where exactly I am going wrong.

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 don't understand how I am 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;
}

I expect

***CK: H2 nSupport: 0

But I am getting

***CK: H2 nSupport: 1303643608
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
csavvy
  • 761
  • 4
  • 13
  • 2
    Don't spam language tags! If your question is about C, don't include tags for C++, Java or Python. – Jonathan Leffler Oct 18 '19 at 07:28
  • Noted , sorry, this is my first post , i thought stackoverflow is asking in general. – csavvy Oct 18 '19 at 07:30
  • 1
    `(CUOfS **)malloc(sizeof (**Btenders) * numOfBCtenders)` is wrong. First of all you [should not cast the result of `malloc` in C](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc); Secondly `sizeof(**Btenders)` is equivalent to `sizeof(CUOfS)`, you use one asterisk to many. – Some programmer dude Oct 18 '19 at 07:32
  • On an unrelated note, it's valid to pass a null pointer to [`free`](https://en.cppreference.com/w/c/memory/free). – Some programmer dude Oct 18 '19 at 07:35
  • SO is for asking general questions, but the tags are meant to guide other people about what the question is asking about. Since the question has nothing to do with Java, that tag was wholly inappropriate. Ditto Python. C++ and C have a more nearly symbiotic relationship — but dual-tagging with C and C++ means you need code that is explicitly in both languages (usually at least two source files, therefore), and you need to be asking about the interworking of the languages in some way. In general, use one of C and C++; if you use both, expect people to be grumpy about it. No harm this time. – Jonathan Leffler Oct 18 '19 at 07:41
  • 1
    It's a good idea to use `for` loops rather than `while` loops: `for (int count = 0; count < numOfBCtenders; count++)` rather than distributing the loop controls over 3 lines with a `while` loop. – Jonathan Leffler Oct 18 '19 at 07:43

1 Answers1

1

The way you allocate memory for Btenders is incorrect.

Btenders = (CUOfS **)malloc(sizeof (**Btenders) * numOfBCtenders)   // WRONG

sizeof (**Btenders) is the same as sizeof(CUOfS), but you need sizeof(CUOfS*) :

Btenders = (CUOfS **)malloc(sizeof (*Btenders) * numOfBCtenders)    // FIXED

Similarly for :

Btenders[counter] = (CUOfS *)malloc(sizeof (Btenders[counter]));    // WRONG

sizeof (Btenders[counter]) is the same as sizeof(CUOfS*), but you need sizeof(CUOfS) :

Btenders[counter] = (CUOfS *)malloc(sizeof (*(Btenders[counter]))); // FIXED
Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40