Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
974 views
in Technique[技术] by (71.8m points)

c - Does using calloc inside function, change the pointer passed as function argument

I do not understand why the second printf loop outputs different data than the first printf loop that was done inside the function scope. Can it be that the pointer is changed somehow inside the function so that when it returns it returns a different value?

Output:

First printf inside function:
Parts TMP|01245
Parts X|40001
Parts Y|98760

Second printf outside function, in main:
It returns jiberish and not the same as when printing inside the function. I tried to fprintf so that I can quickly paste the results in here ,but then I received an uninformative call stack error.

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

void ProtocolParse_Start(int *numParts,char **parts, char *str, const char* partsDelim )
{
  int partCount = strChrCount(str,'~');
  *numParts = partCount;

  parts = (char**)calloc(partCount,sizeof(char));
  char *tempPart;


  tempPart = strtok (str,partsDelim);
  parts[0] = (char*)calloc(strlen(tempPart),sizeof(char));
  strcpy(parts[0],tempPart);


  int i =1;
  for(; i < partCount; i++)
  {
      tempPart = strtok (NULL, partsDelim);
      parts[i] = (char*)calloc(strlen(tempPart),sizeof(char));
      strcpy(parts[i],tempPart);
  }

   i =0;
  for(; i < partCount; i++)
  {
      printf ("%Parts %s
",parts[i]);
  }

}

void ProtocolParse_End(int numParts,char **parts)
{
    int i = 0;
    for (; i < numParts; i++)
        free (parts[i]);

    free (parts);
}


int main()
{
    char proto[32] = "TMP|01245~X|40001~Y|98760~";

    char **parts;
    int numParts;
    ProtocolParse_Start(&numParts, parts,proto,"~");


     int i =0;
     for(; i < numParts; i++)
     {
          printf ("%Parts %s
",parts[i]);
     }

    ProtocolParse_End(numParts,parts);

    return 0;
}

Can anyone please shed some light onto my problem. Because I am not sure what I'm doing wrong ??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The assignment of parts inside the function has no effect on the char **parts from main. In order to modify it, you need to pass a pointer to parts, and add an extra level of indirection (yes, you'd get three asterisks now).

The code that partitions the data to strings is incorrect, too: you need to allocate an array of character pointers, and then copy each token into that array individually.

void ProtocolParse_Start(int *numParts, char ***parts, char *str, const char* partsDelim )
{
  int partCount = strChrCount(str,'~');
  *numParts = partCount;

  *parts = malloc(partCount * sizeof(char*));

  char *tempPart;
  tempPart = strtok (str,partsDelim);
  (*parts)[0] = malloc(strlen(tempPart)+1);
  strcpy((*parts)[0], tempPart);

  int i =1;
  for(; i < partCount; i++)
  {
      tempPart = strtok (NULL, partsDelim);
      (*parts)[i] = malloc(strlen(tempPart)+1);
      strcpy((*parts)[i],tempPart);
  }

  i =0;
  for(; i < partCount; i++) {
      printf ("%Parts %s
", (*parts)[i]);
  }

}

I made three changes to your code:

  • Replaced calloc with malloc: you initialize every element anyway, so there is no reason to zero-fill the block
  • Removed casts in front of malloc - this is not necessary in C
  • Added one to strlen(tempPart) - you need this for null terminated strings.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...