Thread: Memory Allocation issues

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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 or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, 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, 01:52 PM
  3. Memory allocation
    By Storm in forum C Programming
    Replies: 13
    Last Post: 05-19-2004, 08:47 AM
  4. Memory Allocation
    By Strut in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 05:15 PM
  5. memory allocation
    By rotis23 in forum C Programming
    Replies: 6
    Last Post: 10-04-2002, 03:49 PM