473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

dynamic char - realloc use

hi

i expect to find 7 file on a folder... maybe less, maybe more...
i can surely put this number to 1... but i think doing some realloc
will be expensive...

after 7 files, i need to use realoc...

somebody could help me to use realloc?

Expand|Select|Wrap|Line Numbers
  1. int searchfile(char *dir, char *filename, int flength, char *ext)
  2. {
  3. DIR *pdir;
  4. printf("dir: %s\n",dir);
  5. pdir = opendir(dir);
  6. struct dirent *pent;
  7. char **fileArray;
  8. int lenFile=20;
  9. int fileCount=0;
  10. int expectedFile=7;
  11. fileArray = (char**) malloc( expectedFile * sizeof(char*) );
  12.  
  13. if (!pdir)
  14. {
  15. printf ("fct: searchfile - %s - Incapable d'utiliser
  16. opendir()\n", strerror (errno));
  17. return 1;
  18. }
  19.  
  20. snprintf(tmp_mnt_dir_www, sizeof(tmp_mnt_dir_www),"%s/%s",
  21. mnt_dir_www, "ihist.htm");
  22. if((fp = fopen(tmp_mnt_dir_www, "w"))!=NULL)
  23. {
  24. while((pent = readdir(pdir)) != NULL)
  25. {
  26. if(strncmp(pent->d_name, filename, 5) == 0 &&
  27. strcmp(pent->d_name + flength, ext) == 0)
  28. {
  29. if(expectedFile<7)
  30. {
  31. fileArray[fileCount] = (char*) malloc( (lenFile+1)
  32. * sizeof(char) );
  33. }
  34. else
  35. fileArray = realloc( (char
  36. *)fileArray,(lenFile+1)*(sizeof (char);
  37.  
  38. fileCount++;
  39. strcpy( fileArray[fileCount], pent->d_name );
  40. }
  41. }
  42. ;
  43. fclose(fp);
  44. }
  45. for(i=0;i<fileCount;i++);
  46. free(fileArray[i]);
  47.  
  48. free( fileArray );
  49. closedir(pdir);
  50. return 0;
  51. }
  52.  
thanks

Nov 14 '05 #1
2 5315

# fileArray = (char**) malloc( expectedFile * sizeof(char*) );

# fileArray = realloc( (char
# *)fileArray,(lenFile+1)*(sizeof (char);

Is that supposed to be sizeof(char*) instead sizeof(char)? That's a fourfold
difference on my machine.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
Nov 14 '05 #2


collinm wrote:
hi

i expect to find 7 file on a folder... maybe less, maybe more...
i can surely put this number to 1... but i think doing some realloc
will be expensive...

after 7 files, i need to use realoc...

somebody could help me to use realloc?

[code]
int searchfile(char *dir, char *filename, int flength, char *ext)
{
DIR *pdir;
printf("dir: %s\n",dir);
pdir = opendir(dir);
struct dirent *pent;
char **fileArray;
int lenFile=20;
int fileCount=0;
int expectedFile=7;
fileArray = (char**) malloc( expectedFile * sizeof(char*) );

if (!pdir)
{
printf ("fct: searchfile - %s - Incapable d'utiliser
opendir()\n", strerror (errno));
return 1;
}

............ code snipped *********

Ignoring the non-standard code, I see something that doesn't
look right. You have called function malloc to reserve memory
before you check pdir. This will lead to a memory leak should
pdir have value !pdir. Also, you fail to chech the return value
of malloc. The allocation could fail.

You are using dynamic allocations for a fixed size of 7 strings
of 20 chars. You might as well forego dynamic allocations, change
char **fileArray;
to char fileArray[7][20];
and you will have storage

If the insist on dynamic allocations, I would encapsulate
char **fileArray and int fileCount in a struct and write
functions that manipulates the struct.
Example,

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

typedef struct STRINGARR
{
char **String;
size_t nelem;
} STRINGARR;

/* Prototypes */
char *AddSTRINGARR(STRINGARR *p, const char *s);
void PrintSTRINGARR(STRINGARR *p, const char *s);
void FreeSTRINGARR(STRINGARR *p);

int main(void)
{
STRINGARR fnames = {NULL}; /* Emply array of file names */

/************************************************** ******
* TODO: add variables and code to search the directory *
* and for each match call function AddSTRINGARR *
* example: AddSTRINGARR(&fnames, filename); *
************************************************** ******/

/* Example */
AddSTRINGARR(&fnames,"hello.c");
AddSTRINGARR(&fnames,"GoodMorning.c");
AddSTRINGARR(&fnames,"GoodNighe.c");
PrintSTRINGARR(&fnames,"File");
FreeSTRINGARR(&fnames);
return 0;
}

char *AddSTRINGARR(STRINGARR *p, const char *s)
{
char **tmp;

if((tmp = realloc(p->String,(sizeof *tmp)*(p->nelem+1))) == NULL)
return NULL;
if((tmp[p->nelem] = malloc(strlen(s)+1)) == NULL) return NULL;
strcpy(tmp[p->nelem],s);
p->String = tmp;
return p->String[p->nelem++];
}

void PrintSTRINGARR(STRINGARR *p, const char *s)
{
size_t i;

for(i = 0; i < p->nelem; i++)
printf("%s %u. %s\n",s?s:"",i+1,p->String[i]);
return;
}

void FreeSTRINGARR(STRINGARR *p)
{
size_t i;

for(i = 0; i < p->nelem; i++) free(p->String[i]);
free(p->String);
p->String = NULL;
p->nelem = 0;
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert | last post by:
Hi, How can i resize an array of strings to add more? This is an example i just downloaded from a website: char *cpArray; int i; for (i = 0; i < 10; i++) cpArray = (char *)malloc(20 *...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Thomas Christmann | last post by:
Hi! Sorry for the weird topic, I don't know how to describe it better... I have a little problem here I can't wrap my mind around. If I do: ------------------------------------- #define DWORD...
18
by: intercom5 | last post by:
I'm writing a program in C, and thus have to use C strings. The problem that I am having is I don't know how to reallocate the space for a C string outside the scope of that string. For example:...
8
by: Steve Chow | last post by:
I have two structures; struct cordsys { int y, x, length }; struct provinces { int number; int type; struct cordsys cord;
13
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
18
by: hyperboogie | last post by:
Hello all I'm pretty new to C, so please accept my apologies in advance :-) I'm trying to allocate space for an array of pointers to strings (which are accepted as ellipses) inside a while...
4
by: David d'Angers | last post by:
hi all i need a function that returns all the file names contained in a directory since the number of files is not known in advance how should i go about to use "dynamic array" to hold the file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.