Relate memory allocation in struct->variable

This is a discussion on Relate memory allocation in struct->variable within the C Programming forums, part of the General Programming Boards category; Hello, I have a doubt about allocating memory related to a previous allocated memory; sorry if for such bad explanations ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287

    Relate memory allocation in struct->variable

    Hello, I have a doubt about allocating memory related to a previous allocated memory; sorry if for such bad explanations (ok, and sorry for the title, I have no idea on how to resume that question), that's what I mean:

    Code:
    //compiled as C with DevCpp/MingW
    #include <conio.h>
    #include <stdio.h>
    
    struct _SAMPLE
    {
    int *list;
    };
    
    int main()
    {
    int q,MAXL;
    struct _SAMPLE *sample;
    
    MAXL=4;
    sample=(struct _SAMPLE*)malloc(sizeof(struct _SAMPLE));
    if(!sample) {return 0;}
    sample->list=(int*)malloc(MAXL*sizeof(int));
    if(!sample->list) {free(sample);return 0;}
    
    for(q=0;q<MAXL;q++)
        {
        sample->list[q]=q;
        }
    free(sample->list);
    free(sample);
    
    getch();
    return 0;
    }
    First I allocate memory for the 'sample' variable that's a type of 'struct _SAMPLE'. Inside 'sample' there's another variable that needs dynamic memory allocation; that previous code is right? I mean, ¿isthat the correct way to do it? Or should I have to allocate memory for all the data in the structure, something like

    Code:
    MAXL=4;
    sample=(struct _SAMPLE*)malloc(sizeof(struct _SAMPLE)+(MAXL*sizeof(int)));
    if(!sample) {return 0;}
    sample->list=(int*)malloc(MAXL*sizeof(int));
    if(!sample->list) {free(sample);return 0;}
    That's because I have changed from C++ to C and I need the C++ vectors, so I have started with lists and b-trees; all the samples I have seen about that uses 'non mallocated variables' (sorry for that definition ) in structs like

    Code:
    ...
    struct _SAMPLE
    {
    int dontneedmalloc[4];
    };
    ...
    struct _SAMPLE *sample;
    sample=(struct _SAMPLE*)malloc(sizeof(struct _SAMPLE));
    sample->dontneedmalloc[0]=0;
    ...
    Here I ask for a size of memory that's the size of the empty struct plus the size of the future memory allocation of the inner variable 'list'.

    So there isn't necessary to allocate memory for the inner variable to work with it because is included in the 'sizeof(struct _SAMPLE)'. But when I have to allocate memory for the inner variable, should I have to allocate memory for it also when allocate mem for the container structure? (And reallocating it when I reallocate the inner var?)

    Thank's in advance
    Niara

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You are correct in your first example.

    Allocate memory for the struct itself, and then afterwards allocate memory for each pointer in the struct that you need to allocate.

    Offtopic suggestions:

    1) I would not return 0 if there is an error since that is the convention to signify that no error has occurred.
    2) I would not use getch() or conio.h if you do not have to. You can use getchar(), getc(), or fgetc(), which all should be portable.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by Niara
    Hello, I have a doubt about allocating memory related to a previous allocated memory;
    code is right? I mean, ¿isthat the correct way to do it? Or should I have to allocate memory for
    Well I'm not going to help you much with you code today, but I am going to help you with your english.
    1. Do not confuse 'doubt' with 'question'. This drives some people nuts. You have a question. You can be in doubt about something , but that does not mean you are asking a question. Safest way is probably to never use the word 'doubt'.
    2. English questions do not start with an upside-down question mark. I know some other languages do, but not English.
    Actually I had a bit of a chuckle at 'non mallocated variables' too.

    Codewise:
    Please indent your code next time.
    Also whatever you do, make sure that you free what you malloc.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Hey, hello MacGyver, thak's for your time, help and suggestions.

    The 1st suggestion I have already know, but I have wrote a fast and simple code that could 'ilustrate' my doubt (that was the mallocation itself). For the 2nd suggestion I never realized about that, maybe because I only use the console for tests purposes and previous implementations; but from now I avoid the use of it.

    I always like to read those 'offtopic suggestions' (in response to my own questions, or in otherone's threads) because usually are tips and tricks not explained on the manuals, tutorials, howto's,...

    More thank's
    Niara

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Hey iMalc, thank's for your apport. Yes, I know that my english language is worst than my C language .
    I'll bear in mind your recommendation about 'doubt', and the synthax correction about the question mark (my natal language doesn't have initial question mark either, but in english sometimes I use it and sometimes don't).

    I understand that a well explained and well formed syntactically and gramatically question can have more help that a confusing one.

    And finally, my 'indent' inexistent style is a bad programming habit.

    Thank's for that.
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21