Domanda

Sto avendo qualche problema con un programma che è destinato ad essere un buffer di stringa, specificamente questa funzione ha lo scopo di ripristinare il buffer con il CSTR stringa. Se CSTR è nullo quindi le esigenze di contenuto per essere riportato a un carattere vuoto '\ 0'. E sempre si blocca alla seconda serie di realloc dove è il ridimensionamento buf-> contenuti non ho idea perché. Qualsiasi aiuto sarebbe impressionante.

Lo struct:

typedef struct strbuf {
     char   *contents;
     size_t  length;  
} StringBuffer;

Si chiama da

strbuf_reset(sb, NULL)

Questa è la funzione di strbuf_reset che sta avendo il problema.

StringBuffer *strbuf_reset(StringBuffer *buf, const char *cstr)
{
if(buf == NULL)
    return NULL;

StringBuffer *tempBuf = NULL ;

if(cstr == NULL)
    tempBuf = (StringBuffer*)realloc(buf,sizeof(StringBuffer) + sizeof(char));
else
    tempBuf = (StringBuffer*)realloc(buf,sizeof(buf) + strlen(cstr)*sizeof(char));

if(tempBuf == NULL)
    return NULL;

if(cstr == NULL)
    tempBuf->contents = (char*)realloc(buf->contents,sizeof(char));
else
    tempBuf->contents = (char*)realloc(buf->contents,(sizeof(buf->contents) + strlen(cstr)*sizeof(char) + 1));

if(tempBuf->contents == NULL){
    free(tempBuf);
    return NULL;
}
buf = tempBuf;

if(cstr == NULL)
   buf->contents = '\0';
else
   strcat(buf->contents,cstr);

buf->length = strlen(buf->contents);    

return buf;
 }

Con quello che io ritengo essere le modifiche suggerite ...

StringBuffer *strbuf_reset(StringBuffer *buf, const char *cstr)
{
if(buf == NULL)
    return NULL;

StringBuffer *tempBuf = NULL ;

if(cstr == NULL)
    tempBuf = (StringBuffer*)realloc(buf,sizeof(StringBuffer) + sizeof(char) + 10);
else
    tempBuf = (StringBuffer*)realloc(buf,sizeof(buf) + strlen(cstr)*sizeof(char)+ 1);

if(tempBuf != NULL)
    buf = tempBuf;
else
    return NULL;    

if(cstr == NULL)
    tempBuf->contents = (StringBuffer*)realloc(buf->contents,sizeof(StringBuffer) + sizeof(char) + 10);
else
    tempBuf->contents = (StringBuffer*)realloc(buf->contents,sizeof(buf) + strlen(cstr)*sizeof(char)+ 1);

if(tempBuf != NULL)
    buf->contents = tempBuf->contents;
else
    return NULL;

if(cstr == NULL)
   buf->contents = '\0';
else
   strcat(buf->contents,cstr);

buf->length = strlen(buf->contents);    

return buf;
 }
È stato utile?

Soluzione

Dal momento che si sta andando a sovrascrivere il contenuto di StringBuffer, utilizzando realloc non ha senso. Non vi farà risparmiare ogni allocazione di memoria, ma invece sarà copiare i dati vecchi che si intento di sovrascrivere in ogni caso. Utilizzare malloc normale e gratuito.

Con la tua struttura originale

typedef struct strbuf {
  char   *contents;
  size_t  length;  
} StringBuffer;

set strbuf_reset BUF a CSTR. Sui rendimenti successo Buf, in caso di fallimento NULL.

StringBuffer *strbuf_reset(StringBuffer *buf, const char *cstr)
{
  if (!buf)  return NULL;      
  if (!cstr)  cstr = "";

  size_t len = strlen(cstr);
  if (len > buf->length) {
    char *new_contents = malloc(len + 1);
    if (!new_contents)  return NULL;
    free(buf->contents);
    buf->contents = new_contents;
  }
  memcpy(buf->contents, cstr, len + 1);
  buf->length = len;

  return buf;
}

Altri suggerimenti

non sembrano capire che cosa realloc fa.

Il modo in cui si dovrebbe essere pensarci (almeno per quanto va ampliare), è che si alloca un nuovo buffer, copia i vecchi dati in esso, e quindi libera la vecchio tampone .

Il vecchio puntatore è allora non valida e non dovrebbe essere sorprendente se si ottiene si blocca quando si cerca di utilizzare di nuovo in seguito.

Si dovrebbe essere assegnando il valore restituito di nuovo al vecchio puntatore immediatamente, in modo ancora punti a dati validi.

si assegnano più spazio per la stringa nel StringBuffer. Ho appena assunto buf-> contenuto si suppone punto in quel qualcosa in più allocato lo spazio. Se questo non è vero allora perché è lo spazio extra assegnato nel StringBuffer?

Se buf-> contenuti già punti in memoria che è stato assegnato per StringBuffer, il tentativo di realloc si getterà il sistema di memoria in un / blocco / situazione mucchio corrotto incidente, perché si sarebbe realloc'ing un puntatore che non è mai stato allocato .

Invece di tentare di realloc buf-> contenuti, credo che la struttura dovrebbe essere simile:

struct StringBuffer {
    size_t length;
    char contents[1];
};

Poi, invece di reallocing buf-> contenuti che basta copiare la stringa in là e il realloc per StringBuffer si prende cura di tutta la memoria.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top