Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two-D char array and resizing

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
I realize that this method is not really
optimal.
Better would be a simple linked list.

However I am curious to see if there is
anyone who has a proven method for doing this.
I have either a logic error in this code,
or am missing something basic.

char **arrayCreate(char **name, int r, int s) {
int y;
char **new = malloc(r * sizeof(char *));
if (new) {
for (y=0 ; y <= r; y++) {
new[y] = malloc(s * sizeof(char));
}
} else {
return NULL;
}
name = new;
return name;
}

Return/resize the array:
char **arrayResize(char **name, int rows, int old) {
int y;
char **cpy = malloc(rows * sizeof(char *));
printf(&quot;\nResizing at new %d and filling from %d elements\n\n&quot;,rows,old);
for (y=0 ; y <= old ; y++) {
cpy[y] = malloc(strlen(name[y]) + 1 * sizeof(char *));
strcpy(cpy[y],name[y]);
free(name[y]);
}
/* alloc trivial mem for new elements*/
for (y=old ; y <= (rows - old) ; y++) {
cpy[y] = malloc(1 * sizeof(char));
}
free(name);
return cpy;
}


And snippet of main:
sarrpt = arrayCreate(sarrpt,irows,isize);
if (sarrpt != NULL && (mydir = opendir(argv[1])) != NULL) {
while ((en = readdir(mydir)) != NULL) {
if (y > irows) {
sarrpt = arrayResize(sarrpt,(y + 10),irows);
irows = y + 9;
}
sarrpt[y] = realloc(sarrpt[y],strlen(en->d_name) + strlen(argv[1]) + 1 * sizeof(char));
if (sarrpt[y] == NULL) {
printf(&quot;Error memallocation!\n&quot;);
return 1;
}
strcpy(sarrpt[y],argv[1]);
strcat(sarrpt[y],en->d_name);
printf(&quot;%s %p\n&quot;,sarrpt[y], sarrpt[y]);
y++;
}

Better methods or references anyone?
 
Thanks, but how about a C solution?
This one, even without the typo's,
and with a number of improvements falls over.

It shouldn't be that hard to copy the contents of one 2-d array to another,
free the previous memory and reassign the array pointer to the newly returned array. Unfortunately, it does seem to be beyond me at this moment.. :(
 
The funny thing is I can get a similar implementation to work fine with no errors:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void lookit(char **, int);
char **reallocArray(char **,int, int);
char **allocArray(int , int);
void Parray(char **, int);

int main(void) {
int n;
char **bogus;


bogus = allocArray(12, 20);
printf(&quot;before resize\n&quot;);
lookit(bogus,12);
bogus = reallocArray(bogus,50,12);
printf(&quot;After resize\n&quot;);
lookit(bogus,50);
for (n=12 ; n <= 50 ; n++) {
bogus[n] = realloc(bogus[n],15 * sizeof(char));
strcpy(bogus[n],&quot;it works&quot;);
printf(&quot;%s\n&quot;, bogus[n]);
}
Parray(bogus,50);
return 0;
}


char **allocArray(int y, int z) {
int p;
char **array;

array = malloc(y * sizeof(char *));

for (p=0 ; p <= y; p++) {
array[p] = malloc(z * sizeof(char));
strcpy(array[p],&quot;It's working&quot;);
}
return array;
}



char **reallocArray(char **name, int r, int p) {
int z;
char **ptr;

ptr = malloc(r * sizeof(char *));
/*if memory has been allocated*/
if (ptr) {
for (z=0 ; z <= p ; z++) {
ptr[z] = malloc(strlen(name[z]) * sizeof(char));
/*got memory for array col at row z*/
strcpy(ptr[z],name[z]);
printf(&quot;%s to %s\n&quot;, ptr[z], name[z]);
/*free the string*/
free(name[z]);
}
} else {
printf(&quot;malloc() failed, quitting\n&quot;);
exit(1);
}
/*alloc the difference fs&g*/
for (z = p ; z <= r ; z++) {
printf(&quot;Working on %d\n&quot;, z);
ptr[z] = malloc(18 * sizeof(char));
strcpy(ptr[z],&quot;it does work&quot;);
printf(&quot;%d\n&quot;, z);
}
/*return the copied array*/
free(name);
return ptr;
}

void lookit(char **array, int y) {
int x;

for (x=0 ; x <= y ; x++) {
printf(&quot;\n%p %d\n&quot;, *array++);
}
}

void Parray(char **arr, int p) {
int y = 0;

while (y <= p) {
printf(&quot;\n%3s %5d\n&quot;, arr[y], y);
y++;
}
}

But when reading the returned strings from the original and readdir either my error check fails or it returns some
garbage or null string and I'm not catching it.

Oh well linked list time I guess.

Thanks.
 
Hi

You can use realloc to increase the size of the array of pointers too.
The contents are automatically transferred to the newly allocated memory.

But in case you are decreasing you should make sure that the char arrays are freed before you reduce the size as it would lead to memory leaks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top