Thread: Problem with memory allocation

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Problem with memory allocation

    I am trying to allocate a dynamic array of number elements in which each element is of a structure type. The structure is
    Code:
    struct person
    {
        char name[20]; 
        int age;
    };
    typedef struct person Person;
    Here is what I have so far:
    my main function:

    Code:
    int main( void )
    {
        int number = 3;
        Person *p = allocate( number );
      
      system("PAUSE");	
      return 0;
    }
    Here is my allocate function so far:

    Code:
    struct person* allocate( int number )
    {
        Person *people[number] = ( Person* ) malloc( sizeof( Person ) * number );
    }
    When I try to compile this, it tells me that variable-sized object cannot be initialized. I have tried everything I could think of. I am still new to C, so it might be obvious. What am I doing wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by soulwarrior89 View Post
    Code:
    struct person* allocate( int number )
    {
        Person *people[number] = ( Person* ) malloc( sizeof( Person ) * number );
    }
    When I try to compile this, it tells me that variable-sized object cannot be initialized. I have tried everything I could think of. I am still new to C, so it might be obvious. What am I doing wrong?
    Part in red is so wrong. (You are making a pointer, not an array of pointers.)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Also read the FAQ on casting malloc in C programs.
    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.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by tabstop View Post
    Part in red is so wrong. (You are making a pointer, not an array of pointers.)
    There's more wrong than that. 'person' is the name of the data type 'Person' is undefined.
    Also, it doesn't actually return anything.

    soulwarrior89: Turn your compiler's warning level up higher and pay attention to anything it tells you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    'person' is the name of the data type 'Person' is undefined.
    I think you missed:
    Code:
    typedef struct person Person;
    But yeah, it is a little strange to use struct person when one made the typedef to use Person.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What is sizeof(Person)? It is probably not what you think it is. The problem from the error is with 'Person *people[]'. As mentioned your, your allocate() is not returning anything...where is your return statement?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by slingerland3g View Post
    What is sizeof(Person)? It is probably not what you think it is.
    Why not? It seems to me that is exactly what the OP wants since Person is typedef for struct person. Ie, the function allocates n of struct person.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What is sizeof(Person)? It is probably not what you think it is.
    What do you mean by that? sizeof(Person) is probably exactly what he thinks it is.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You don't need a function to wrap a simple malloc() call. Just do it in your main():
    Code:
    Person* p = malloc( sizeof( Person ) * number );
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by Elysia View Post
    Why not? It seems to me that is exactly what the OP wants since Person is typedef for struct person. Ie, the function allocates n of struct person.
    True, I somewhat overlooked

    Code:
       char name[20]
    My goof was that in showing the size of the struct here would only indicate the size of the pointer to name and not indicate that this actually contains 20 chars.

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    Thanks for the help! I didn't realize I was doing so many things wrong.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    As others have pointed out, you really don't need a memory using struct called 'person' since you are allocating it dynamically. I hope your code ends up looking something like this:
    NOTE 'Person' is used like a basic data type once it's defined.
    Code:
    	typedef struct
    		{
    		char name[20]; 
    		int age;
    		} Person;
    
    Person * allocate(int number) {
    return (Person *)malloc(sizeof(Person) * number);
    }
    
    ...
    
    somewhere in main or another function...
    
    	Person *p = allocate(number);
    Salem, please no lecture about casting malloc(). I am using Microsoft Visual Studio, C/C++ (which can not be forcibly downgraded to just act like "C")

    It will complain about non conforming data type: if I omitted the cast the compiler would say:

    error C2440: 'return' : cannot convert from 'void *' to 'Person *'
    1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Last edited by nonoob; 07-14-2009 at 02:20 PM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remove the cast before malloc...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    I think you missed:
    Code:
    typedef struct person Person;
    But yeah, it is a little strange to use struct person when one made the typedef to use Person.
    Yeah. Damn friggin C!

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by Elysia View Post
    Remove the cast before malloc...
    No. Read my comment as to why I need it. The compiler tells me I need it. Go play one-upmanship-standards-but-never-really-programming-for-a-living with Salem. Sorry, I'm getting a bit annoyed now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM