Thread: malloc fot int* with struct

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    18

    malloc fot int* with struct

    Code:
    typedef struct serie {
        char serieName[20];
        int code;
        int rank;
        int* watchingDetails;
        int watchingnumber;
    }serie;
    
    void main()
    {
    ...
    
    
    else if (choise == 2)
            {
                if (getunif == getunify)
                    printf("Get Unify Database Details Has Been Done Already !!!\n");
                else if (tounify == unifydone)
                {
                    serie_list = getUnifyDatabaseDetails();
                    getunif = getunify;
                    for (i = 0; serie_list[i]->serieName != "\0"; i++)
                    {
                        serie_list[i]->watchingDetails = (int*)malloc(sizeof(int)*(serie_list[i]->rank + 1));
                        if (!serie_list[i]->watchingDetails)
                            exit(1);
                    }
    
    
                    for (i = 0; serie_list[i]->serieName != "\0"; i++)
                        for (j = 0; serie_list[i]->watchingDetails[j]; j++)
                            serie_list[i]->watchingDetails[j] = 0;
                }
    ...
    
    }
    
    
    serie** getUnifyDatabaseDetails()
    {
        serie** serie_list;
        serie* seriel;
        FILE* f3, *f;
        int fur = 3, i, j;
        int linesnum = 0;
        f3 = fopen("series.txt", "r");
        for (i = 0; fur != EOF; i++)
        {
            fur = fscanf(f3, " %*[^\n]\n");
            linesnum = linesnum + 1;
        }
        seriel = (serie*)malloc(sizeof(seriel) * (linesnum + 1));
        serie_list = (serie**)malloc(sizeof(serie) * (linesnum + 1));
        rewind(f3);
    
    
        for (i = 0, fur = 3; fur != EOF; i++)
        {
            fur = fscanf(f3, " %21[^,] ,%d ,%d\n", seriel[i].serieName, &seriel[i].code, &seriel[i].rank);
            if (fur != EOF)
            {
                seriel[i].watchingnumber = 0;
                seriel[i].watchingDetails = (int*)malloc(sizeof(int)*(seriel[i].rank + 1));
                if (!seriel[i].watchingDetails)
                    exit(1);
                for (j = 0; j <= seriel[i].rank; j++)
                    seriel[i].watchingDetails[j] = 0;
    
    
                serie_list[i] = &seriel[i];
            }
            else
                serie_list[i] = 0;
        }
        f = fopen("watching.txt", "w");
        fclose(f);
        fclose(f3);
        printf("Get Unify Database Details Succeeded\n");
        return serie_list;
    }
    i have a problem with the malloc for the watchingDetails and then i want to make all of them all equal to zero.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,549
    Quote Originally Posted by eleleljubraniii
    i have a problem with the malloc for the watchingDetails
    What's the problem, i.e., how does it not work?

    Quote Originally Posted by eleleljubraniii
    then i want to make all of them all equal to zero
    For the special case of zero, it might be more convenient to use calloc instead of malloc then.

    By the way, this likely to be a bug:
    Code:
    for (i = 0; serie_list[i]->serieName != "\0"; i++)
    You probably want to compare with "" instead of "\0", and furthermore you could compare i with the number of elements of serie_list in order to avoid array out of bounds access in the event that there is no serieName that meets the condition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Quote Originally Posted by laserlight View Post
    What's the problem, i.e., how does it not work?


    For the special case of zero, it might be more convenient to use calloc instead of malloc then.

    By the way, this likely to be a bug:
    Code:
    for (i = 0; serie_list[i]->serieName != "\0"; i++)
    You probably want to compare with "" instead of "\0", and furthermore you could compare i with the number of elements of serie_list in order to avoid array out of bounds access in the event that there is no serieName that meets the condition.
    Regarding comparing to ""... is that allowed in C? "", is a string literal and therefore you'd be comparing serie_list[i]->serieName (an array) with a, well, string literal/pointer to string (??)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,549
    Yes, but it means either using strcmp or directly comparing for the empty string by testing if the first character is '\0'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Quote Originally Posted by laserlight View Post
    Yes, but it means either using strcmp or directly comparing for the empty string by testing if the first character is '\0'.
    I must be misunderstanding what you meant by "You probably want to compare with "" instead of "\0"". Not really sure now

    Code:
    int main(void) {
        char str[20] = "";
        return str == "" ? 0 : 1;
    }
    
    $ clang -Wall -Wextra test.c 
    test.c:3:16: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
        return str == "" ? 0 : 1;
    $ ./a.out
    $ echo $?
    1

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,549
    Quote Originally Posted by Hodor
    I must be misunderstanding what you meant by "You probably want to compare with "" instead of "\0"".
    I meant exactly that: compare serie_list[i]->serieName with "" instead of "\0", because the intent is likely to check if serie_list[i]->serieName is an empty string, whereas "\0" is not semantically an empty string. I didn't specify more precisely how to compare because I was distracted by the possibility that no such string exists in the sequence to begin with. Since you spotted my omission, why not just state that the comparison should be done with strcmp or alternatives rather than ask if I meant that the code should literally be changed to serie_list[i]->serieName != "" when you already know the reasoning why that will still not be correct?

    If you insist:
    You probably want to compare with "" instead of "\0" by using strcmp and the like, or by directly comparing the first character with '\0', and furthermore you could compare i with the number of elements of serie_list in order to avoid array out of bounds access in the event that there is no serieName that meets the condition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Quote Originally Posted by laserlight View Post
    Since you spotted my omission, why not just state that the comparison should be done with strcmp or alternatives rather than ask if I meant that the code should literally be changed to serie_list[i]->serieName != "" when you already know the reasoning why that will still not be correct?
    I didn't think it was legal C but I was genuinely confused by what you'd written and hadn't written the test code at the time I asked the question; no other reason.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,549
    Quote Originally Posted by Hodor
    I didn't think it was legal C
    It is legal (as you can see from your test with a compiler), but that means in your post #3 you correctly worked out the ramifications of the conversion of string constant to pointer (to char) on your own (just mistaking it as illegal when it is legal but typically incorrect), which you're certainly capable of! Trust yourself!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Malloc question
    By seal308 in forum C Programming
    Replies: 3
    Last Post: 03-14-2016, 07:18 PM
  2. Struct and Malloc Help
    By VinrVanyali in forum C Programming
    Replies: 9
    Last Post: 11-14-2011, 11:36 PM
  3. Malloc the struct
    By farukyaz in forum C Programming
    Replies: 4
    Last Post: 10-17-2011, 04:24 AM
  4. another malloc of ptr in struct
    By tikelele in forum C Programming
    Replies: 7
    Last Post: 11-20-2007, 03:17 PM
  5. how to use malloc with a struct?
    By allplay in forum C Programming
    Replies: 6
    Last Post: 09-13-2006, 02:49 PM

Tags for this Thread