Thread: pointer to an array in a structure

  1. #1
    Registered User
    Join Date
    Apr 2005

    pointer to an array in a structure

    Hi Guys!
    I need some guidance. How do I allocate a dynamic array in a structure? I am doing something like this

    struct strArray {
    	int size;
    	int position;
    	int *byteArray;
    And then initializing array like this

    int i = 32;
    strArray *source;
    strArray src;
    source = & src;
    source->byteArray = new int[i];
    But when I used boundschecker to detect memory leaks (the program compiles and runs fine) I get a memory leak statement on

    source->byteArray = new int[i];
    I am assuming I am doing something wrong here, because i then assign values to this array like

    source->byteArray[0] = 30;
    Again, any help and any comments on my style etc will be greatly appreciated as I am just starting out in this.

    Thanks in advance


    I have searched online and any link on a thread or online reference that will answer my question will also be helpful.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well until you do
    delete [] source->byteArray;
    Boundschecker will think it is still in use, and if you've exited the program then that's a memory leak.
    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
    Apr 2005
    yup, that works!! thanks Salem! I sometimes think there are no bounds to human (my) stupidity But I console myself saying that i feel same way when I am starting out on new stuff and ask questions to experts. Thanks again!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM