What is the safest way to pass strings in C?

I have a program in C using Solaris with VERY ancient compatibility. Many examples, even here on SO, do not work, as well as a lot of code written on Mac OS X.

So, when using very strict C, what is the safest way to pass strings?

I am currently using char pointers everywhere, because of what I thought was simplicity. So I have functions that return char *, I pass them char *, etc.

I already see strange behavior, for example, like char *, when I entered the function, and then the value mysteriously disappeared or was damaged / overwritten after something simple, for example, one printf () or malloc to another pointer.

One approach to functions, which I'm sure is wrong, could be:

char *myfunction(char *somestr) {    
  char localstr[MAX_STRLENGTH] = strcpy(localstr, somestr);
  free(somestr);
  /* ... some work ... */
  char *returnstr = strdup(localstr);
  return returnstr;
}

... . - ?

, , . , , , : '

char *get_fullpath(char *command, char *paths) {
  printf("paths inside function %s\n", paths); // Prints value of paths just fine

  char *fullpath = malloc(MAX_STRLENGTH*sizeof(char*));

  printf("paths after malloc %s\n", paths); // paths is all of a sudden just blank
}
+5
3

C :

  •  
  • int, 0 , -1 . errno (, EINVAL). 
  • , "" , " out". , . 
  • ; , free free , malloc/calloc
  • const char* char*, , . , const char*
  • , , NUL, , . 
  • string/buffer (.. char*) , , , , /, ( ).

, localstr, returnstr. , . . , , , , . "undefined " .


(get_fullpath), , get_fullpath, , . , paths , . , get_fullpath, , . "" - "fullpaths", , malloced, .

2
C Coding Conventions - , C, . , , localstr returnstr, , .

+12

, . , .

,

char localstr[MAX_STRLENGTH] = strcpy(localstr, somestr);

, strcpy() localstr [], . , , .

char localstr[MAX_STRLENGTH];
strcpy(localstr, somestr);

, free() . free() , malloc(). , .

, , :

// use a prototype like this to use the same buffer for both input and output
int modifyMyString(char buffer[], int bufferSize) {
    // .. operate you find in buffer[],
    //    leaving the result in buffer[]
    //    and be sure not to exceed buffer length
    // depending how it went, return EXIT_FAILURE or maybe
    return EXIT_SUCCESS;

// or separate input and outputs
int workOnString(char inBuffer[], int inBufSize, char outBuffer[], int outBufSize) {
    // (notice, you could replace inBuffer with const char *)
    // leave result int outBuffer[], return pass fail status
    return EXIT_SUCCESS;

malloc() free() .

+4

"" ? , : , . - , , , , , , malloc, .

, , . , - , , . free'd , malloc .

:

- C. , , . , strncpy strncat strcpy strcat . : ", 60 " , , - . , , , ISBN, - , , - . .. , . , .

, , . , , C, , , - . , , , , . , , ... , ? , , ... ? , , . , . , , :

if (strcmp(getMeSomeString(),stringIWantToCompareItTo)==0) etc

. getMeSomeString , , , , , . ,

char* someString=getMeSomeString();
int f=strcmp(someString,stringIWantToCompareItTo);
free(someString);
if (f==0)
etc

, , .

, , , . , , , . , .

0

All Articles