C/C++ What are the basics of building a chat program in C++?

  • Thread starter Thread starter James889
  • Start date Start date
AI Thread Summary
The discussion focuses on building a chat program in C++ and addresses issues with returning a string from a function that prints an IP address. The main problem arises from local variables in the function being overwritten after it returns, leading to garbage output. Suggestions include declaring the buffer as static or passing an output buffer as an argument to the function. Additionally, there is a caution about handling null pointers returned by the inet_ntop function to avoid crashes. The conversation concludes with a question about using C library functions in C++ for the chat program.
James889
Messages
190
Reaction score
1
Hi,

I am trying to get my head around this problem.

The task is to write a function that takes a pointer to a struct and simply print the address filled in. My problem is that the function does not return the string properly and i am not sure why. Printing it from within the function works.



Here is my attempt:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

char *printInet(struct sockaddr_in *my_struct) {
        char Buffer[32];
        char *address = inet_ntop(AF_INET,&(my_struct->sin_addr),Buffer,32);
        printf("%s\n",address);
        return address;
}


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


struct sockaddr_in socket, *socket_ptr;
                   socket.sin_family = AF_INET;
                   socket.sin_addr.s_addr = inet_addr("12.12.12.12");
                   socket.sin_port = htons(80);


socket_ptr = &socket;

printf("%s",printInet(socket_ptr));

return 0;
}

The printf statement prints garbage.
 
Technology news on Phys.org
The character pointer "address" and the array "buffer"in "printInet()", are local variables, they reside on the stack. When printInet() returns, the stack pointer is set back to what it was in "main()", and the local variables in printInet(), may get overwritten (which is apparently what happened in this case).

You can declare "address" and "buffer" as static, so that they are permanent, but with local names. You could also move them outside all functions, but then they would be a global variables. You could also allocate a buffer in printInet, then return the poiner to that buffer, and have the main code free that buffer after printing it out.

update - as phiby points out in the next post, only Buffer needs to be static. printInet() returns the value stored in address so there's no point in making address static.
 
Last edited:
rcgldr said:
The character pointer "address" and the array "buffer"in "printInet()", are local variables, they reside on the stack. When printInet() returns, the stack pointer is set back to what it was in "main()", and the local variables in printInet(), may get overwritten (which is apparently what happened in this case).

You can declare "address" and "buffer" as static, so that they are permanent, but with local names.

Buffer needs to be static. address doesn't.
 
phiby said:
Buffer needs to be static. address doesn't.
Even better in my opinion would be to add the output buffer as an argument to printInet, similar to how inet_ntop works.
James889 said:
Code:
char *printInet(struct sockaddr_in *my_struct) {
        char Buffer[32];
        char *address = inet_ntop(AF_INET,&(my_struct->sin_addr),Buffer,32);
        printf("%s\n",address);
        return address;
}
There's an error in this code. The function inet_ntop will return a null pointer to indicate a problem with the provided pointer to an internet address structure. Print this null pointer and you'll get a core dump. Your code should protect against the potential dereferencing of a null pointer by printf.
 
Thanks for the valuable input.

Just one last follow up question.
If i wanted to build a simple chat program in C++, can i use the same library functions that i did in C?
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top