• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

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
0
kenbond
Asked:
kenbond
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now