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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top