Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [C] Da standard input a stringa

    Ciao a tutti,
    devo memorizzare il contenuto dello standard input in una stringa. Ovviamente non so quanto può essere lungo lo standard input e quindi inizialmente mi creo staticamente un buffer di 1000 posizioni con:
    codice:
     
    char buffer [1000];
    successivamente vengo a conoscenza della lunghezza dello stdin e vorrei ridimensionare il mio buffer, quindi mi viene in mente la realloc, ma se faccio questo:
    codice:
    buff[0]=(char*) realloc((void*)buff[0],sizeof(i+1));
    gcc si lamenta. Quale misfatto compio?

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Il peggiore che puoi immaginare. Quel buffer è sullo stack e tale rimane. Per fare quello che ti serve, alloca il buffer con una malloc.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    se faccio in questo modo:
    codice:
    char* buff;
    buff=(char*)malloc(sizeof(1000));
    while((buff[i]=fgetc(stdin))!=EOF){
    		i=i+1;		
    }
    buff[i]='\0';
    buff=(char*) realloc((void*)buff,sizeof(i+2));
    la realloc mi da problemi in memoria(invalid next size)

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    codice:
    buff=(char*)malloc(1000*sizeof(char));
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.