Help: Resizing an Array of Pointers

This is a discussion on Help: Resizing an Array of Pointers within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm hoping somebody can help me with an issue I'm running into while trying to resize an array ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    Question Help: Resizing an Array of Pointers

    Hi everyone,

    I'm hoping somebody can help me with an issue I'm running into while trying to resize an array of pointers. Part of the program I'm writing reads in a directory from Windows and stores each folder into an array so I know how many folders are in that directory and what those folders are.

    As expected, I can't guarantee each folder has the same number of directories, so I want to have code in place that resizes the array to be larger if it hits the max initial size.

    Right now I have the following for doing this:

    *initial declaration of array*
    Code:
    int arraySize = 64;
    char* textList[arraySize];  // the array in question
    memset(textList,'\0',arraySize);  // setting all values of array to null since it won't initially be full anyway.  That way I can run normal print-outs and it will end where it should depending on it's size.
    *resizing code*
    Code:
    if(i==arraySize){
        arraySize = arraySize * 2;  // double array size
        char * textList2[arraySize];  // create temporary array
        memset(textList2,'\0',arraySize);  // set all values to null like before
    
        for(j=0;j<listSize;j++){
            textList2[j] = textList[j];  // place all items from textList into textList2
        }
        free(textList);  // free memory at textList
        *textList = *textList2;  // reassign textList to textList2
    }
    Right now the code crashes on item #85 after the increase. I've determined that something is up with my reassignment I think, because if I assign say textList2[119] to something and print out textList2[119] it displays fine. However after reassigning textList to textList2 and trying to print textList[119] it crashes.

    Can anyone help point out what I'm doing wrong? Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    258
    i think i would use malloc and realloc for that kind of resizing. just a suggestion.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    Quote Originally Posted by gemera View Post
    i think i would use malloc and realloc for that kind of resizing. just a suggestion.
    Thank you for the suggestion, but I forgot to mention that I actually tried that too but it resulted in the same issue.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    434
    I see a couple problems here:

    free(textList); // free memory at textList
    *textList = *textList2; // reassign textList to textList2

    These are done unconditionally, even if the array is not resized,
    and you're assigning a value to *textList after the memory for it has been freed.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    4
    Quote Originally Posted by megafiddle View Post
    ...and you're assigning a value to *textList after the memory for it has been freed.
    I thought reassigning the textList pointer points textList to the memory location of textList2 making the original textList memory location not needed anymore, hence it being freed. Or is that logic wrong? Perhaps I need to do a little further research on how to reassign those things.

    In any case, after ridding the free(textList) command I still get the same result haha.

    Any other ideas?

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    434
    This:

    *textList = *textList2;

    doesn't do what you want.

    *textList and *textList2 are not pointers, but are the values that textList and textList2 point to.

    I think what you are trying to do is this:

    textList = textList2;

    However, that isn't permissable. You can't assign a value to an array name. It's not modifiable.
    Right idea, you just can't do it with an array like that.

    So you actually modified the first element of textList, not the location of that array. textList still points to the memory area that you freed up.

    Better to just use pointers, which you can reassign, instead of arrays.
    Last edited by megafiddle; 05-21-2012 at 02:45 AM.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It looks to me like your main issue is that the concepts of arrays, pointers, pointers to arrays and arrays of pointers are all jumbled up in your mind. I suggest you revisit these topics either on our FAQ here or in your textbook, or any other online reference before proceeding.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. resizing array pointed to by pointer
    By sys_x in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2008, 11:06 AM
  2. Deleting and resizing a pointer array.
    By Terran in forum C++ Programming
    Replies: 9
    Last Post: 06-19-2008, 10:25 AM
  3. Dynamic array resizing
    By farklem in forum C Programming
    Replies: 3
    Last Post: 03-27-2006, 12:00 PM
  4. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-03-2005, 11:13 PM
  5. when resizing an array..
    By Brain Cell in forum C Programming
    Replies: 7
    Last Post: 03-23-2004, 07:40 PM

Tags for this Thread


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