Memory Allocation issues

This is a discussion on Memory Allocation issues within the C Programming forums, part of the General Programming Boards category; I'm trying to allocate memory for an array of pointers to structures with: Code: struct onode *onodeArray[size] = (struct onode**)calloc(size,sizeof(struct ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    16

    Memory Allocation issues

    I'm trying to allocate memory for an array of pointers to structures with:
    Code:
    struct onode *onodeArray[size] = (struct onode**)calloc(size,sizeof(struct onode*));
    size is a parameter of the function this command is in, and it's simple the size (number of rows) of the array. My problem is that I'm getting this message when I try to compile my code: error: variable-sized object may not be initialized

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,747
    > struct onode *onodeArray[size]
    Use

    struct onode **onodeArray
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,526
    In addition to Salem's advice, if you want the code to compile then turn the "(struct onode **)" type conversion into "(struct onode *)". i.e. you have one two many asterixes.

    In C, it is best to remove the type conversion completely, as it is not required. If you need it then either you have forgotten to #include <stdlib.h> or you are using a C++ compiler (which does require the type conversion) OR both.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Allocation
    By Prodiga1 in forum C Programming
    Replies: 7
    Last Post: 03-01-2011, 10:13 PM
  2. Memory allocation
    By ski_photomatt in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 02:52 PM
  3. Memory allocation
    By Storm in forum C Programming
    Replies: 13
    Last Post: 05-19-2004, 09:47 AM
  4. Memory Allocation
    By Strut in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 06:15 PM
  5. memory allocation
    By rotis23 in forum C Programming
    Replies: 6
    Last Post: 10-04-2002, 04:49 PM

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