c - Dynamic Character Array - Stack -
general background
i had need character array contained dynamic number of character arrays based on number of files located in specific folder. able accomplish initializing char (*fullpathnames)[max_filename_and_pathname_length]
, using fullpathnames = malloc( sizeof(*fullpathnames) * numoffiles * max_filename_and_pathname_length ) )
after know how many files function discovered( not provided). process works flawlessly.
technical limitations
i can use ansi c; using labwindows cvi 8.1, compile code. cannot use other compiler.
general supporting code
char (*fullpathnames)[max_filename_and_pathname_length]; size_t size; numoffiles = numberofuserfiles(“*.txt”, “c:\\programdata” ); fullpathnames = malloc( sizeof(*fullpathnames) * numoffiles * max_filename_and_pathname_length ) ); size = sizeof(*fullpathnames) * numoffiles; memset(fullpathnames,0,size);
quick explaination
the above code doing want. can fill array enough following code:
strcpy(fullpathnames[0],”test word”);
my question follows. able pass fullpathnames
array of pointers variable amount of character arrays method. want method able remove single character array character array @ given index. results of method when exists fullpathnames
no longer contains given character array @ index. have done passed fullpathnames method in order add strings it. don't need creating array. have part working. my question limited behavior happening when attempt modify char pointer passed remove_element method.
the following code not finished, there problems it, in process of writing discovered gap in knowledge regards how pointers work in c. not interested in being told problems are, besides problem have identified, can fix else. discovered while able modify array within method, not able replace it, , struggling find way so.
i calling method following code.
remove_element(fullpathnames,1, numoffiles);
my expectation of have original data of fullpathnames remains except index removed, copying data index + 1
, , original pointers contained within fullpathnames of course updated. since wanted shrink array attempted set array equal new array. following information explains attempts @ debugging behavior.
debugging information
the watch variables present following information enter method.
fullpathnames = xxxxxx newarray = unallocated array = xxxxxx
after fill new temporary array following happens:
fullpathnames = xxxxxx newarray = yyyyy array = xxxxxx
as exit method following happens:
fullpathnames = xxxxxx newarray = yyyyy array = yyyyy
meat of problem
i attempting modify fullpathnames
passing in pointer. tried task using realloc resulted in free pointer exception. believe understand reason happens.
i have confirmed modify pointers contained fullpathnames
printing strings contained in original block of code initialized fullpathnames
. in order fill array passed fullpathnames
argument method calls remove_element method. point working how expected work, except, can’t seem modify original variable on stack. i believe can accomplish want? if understand reason unable modify original variable on stack, might able figure out myself, what misunderstanding and/or how can accomplish in effective way? can provide more clarification question once understand, isn’t clear, can’t post question location code @ might take while so.
code in question
void remove_element( char (*array)[max_filename_and_pathname_len], int index, int array_length ) { int i; char string[max_filename_and_pathname_len]; char (*newarray)[max_filename_and_pathname_len]; int newlength = array_length - 1; size_t size; newarray = malloc( sizeof( *newarray) * newlength * ( max_filename_and_pathname_len ) ); size = sizeof( *newarray ) * newlength; memset(newarray, 0, size); ( = index; < array_length - 1; i++ ) { memcpy(string,array[i+1],max_filename_and_pathname_len); // remove last index avoid duplication strcpy( array[index], string ); } array = newarray; }
notes: max_filename_and_pathname_length = 516;
due feedback seems ultimate question not clear. understand reason can modify contents of original array passed method unable replace array array. if can fill in gap in understand can figure out solution on own.
if understand correctly, want modify fullpathnames pointer in code part initialize original array.
with declartion of fullpatchnames
char (*fullpathnames)[max_filename_and_pathname_length]
you declare pointer array of max_filename_and_pathname_length char elements. call void remove_element(...)
give copy of pointer local variable array
valid inside function. because of array = newarray;
, changes local copy of pointer inside function, not fullpathnames
.
if want change value of fullpathnames
must give pointer pointer function. prototype of remove_element
must this:
void remove_element( char (**array)[max_filename_and_pathname_len], int index, int array_length )
now array pointer pointer (one dimansional) array of char. dereferencing pointer, can change original pointer fullpathnames point new object created inside function. must modify call function remove_element(&fullpathnames,1, numoffiles);
. read array, must dereference using * operator:
memcpy(string,*array[i+1],max_filename_and_pathname_len); ... array = newarray;
warning: code produce memory leak, since loosing reference orignal object. should remove using free() function somewhere in code!
Comments
Post a Comment