GeekPeek
GeekPeek

Reputation: 1767

Using a pointer results in a segmentation error

I have a question: I try to use the systemcall gethostname( char *name, size_t size ) but I get some results I don't understand:

char *hostname;
gethostname( hostname, size );  //results in a segmentation error
gethostname( &hostname, size ); //works fine.

Why should I use the & sign here? gethostname asks for a pointer, but hostname is already a pointer. I just got the result by accident and have no idea why the first call doesn't work. Sorry if this is a dumb question.

[EDIT] The answer is: hostname did not allocate any memory yet and therefore, the gethostname tried to access non-allocated memory, raising the segmentation error. Furthermore, the second LOC did not work legally either, it overwrote other data on the stack and therefore corrupted it. The way to use it is to first allocate some memory and then pass it to gethostname systemcall.

Thanks for your time and information guys! :)

Upvotes: 0

Views: 65

Answers (3)

micbarton
micbarton

Reputation: 21

Actual the 'works fine' line was also incorrect; it did not result in a seg fault but did write onto the stack where your pointer is actually located. In this case a seg fault did not occur because it is valid memory - but still illegal since it would corrupt any other automatic vars you would add to this program, and those vars would corrupt the name once used. Allocate real space as shown by others to fix.

Upvotes: 2

slugonamission
slugonamission

Reputation: 9642

gethostname expects you to pass a buffer for it to write in to. In C, strings are implemented as an array of characters, which can decay to a pointer to the first character in the string.

What gethostname will do is to place the hostname in the buffer you specify, until it has written size characters. Given this, you need to give it a valid buffer to write into. In your example, nothing has yet been assigned to the pointer, hence the value of the pointer is undefined, and it's pointing to somewhere random in memory. Attempting to dereference that pointer is then undefined, and thus the program segfaults.

As I say, the way to fix this is to pass in a real area of storage. Both of the following approaches are valid:

char hostname[50];
gethostname(hostname, 50); // Valid as a char[] decays to char*
// ---------- OR ----------------
char hostname*;
hostname = malloc(sizeof(char)*50); // Actually give the pointer some storage.
gethostname(hostname, 50);

It should be noted that it worked in your second case by accident. char* hostname is a pointer that is stored on the stack. &hostname will then get the address of the pointer hostname. Since hostname has some valid storage on the stack (to store the address it points to), it is possible to write to this address, but you'll start overwriting useful data on the stack. This will then corrupt the state of the program and it'll probably crash or become unstable.

Upvotes: 3

jim mcnamara
jim mcnamara

Reputation: 16389

There should be space allocated for the string hostname, unless this is in a function where hostname is a foreign string (declared in another function) as Oli points out

char hostname[128];

gethostname(hostname, sizeof hostname);
printf("My hostname: %s\n", hostname);

Upvotes: 1

Related Questions