Thread: memory allocations probs (continue)

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    25

    memory allocations probs (continue)

    Hi,

    just wondering if I am allocation (totalsize) * (sizeof mystructarray), is it normal if it gives me segmentation fault when I am trying to access mystructarray[totalsize+1]?

    just wondering whether I ve done my memory allocation right or wrong...

    thanks,

    Ferdinand

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: memory allocations probs (continue)

    Originally posted by CyC|OpS
    Hi,

    just wondering if I am allocation (totalsize) * (sizeof mystructarray), is it normal if it gives me segmentation fault when I am trying to access mystructarray[totalsize+1]?

    just wondering whether I ve done my memory allocation right or wrong...

    thanks,

    Ferdinand
    when you allocate (totalsize * sizeof (mystruct)), you can legally access mystructarray[0] to mystructarray[totalsize-1], thats it.
    hello, internet!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    C's memory allocation routines will allocate blocks of memory of the size you you pass to the function. So

    T *mem = malloc ( totalsize * sizeof ( T ) );

    Will allocate totalsize * sizeof ( T ) bytes. This leaves room to place totalsize items that are of size T into mem. Assuming you're using it as an array of T's the array would be represented as such:

    mem[0],mem[1]...mem[totalsize-1]

    So indexing mem with totalsize or totalsize+1 is accessing memory that you don't own, which will cause a seg fault.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    thanks Prelude + Moi,

    I ve got the idea now....
    but got another problem...

    if I got this structure...

    Code:
    struct arr
    {
             int  num;
             char *word'
    } Array;
    and if I ve allocated memory for 10 * sizeof (Array), Array[0]-Array[9]

    how to free them...

    i ve tried free( Array )
    but I still can access my Array [0] - Array [9]

    if I do free( &Array[1] ) OR free ( &Array[9] ) OR free( &Array[5] )
    it gives me the same result which is seg fault when accesing any of thee Array...therefore I believe free( &Array[5] ) doesnt mean that free ing memory allocted for Array[1].

    so How is it to free the mem?

    ps: I declared Array *

    thanks,

    Ferdinand

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you do one call to malloc(), you do one call to free(). It doesn't matter if you malloc enough memory to hold 1 or 500 objects, it was still 1 call, and therefore still only needs 1 free().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    thanks Hammer,

    but that doesnt anwser my question,

    i ve tried free( Array )
    but I still can access my Array [0] - Array [9]

    if I do free( &Array[1] ) OR free ( &Array[9] ) OR free( &Array[5] )
    it gives me the same result which is seg fault when accesing any of thee Array...therefore I believe free( &Array[5] ) doesnt mean that free ing memory allocted for Array[1].

    so How is it to free the mem?

    ps: I declared Array *
    I am confuse coz free( Array ) doesnt free the memory ( I still can access them...
    and free( &Array[1] ) OR free( &Array[number between 0-9] ) does the job...but I dont think thats the proper way to do it....

    any suggestion?

    thanks,

    Ferdinand

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I still can access them
    Just because you can doesn't mean you should do so. That section of memory has now been released and will be reused, but until something overwrites what you put there, it will remain.

    This is one of the features of C, you can put a memory address into a pointer, and try to read from it. Sometimes the OS will stop you, sometimes it won't.


    Bottom line, stick with my original answer: 1 malloc == 1 free.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    Got it now,

    thanks so much for your help

    Ferdinand

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Hi Hammer,

    Does this mean that C will not always throw a seg fault for memory accessed illegally from the heap?

    -Addie

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does this mean that C will not always throw a seg fault for memory accessed illegally from the heap?
    Correct, it may happily overwrite memory that you don't own without telling you, possibly crucial OS data which will effectively kill your computer and require a reformat to fix. Granted, this is an extreme case, but I've seen it happen before, so it isn't impossible.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  3. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 06:45 PM