• C

creating a socket, but gettin a seg/bus error?

Ok, after reviewing all the stuff on sockets on the web, it seemed pretty straight forwared.

so I got a socket struct:

struct Socket {
  int sockFD;      
  socketType sType;  
  struct sockaddr_in remoteSockAddr;
};

in my main I'm implementing a function that creates a server socket: void *createServerSocket(const int port)

main {

int port = 4500;

createServerSocket(port);
}

void *createServerSocket(const int port) {
        struct Socket *new_socket;

         new_socket->sockFD = socket(PF_INET,SOCK_STREAM,0);
        .....
}

thing is I get an error when I call socket(), a bus error.
If I say forget bout the createServerSocket and put that code into main(), I get a seg fault.

What wrong?

Thanks
kenbondAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
void *createServerSocket(const int port) {
        struct Socket *new_socket;
         new_socket = (struct Socket *)malloc(sizeof struct Socket));
         new_socket->sockFD = socket(PF_INET,SOCK_STREAM,0);
        .....
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kenbondAuthor Commented:
Hmm that works, but did we have to use malloc? I know what it does, but still unclear on when to use it and stuff. thanks
0
kenbondAuthor Commented:
I meant "when do we have to use malloc"
0
van_dyCommented:
struct Socket *new_socket;  <--- this just tells the compiler that new_socket is a pointer to a Socket structure.
There is no space reserved for the whole structure. if you did something like:

 struct Socket new_socket;   <-- this will reserve the space for the whole socket and you could do
new_socket.sockFD = socket(PF_INET,SOCK_STREAM,0);
However new_socket will be on the stack of createServerSocket() function, and will no longer exist after the function returns
Hence you need to allocate the space for the Server Socket in heap if you want to use the socket outside the createServerSocket() function.

another approach would be

struct Socket {
  int sockFD;      
  socketType sType;  
  struct sockaddr_in remoteSockAddr;
};

main {

int port = 4500;
Struct Socket serversocket;
createServerSocket(&serversocket, port);
}


Now you can write createserversocket() without malloc:
void *createServerSocket(struct Socket *new_socket, const int port) {
         new_socket->sockFD = socket(PF_INET,SOCK_STREAM,0);
        .....
}

So the general rule is, if you are creating a type in a function and you want to return it, use malloc
otherwise
pass to the function, the address of the location where you want the results(like in the second approach).

Hope this helps,
van_dy

0
van_dyCommented:
and yes, when a function issues malloc, it is the responsibility
of the caller  of the function of free the memory acquired using
free()
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.