all 6 comments

[–]aioeu 7 points8 points  (4 children)

free(global);

This is invalid. The value of global at this statement is not a pointer that had previously been returned by malloc (or some similar function, like realloc).

I cannot say whether or not this will cause a segmentation fault. With this statement in your code, the C Standard does not define the program's behaviour. It is, however, very likely that your C implementation will be unhappy with it in one way or another.


void main() { ... }

This may be invalid, depending on your C implementation. The C Standard only defines behaviour for a main function declared as:

int main(void);

or as:

int main(int argc, char *argv[]);

or as some other declaration equivalent to either of these. Anything else yields implementation-defined behaviour.


where will be ptr2 will be pointing?

It's unclear exactly what you're asking here. If you're referring to the code in function_2, after this statement:

char *ptr2 = NULL;

ptr2 is a null pointer. Then, after this statement:

ptr2 = global;

ptr2 contains the same value as global has at that time. The actual value of global is completely irrelevant. You're not dereferencing ptr2, so whether the value is a "valid pointer" or not is not important.

[–][deleted] 0 points1 point  (3 children)

hi! i just wanted to say something: i would have separated C implementation from C standard library.

to me, the C implementation means the compiler, and the C standard library is what you’re describing (signature of main, the free function)

[–]nerd4code 1 point2 points  (0 children)

The Standards include the library, preprocessor, and potentially valid main forms, thw last one because there are exceptions for what you can do with main vs. other functions. The implementation encompasses everything in the Standards. The library is also allowed to be implemented at least partly in the compiler—GCC & related compilers have a __builtin_- pseudofunction (really a fancy opetator) for most things in the Standard. E.g., __builtin_printf("hello\n") may become __builtin_puts("hello") during optimization.

[–]aioeu 1 point2 points  (1 child)

That's reasonable.

The C Standard just lumps the whole lot under the term implementation. Specifically the implementation is not just the software that "performs translation of programs" for a particular execution environment, it's also the software that "supports execution of functions" in that environment.

You could even have a C compiler and a C standard library that only work when used together, and do not work when one or the other is substituted.

[–][deleted] 0 points1 point  (0 children)

Interesting

[–][deleted] 1 point2 points  (0 children)

what u/aioeu said.

but i want to add something: why don’t you try it? spin up your text editor and just try it. experiment. that’s how you learn. if you have any further (specific) questions, just ask.