Question

    

Cette question a déjà une réponse ici:

         

Je suis un problème avec la fonction realloc. J'utilise C seulement (donc pas de vecteur) avec libcURL. Le problème que je vais avoir est que je reçois l'erreur suivante (realloc (): prochaine taille non valide) le 12 itération de la fonction write_data (la fonction que je passe à Curl comme un rappel, il est appelé chaque libcurl de temps a des données à transmettre avant (les données sont transmises par blocs)).

Trace:

-Removed -

Source:

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

char * Data; //stores the data
size_t  RunningSize;

int write_data( char *ptr, size_t size, size_t nmemb, void *stream )
{
    size_t ThisSize = (size * nmemb); //Stores the size of the data to be stored
    size_t DataLen = strlen( Data ); //length of the data so far

    RunningSize = (RunningSize + ThisSize ); //update running size (used as new size)

   Data = realloc( Data, RunningSize ); //get new mem location (on the 12th iteration, this fails)
   strcat( Data, ptr); //add data in ptr to Data

    return ThisSize; //the function must return the size of the data it received so cURL knows things went ok.
}

int main( )
{
  CURL *curl;
  CURLcode res;
  const char * UserAgent = "";

  Data = malloc(1); //so realloc will work
  RunningSize += 1;

  curl = curl_easy_init();
  if(curl)
  {
    curl_easy_setopt( curl, CURLOPT_NOBODY, 0 );
    curl_easy_setopt( curl, CURLOPT_URL, "http://www.google.co.uk/" );
    curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt( curl, CURLOPT_USERAGENT, UserAgent );
    curl_easy_setopt( curl, CURLOPT_HEADER, 1 );

    //preform request.
    res = curl_easy_perform(curl);

    //output the data (debugging purposes)
    puts( Data );

    //cleanup
    curl_easy_cleanup(curl);
    free(Data);
  }

  return 0;
}

Merci à l'avance,

Était-ce utile?

La solution

Les données transmises pour write_data() est pas nécessairement nul à terminaison; Voilà pourquoi il vous indique le nombre d'octets.

Cela signifie que vous ne pouvez pas utiliser strcat(). Son utilisation est en cours d'exécution de la fin du tableau et corrompant les structures de données utilisées par malloc / realloc, d'où l'erreur.

Votre write_data() doit utiliser memcpy() à la place, comme suit:

int write_data( char *ptr, size_t size, size_t nmemb, void *stream )
{
    size_t ThisSize = (size * nmemb); //Stores the size of the data to be stored
    size_t DataLen = RunningSize; //length of the data so far

    RunningSize = (RunningSize + ThisSize ); //update running size (used as new size)

    Data = realloc( Data, RunningSize ); //get new mem location (on the 12th iteration, this fails)
    memcpy((char *)Data + DataLen, ptr, ThisSize); //add data in ptr to Data

    return ThisSize; //the function must return the size of the data it received so cURL knows things went ok.
}

Vous aurez également besoin de Initialiser RunningSize à 0, pas 1. Vous pouvez Data à NULL initialiser -. Passant NULL à realloc() est autorisé (et en fait se comportent comme malloc())

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top