Link to home
Start Free TrialLog in
Avatar of thefirstfbli
thefirstfbli

asked on

Dynamic programmig, string and malloc

char name[20];

file *smth;
smth=fopen("asd.txt","r");
....
fscanf(smth, "%s", name);
...
..

printf ( " name is : %s", name );


i want to learn how to create dynamic programming for name array ? i want to "fscanf" the string from txt and i am going to use it.. but for ex i dont know its length i want to use malloc..
ASKER CERTIFIED SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well at first fscanf with %s and no limit is asking for trouble. What happens if name is longer then 20 chars? A buffer overflow and that means an vulnerable program.

The usual way to try you undertaking is having a very buffer and fgets into it. If you do want to allocate this space you'd do it as follows

- Either read from the start of the line till the end of line and count the chars than seek back, allocate the proper space
and run fread with the found limit (however I've never seen that applied in the "wild"
- so do the following:
allocate some buffer
int some_size = 100;
char * buf = malloc(some_size);
char *pc = NULL;

Then read the line char by char
int i = 0;
do {
    c = getchar();
    if (c == EOF || c == '\n') {
           break;
    }
    if (! (i < some_size-1) ) { // - 1 for the trailing '\0'
        /* buffer to small reallocate it I propose usine *2 as new size */
     some_size *= 2;
      pc = realloc(buf, some_size);
      if (! pc ) {
          /* error handling */
          break;
      } else {
         /* can use buffer safely */
       buf = pc;
      }
     buf[i]  = c;
     }
   ++i;
} while (true);

buf[i] = '\0';

That's the idea (minus typos etc.)

Regards
Friedrich
Avatar of thefirstfbli
thefirstfbli

ASKER

thanks,

char * buf = malloc(some_size);

what about malloc ? still we say some_size ? what is the idea behind of malloc, if it is largen than some_size ?
and also cant i use array to pointer, name -->array then array --->pointer than malloc pointer ?

i want to use it like this.
malloc reserves somesize*8bits of memory, it is very dangerous changing pointers with malloc, since only char has 8 bits short have 16 bits and int have (mostly) 32 bits, this is very platform dependent. So be careful using malloc around, if you dont want to get seg faults.
you can always use:
char * buf = malloc(some_size*sizeof(char));
which can help you in much places.
about your arraws:
void *p0;
int p1[10];
int *p2;
int *p3;
int i=0;

p0=(void *)p1;
p2=malloc(sizeof(int)*10);
p3=p0
for(;i<10;i++){
p2[i]=p3[i];
//or
p2[i]=p1[i];
}

Hi,

use:

#include      <sys/types.h>
size_t      read(int fildes,void *buf,size_t nbyte);

As an example of reading each char:

...
buff=malloc(1*sizeof(char));
...
/*read until EOF*/
while(read(smth,(char *)buff,1)){
/*do something*/
}
...
this posting about malloc is nonsense. malloc has no idea about some memory * 8 bit of size. and
sizeof(char) == 1 (that's guaranteed in the standard), well by chance it happens that char usually has 8 bits. but that does not matter really.

Of course you have to provide some initial size for malloc that's a starting point and you can resize this size with realloc that it what my code shows. this read stuff is at least a bit problematic. It is used properly here but it's surely not any valid "string", because there's just room for 1 byte in the allocated space and I doubt it makes very much sense to just work on the input char-by-char if that is intended it seems to be a waste to use read (which BTW is non portable) and use fgetc instead.

Friedrich
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what jk2001 tells is an alternative. Howerver you have to keep in mind that you have
either keep the read in string somewhere
or that you reset the read position while rereading.

Regards
Friedrich