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

  • Context: C/C++ 
  • Thread starter Thread starter James889
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the challenges of writing a function in C that prints an IP address from a struct and the implications of variable scope and memory management. It also touches on the potential transition to building a chat program in C++ using similar library functions.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an issue with a function returning a pointer to a local variable, leading to garbage output when printed in the main function.
  • Another participant explains that local variables in the function reside on the stack and may be overwritten after the function returns, suggesting the use of static variables or dynamic memory allocation as solutions.
  • A later reply emphasizes that only the buffer needs to be static, while the address pointer does not require static storage.
  • One participant proposes modifying the function to accept an output buffer as an argument, similar to the behavior of inet_ntop, and warns about the potential for dereferencing a null pointer returned by inet_ntop.
  • A final post raises a question about the applicability of C library functions in building a simple chat program in C++, seeking clarification on the compatibility of the two languages.

Areas of Agreement / Disagreement

Participants express agreement on the need to manage variable scope and memory correctly, but there is no consensus on the best approach to handle the output buffer. The discussion remains unresolved regarding the optimal implementation strategy.

Contextual Notes

Participants mention the implications of stack memory and the behavior of the inet_ntop function, but do not resolve the specifics of memory management or error handling in the provided code.

Who May Find This Useful

Individuals interested in C and C++ programming, particularly in network programming and memory management, may find this discussion relevant.

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?
 

Similar threads

  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 13 ·
Replies
13
Views
21K
  • · Replies 7 ·
Replies
7
Views
75K
Replies
8
Views
4K
Replies
8
Views
4K