Thread: how do you free an array of string?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    how do you free an array of string?

    anyone? lets say that I have an array of strings called str

    Code:
    char** str;
    how do I free all the values?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and how do you find the length of a string array?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by "length of a string array"? Do you mean the number of pointers in it? In this case, given a dynamic array, it's up to you to always always always always know how many items are in it.

    To free, you would release each string separately, if you allocated the memory for them (if they're string literals or the like, don't free those), and then free str itself.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Yes I mean the number of char* pointers in it...
    so say I have an array

    hi | my | name | is

    then the size of array is 4

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you dynamically allocated the memory, you have to know. There is no way to recreate that information.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    can I just do sizeof the array?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, because str is just a pointer, and not an actual array. So you'll get 4 (probably).
    Last edited by tabstop; 04-13-2008 at 06:14 PM. Reason: Forgot variable name

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so we should keep track when we are doing a malloc in order to find the size of the array?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by -EquinoX- View Post
    so we should keep track when we are doing a malloc in order to find the size of the array?
    You have to know the size of the array to do a malloc in the first place. But yes, you'll want to hold on to that number.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so say if I am given a char** str and someone else declared that array for me then there's no way that I can free it?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's no way you can do anything at all with it. You can't even just print all the strings out, since there's no way to know how many there are.

    Edit: We've used the words "linked list" before -- if you want/need to pass things around without keeping track of the size, that may be the way to go.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    an accesing an array in C, can we do this:

    int i = 9
    array[i]?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course you can use a variable in the subscript of an array, to access the data. You can't use that to declare an array, but I know you've seen for-loops that iterate over an array using i.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But, if as you say, you were simply passed a char** variable, then the above is unsafe because you don't know the length and can't tell if it's outside the bounds of the array or not.
    Remember that. Always pass the size along with any arrays.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM