Question

I'm having a problem with my program: I keep getting segmentation fault on a sprintf function and i can't get why,the buffer is large enough and i THINK to be passing the pointer correctly,i just can't figure out why it won't work.

Here's the code:

Calling:

char dataBuff[100];
//same error with char *dataBuff=malloc(sizeof(char)*100);
//those vars were declared before
int tmpData[5]={TID,i,JobList[i].Num1,JobList[i].Op,JobList[i].Num2};
//here's the function that return the sigsegv error
BuildCMD(CALC,tmpData,0.f,dataBuff);

BuildCMD code:

int BuildCMD(enum CMD cmd,int *values,float Res,char *dataBuff)
{
     switch(cmd)
     {//........
         case CALC:
         {
              //this line cause the error,it's just a formatted parameters list
              //note:same error with just 
              //sprintf(dataBuff,"abc");
              spritf(dataBuff,"0*;%d;%d;%d;%d;%d;%d;%.5f|\n",cmd,values[0],values[1],values[2],values[3],values[4],Res);
              break;
         }
      //........
    }
}

I'm pretty sure that the problem is "dataBuff" since even if i use sprintf to try to store a normal constant string it gives me the same error. It's just...i can't figure out what i'm doing wrong. Thanks in advance.

EDIT:problem solved as function header:

int BuildCMD(enum CMD cmd,int *values,float Res,char dataBuff[100])

as sprintf call:

sprintf(&dataBuff,"0*;%d;%d;%d;%d;%d;%d;%.5f|\n",cmd,values[0],values[1],values[2],values[3],values[4],Res);
Was it helpful?

Solution

For BuildCMD() the parameter dataBuff is a pointer. But you have declared dataBuff as array of pointers! if you have char dataBuff[100]; then you can use BuildCMD(CALC,tmpData,0.f,dataBuff);

OTHER TIPS

Given this function prototype:

int BuildCMD(enum CMD cmd,int *values,float Res,char *dataBuff)

This call does not make sense:

char *dataBuff[100];
BuildCMD(CALC,tmpData,0.f,dataBuff);

You see how dataBuff's type is wrong? Do you not have compiler diagnostics enabled? You should, because the compiler would have caught this error right away, with no need to run it to find out it's wrong.

A SIGSEV can occur when trying to dereference a pointer that has an invalid value. In particular, 0 is an invalid value for most environments. The printf must deference your value array, so it might be that you are assigning an invalid value to it.

You are allocating an array of char pointers instead of an array of chars, I'm pretty sure that the compiler complains about passing a char** as a char* in the call to BuildCMD.

Anyway, you should be using asprintf anyway (I think, it's in the C11 standard) because fixed buffer sizes like this spell doom on future program correctness (you needs will grow and your buffer will not, until somewhere in the distant future, your program will explode...).

Edit: A correct version would be

//Don't do this!
char dataBuff[100];
int tmpData[5]={TID,i,JobList[i].Num1,JobList[i].Op,JobList[i].Num2};
BuildCMD(CALC, tmpData, 0.f, dataBuff);

int BuildCMD(enum CMD cmd, int* values, float Res, char* dataBuff) {
    sprintf(dataBuff, "whatever...");
}

This works because arrays identifiers are implicitly converted to pointers to their first element, so the dataBuff becomes a char* when passed to BuildCMD (which is also the type sprintf is expecting as its first argument). ----- This is correct, but it is not what you should be doing! -----

What you should be doing is this:

char *data = NULL;
int tmpData[5]={TID,i,JobList[i].Num1,JobList[i].Op,JobList[i].Num2};
BuildCMD(CALC, tmpData, 0.f, &dataBuff);
//do whatever you need to do with the string
free(dataBuff);

//mallocs a buffer in dataBuff, the caller is responsible to free it.
int BuildCMD(enum CMD cmd, int* values, float Res, char** pointerAdress) {
    asprintf(pointerAdress, "whatever...");
}

Note that this passes a pointer to a pointer into BuildCMD (the adress of the pointer stored in dataBuff), so that BuildCMD/asprintf can return a newly allocated pointer in the memory location pointed to by the pointer pointer (after the call, dataBuff will point to the new string).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top