Thread: malloc, realloc

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    5

    malloc, realloc

    Currently, I create a processElement which has resource array & request
    array. These 2 arrays will be expanded in the later stage for storage.

    i.e.
    process = (process_type*) malloc( sizeof(process_type) );
    resource = (resource_type**) malloc( resourceNum*sizeof(resource_type*) );
    request = (request_type**) malloc( requestNum*sizeof(resource_type*) );
    (Do I malloc correctly?)

    then
    resource = (resource_type**)realloc(resource,
    growthSize*resourceNum*sizeof(resource_type*));

    after realloc, I check the resource[0] has the number I want, but
    I can't access resoruce[1], ect. It is seg fault. I am not sure
    the behaviour of realloc. As I know, resource sits inside the
    realloc may cause mem leak. Is it the cause?

    regards
    figo2476

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    5
    I tries this 2 ways. It is still seg fault.
    ...........

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Try to trim your code down into a reasonably-size snippet that demonstrates the problem, and post the actual code. (That means the stuff besides the lines you think are causing the problem.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc and realloc
    By jayfriend in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 02:25 PM
  2. To malloc or not realloc on struct members
    By gh0st in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 05:27 AM
  3. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. Need help on malloc and realloc
    By YevGenius in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:55 AM