Thread: Help Dynamically allocating a struct

  1. #1
    Registered User
    Join Date
    Sep 2006

    Help Dynamically allocating a struct

    I am programming in GTK using C and need help solving a problem I have. I am writing a simple program that shows student records. The problem is that I would like to create "student" struct objects dynamically as needed, every time a new student is read from the file.

    I know I have to use malloc and my question is, would it be legal to declare a global pointer to the struct and use malloc to create new instances? Thanks.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    If you malloc() and overwrite the result before calling free(), you leak memory.

    Consider using a linked list.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    What's the point of using global variables when you're dynamically allocating new objects, thus overwriting the old one?
    A linked list is cool, otherwise you can use a dynamic array.
    Global variables should be avoided.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2007

    Unhappy Provide me a example

    How can i implement the above code using linked list.
    Do i need to use a pointer to the above structure
    how do i need to generate a structure array?

    I am confused

    If possible provide an example.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    What above code?

  6. #6
    Registered User
    Join Date
    Oct 2007
    Give the code we will try it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically allocating memory...
    By Junior89 in forum C++ Programming
    Replies: 28
    Last Post: 05-08-2007, 10:17 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM