Like Tree1Likes
  • 1 Post By dmh2000

Problem allocating memory in the "good" way

This is a discussion on Problem allocating memory in the "good" way within the C Programming forums, part of the General Programming Boards category; This is what i have: Code: class coordinate; // (which i define later on, of course) coordinate*** matrix; //Now i ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    7

    Problem allocating memory in the "good" way

    This is what i have:
    Code:
    class coordinate; // (which i define later on, of course)
    
    coordinate*** matrix;
    
    //Now i try to make a x*y matrix, with values which are all coordinates.
    
    matrix = (coordinate ***) malloc (sizeof (coordinate **));
    matrix[0] = (coordinate **) malloc (sizeof (coordinate*));
    matrix[0][0] = NULL;
    Now this works as far as i know, at least it compiles. But last time i posted here something, someone said to me that i shouldn't cast mallocs and i was given this link: FAQ > Casting malloc - Cprogramming.com

    So now i try to malloc the memory the way as it should:

    Code:
    matrix = malloc (sizeof ***coordinate);
    matrix[0] = malloc (sizeof **coordinate);
    matrix[0][0] = NULL;
    But this doesn't work, it doesn't comple. THe error i get is: type name not allowed...

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by NunnoX View Post
    Now this works as far as i know, at least it compiles.
    I love your optimism.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by NunnoX View Post
    But this doesn't work, it doesn't comple. THe error i get is: type name not allowed...
    Because you are using a C++ compiler, not a C compiler. There are subtle differences. In C++ you need a cast, in C, you don't. Note we do have a C++ forum, but this isn't it.

    Also, see the next post...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    do you see that you have the '*'s on different sides of the type in your second example?
    MK27 likes this.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,211
    The other possibility (which is actually one reason that converting the return from malloc() is discouraged in C, as it allows the compiler to detect some errors) is that you have neglected to #include <stdlib.h>.

    Quote Originally Posted by NunnoX View Post
    Code:
    matrix = malloc (sizeof ***coordinate);
    matrix[0] = malloc (sizeof **coordinate);
    matrix[0][0] = NULL;
    That said, even if you get this code to compile, any usage of matrix will give undefined behaviour. The sizes are mixed up.

    Your first coding was valid, if you leave out the type conversions (assuming you don't want to access any element of matrix other than matrix[0][0]).
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-13-2008, 11:34 AM
  2. Replies: 5
    Last Post: 11-13-2007, 03:16 PM
  3. Good architecture/design needed for "sample pipeline"
    By johny145 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 10:43 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

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