C Pointer Confusion

I want to save the string in memory and read it later:

$$->desc.constant->base.id = (char*)malloc(200); sprintf($$->desc.constant->base.id, "%f", $1); printf("->%s\n", $$->desc.constant->base.id); //LINE A printf("->%i\n", $$->desc.constant); //LINE B //SOME OTHER CODE //Then, later on in a function call: printf("%i", expr->desc.constant); // LINE D printf("%s", expr->desc.constant->base.id); // LINE C 

Although line B and line D show the same address, printf in line C fails with a segmentation error. What am I missing?

Any help would be really appreciated!

+3
source share
4 answers
 printf("->%i\n", $$->desc.constant); //LINE B 

This is not true. As long as you show the string to the point that constant is actually a pointer, you cannot treat it as if it were of type int . They do not have the same size and alignment. Use the format used for void* . It will correctly output memory addresses:

 printf("->%p\n", (void*)$$->desc.constant); //LINE B 
+9
source
  • always check malloc return value.
  • sprintfsnprintf
  • "%f""%.*g"

Here is an example:

 /** $ gcc print_number.c -o print_number */ #include <assert.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { const char* number_format = "%.*g"; const int ndigits = 15; assert(ndigits > 0); const int maxlen = ndigits + 8 /* -0.e+001, Infinity */ + 1 /* '\0' */; char *str = malloc(maxlen); if (str == NULL) { fprintf(stderr, "error: malloc\n"); exit(1); } double number = 12345678901234567890.123456789012345678901234567890; /** `number = 0/0` crashes the program */; printf("number: %f\t", number); int len_wouldbe = snprintf(str, maxlen, number_format, ndigits, number); assert(len_wouldbe < maxlen); printf("%s\n", str); return 0; } 

Output:

 number: 12345678901234567000.000000 1.23456789012346e+19 
+1
source

Maybe between the time of the two parts of the code you have a free d line?

 $$->desc.constant->base.id = (char*)malloc(200); sprintf($$->desc.constant->base.id, "%f", $1); printf("->%s\n", $$->desc.constant->base.id); //LINE A printf("->%i\n", $$->desc.constant); //LINE B //SOME OTHER CODE // which happens to do free($$->desc.constant->base.id); printf("%i", expr->desc.constant); // LINE D printf("%s", expr->desc.constant->base.id); // crash 
0
source

Given that the program produces a segmentation error, I think the problem is most likely that the structure indicated (indicated) by expr->desc.constant was reused because the space was allocated, or perhaps the space never was not really allocated to everything.

The code shows various expired sins, such as using sprintf() instead of snprintf() and free allocation of 200 bytes for a string representation of a floating point number. (You are unlikely to need so much space, and if you do, you will most likely need to have at least 100 more digits than yours, since the range of exponential floating point numbers is usually +/- 308, and the only reason which you'd need 200 characters to allow incredibly large or incredibly small numbers.)

You showed that $$->desc.constant points to the same place, but you did not specify how this space is allocated. Then you allocate the string space in $$->desc.constant->base.id without highlighting the space for base .

0
source

All Articles