Thread: pointer to an array in a structure

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    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

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

    Code:
    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

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

    Code:
    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

    -Sam

    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
    Location
    The edge of the known universe
    Posts
    39,659
    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
    Posts
    7
    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!

    -Omer

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