c++ - snprintf segmentation fault -


i trying understand char pointers in c. doing declaring char* pointer main , passing reference in function , modifying there. now, want print value of char in main, gives me segmentation fault. if print value in called function prints fine.

also, when try snprintf on char pointer within main, again segmentation fault not in called function.

i searched , try understand chars , pointers not able debug this.

below code comments:

#include<stdio.h>    int main(void) {     char *a;     int ret;     /* below line gives segmentation fault. */ //  snprintf(a,10,"%s","hello");     /* below line prints '(null)'.ok */     printf("before function call: %s\n",a);      ret = func(&a);     /* below line prints count of characters returned func .ok */     printf("characters written : %d\n",ret);     /* below line gives segmentation fault. */     printf("after function call: %s\n",a);     return 1; }  int func(char *b) {     int ret = 0;     /* below line prints blank. why? above prints '(null)'*/     printf("    in func-> before operation: %s\n",b);     ret = snprintf(b,10,"%s",", world");     /* below line prints ' world'. ok */     printf("    in func-> after operation: %s\n",b);     return ret; } 

let's go through functions line line.

char *a; 

here declare pointer far points nowhere.

/* below line gives segmentation fault. */ //  snprintf(a,10,"%s","hello"); 

of course does. a points "nowhere" or "anywhere", undefined behaviour. should first allocate memory 1 or other way , let a point there. can use describe.

ret = func(&a); 

here pass a's address func() - ok.

/* below line gives segmentation fault. */ printf("after function call: %s\n",a); 

a changed, not null pointer longer above, points destination nothing can read of. undefined behaviour again.

return 1; 

that means failure. better return 0 mean success.

int func(char *b) 

stop. above passed &a func. a char *, &a char **. b func accepts char *. there discrepancy leads errors.

/* below line prints blank. why? above prints '(null)'*/ printf("    in func-> before operation: %s\n",b); 

because above print a, here print b &a.

ret = snprintf(b,10,"%s",", world"); 

here write b points, , a main(). a pointer, has size 4 on 32 bit systems , size 8 on 64 bit systems. , should not abused storing strings.

printf("    in func-> after operation: %s\n",b); 

this chance; have undefined behaviour again , disturbing caller's stack frame.

let's improve code bit:

// prototype - make function known main() right calling convention used int func(char *b);  int main(void) {     char *a = malloc(100); // should adjusted depending on needs...     int ret;     /* below line no longer gives segmentation fault now. */     snprintf(a,10,"%s","hello");     printf("before function call: %s\n",a);      ret = func(a);     /* below line prints count of characters returned func .ok */     printf("characters written : %d\n",ret);     printf("after function call: %s\n",a);     free(a); // alloc'ed it...     return 0; // didn't notice going wrong... }  int func(char *b) {     int ret;     printf("    in func-> before operation: %s\n",b);     // here qustion: want append or overwrite?     char * b_append = b + strlen(b);     ret = snprintf(b_append,10,"%s",", world");     printf("    in func-> after operation: %s\n",b);     printf("    in func-> appended %s\n",b_append);     return ret; } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -