Thread: Problem allocating memory.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Question Problem allocating memory.

    I'm having a problem with my app, it crashes when I try to allocate some memory. What I use to allocate the memory is
    Code:
    entpairs[i].key = (char*) (MAX_ENTITY_KEYLENGTH*sizeof(char));
    entpairs is defined as a entpairs_t
    Code:
    typedef struct
    {
    	char *key;
    	char *val;
    } entpairs_t;
    and MAX_ENTITY_KEYLENGTH is
    Code:
    #define MAX_ENTITY_KEYLENGTH	64
    Is there something wrong with my code?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    You aren't allocating anything. All you are doing is a cast of the value:

    MAX_ENTITY_KEYLENGTH*sizeof(char)

    to char*

    That's not allocation. Look up malloc() and calloc()
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Originally posted by FillYourBrain
    You aren't allocating anything. All you are doing is a cast of the value:

    MAX_ENTITY_KEYLENGTH*sizeof(char)

    to char*

    That's not allocation. Look up malloc() and calloc()
    Haha, I forgot to add the malloc, the line should be
    Code:
    entpairs[i].key = (char*) malloc(MAX_ENTITY_KEYLENGTH*sizeof(char));
    THANKS FillYourBrain.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >entpairs[i].key = (char*) malloc(MAX_ENTITY_KEYLENGTH*sizeof(char));
    The cast isn't needed for malloc and can hide errors if you forget to include stdlib.h. Also, sizeof ( char ) is redundant as it will always be 1. This would be better:
    Code:
    entpairs[i].key = malloc(MAX_ENTITY_KEYLENGTH);
    Of course, an explicit sizeof for char arrays is often used to force the type of the expression to size_t. This is done by overly anal programmers...such as myself. The following is the preferred method for doing this as it doesn't rely on the type of pointer you are allocating memory to:
    Code:
    entpairs[i].key = malloc(MAX_ENTITY_KEYLENGTH * sizeof *entpairs[i].key);
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Analysis of the allocation statement:

    Code:
    entpairs[i].key = (char*) malloc(MAX_ENTITY_KEYLENGTH*sizeof(char));
    In the code above, i don't think there can be anything wrong to the right hand side of the statement. so possible errors in the left hand side of the statement are,

    1) value of ' i ' goes out of array bound.

    2) If ' i ' isn't out of bound index then is entpairs[i] allocated memory?

    -Give more Information about your code.

    Thanks

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Originally posted by doraiashok
    Analysis of the allocation statement:

    Code:
    entpairs[i].key = (char*) malloc(MAX_ENTITY_KEYLENGTH*sizeof(char));
    In the code above, i don't think there can be anything wrong to the right hand side of the statement. so possible errors in the left hand side of the statement are,

    1) value of ' i ' goes out of array bound.

    2) If ' i ' isn't out of bound index then is entpairs[i] allocated memory?

    -Give more Information about your code.

    Thanks
    The problem is fixed, for some reason I forgot to add the malloc so it wasn't doing anything as FillYourBrain pointed to me. So everything works, thanks anyway.

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 -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Problem with usb Flash memory
    By khpuce in forum Tech Board
    Replies: 6
    Last Post: 02-26-2004, 08:54 AM