0

Sorry if it is a too simple question. I was trying to compile some socket invoking code, and I had to use this

SOCKADDR_IN * sin;
sin.sin_addr.s_addr    = htonl(INADDR_ANY);   

The problem is that I am told that I must initialize the pointer. (Is it necessary ?) Btw, how to initialize a structure pointer ?

2 Answers 2

4

It is necessary that you allocate memory for a SOCKADDR_IN instance. You don't have to create a pointer though; you could allocate it on the stack instead

SOCKADDR_IN sin;
sin.sin_addr.s_addr    = htonl(INADDR_ANY);

then use the address operator & if you want to pass sin to a function that expects a SOCKADDR_IN*

SOCKADDR_IN sin;
sin.sin_addr.s_addr    = htonl(INADDR_ANY);
....
function_using_socket_pointer(&sin);

Update: it seems you want to use a dynamically allocated pointer. You can do this using

SOCKADDR_IN* sin = malloc(sizeof(*sin));
if (sin == NULL) {
    /* insert oom handling here */
}
sin.sin_addr.s_addr    = htonl(INADDR_ANY);
....
free(sin);
Sign up to request clarification or add additional context in comments.

1 Comment

oh thanks I know about that, but I wanted to know if there is a specific way to initialize a struct pointer ?
3

You want to declare one on the stack instead of as a pointer. A pointer is just a memory address, not the actual instance of the object.

Just change your code to

SOCKADDR_IN sin;
sin.sin_addr.s_addr    = htonl(INADDR_ANY);   

And then when you need to use it, take the address of it like this: &sin

Edit for original question answer

To initialise a pointer if you really want to would be to do this:

SOCKADDR_IN* sin = (SOCKASSR_IN*)malloc(sizeof(SOCKADDR_IN));
....use sin....
free(sin);

This will allocate you memory on the heap with the size being set to the size of the SOCKADDR_IN struct. But as said before, this is unnecessary and generally should be avoided unless you need to use this structure outside of the current stack frame (function).

5 Comments

oh thanks I know about that, but I wanted to know if there is a specific way to initialize a struct pointer ?
Edited my answer to answer that question
thank you, tell me if I am wrong but when I use & to retrieve the local variable and use it in an other function, I can control its value when <i get back to the first function that declared it. So what is the advantage to use & than allocating dynamically memory ? I suppose it is a speed issue, isn't it ?
@Salgar It'd be better practice not to cast the return from malloc. It'd also arguably be slightly more future proof if you allocated sizeof(*sin) bytes (its equivalent to sizeof(SOCKADDR_IN) now but will keep working if you ever change the type of sin in future)
@Newben Stack allocation is typically much faster than malloc. More importantly, you have to remember to call free for dynamically allocated memory; variables on the stack have their storage reclaimed automatically when they go out of scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.