Deleting all data in a struct array

This is a discussion on Deleting all data in a struct array within the C++ Programming forums, part of the General Programming Boards category; Hello all, im tryig to input a txt file, store it in an struct array, output that data into a ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    22

    Deleting all data in a struct array

    Hello all,

    im tryig to input a txt file, store it in an struct array, output that data into a .dat file and from then on the programm is to read from the .dat file, the thing is i no longer need the info stored in the struct.

    a ROUGH example of my code is:

    Code:
    struct staff
    	{
    		char givenname[30];
    		char surname[30];
    		char title[5];
    		char position[30];
    		char department[10];
    		char roomnum[30];
    		char phonenum[4];
    		char username[30];
    	};
    
    staff unistaff[300];
    
    //inputs
    //outputs etc etc
    
    delete[] unistaff;
    anyway comes up with the warning "[Warning] deleting array 'staff unistaff[300]'
    ive tryed many variations of delete, such as:

    delete unistaff
    delete unistaff[300];
    delete[] unistaff[300]
    delete staff
    etc.

    any help appreciated

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You have allocated the struct array in the program stack, which means, it is not dynamically gotten from the heap.

    The stack is actually part of your program (your program's definition) and exists for the life of your program. You can't change its size, larger or smaller, at runtime.

    delete and delete[] are used for getting rid of (freeing) memory you have specifically obtained at runtime. So, hopefully now you can now see, if YOU didn't get the storage, YOU can't free it.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    22
    wait, im just talking about removing all the data within it, but still keeping the struck itself for later use, so its like it was at the begining of the program.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Then just loop through it and assign null values to everything. (well, in this case, set a string of length 0 to everything, or zero out each string completely.)
    Last edited by Dino; 03-29-2008 at 08:54 AM.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    And, at the beginning of the program, since you didn't initialize it when you declared it, it has random garbage data in it. I guess you could come up with an algorithm to fill it with random garbage data...

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    22
    how do i assign a null value to a char

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    To cause a C string (a char array) to have a zero length, you put a ZERO in the first element position.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    22
    Can someone give me an exapmle, i cant seem to get this

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    What do you want to do? As has been said you cannot delete because it's not dynamically allocated. To clear the struct a simple memset would suffice or you can create a constructor in the struct that would memset the members.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    22
    Yeah i understand what ur saying, im just not sure how to do it, im first year student.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by grimuth View Post
    Yeah i understand what ur saying, im just not sure how to do it, im first year student.
    Excepting the delete[], this is C, not C++. Are you sure you don't want those character arrays to be strings?

    But if they are supposed to be char arrays, you have to do what Todd says. I'm pretty sure even first-year students learn how to wield an equal sign:
    Code:
    chararray[0] = 0; //set first position of array to zero

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you no longer need the data in the structs, just leave them. You don't need to clear out the data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Replies: 3
    Last Post: 11-22-2002, 07:08 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21