Question about memory allocation when initializing char arrays in C/C?

Up vote 0 down vote favorite share g+ share fb share tw.

Before anything, I apologize if this question has been asked before. I am programming a simple packet sniffer for a class project. For a little while, I ran into the issue where the source and destination of a packet appeared to be the same.

For example, the source and destination of an Ethernet frame would be the same MAC address all of the time. I custom-made ether_ntoa(char *) because Windows does not seem to have ethernet. H like Linux does.

Code snippet is below: char *ether_ntoa(u_char etheraddrETHER_ADDR_LEN) { int i, j; char eout32; for(i = 0, j = 0; I > 4; eoutj++ = etheraddri & 0xF; eoutj++ = ':'; } eoutj++ = etheraddri >> 4; eoutj++ = etheraddri & 0xF; eoutj++ = '\0'; for(i = 0; I Is this incorrect? Thanks! Carlos Nunez c++ c string arrays char link|improve this question edited Apr 7 '10 at 23:15R Samuel Klatchko33k43173 asked Apr 7 '10 at 22:50Carlos Nunez1.

Can you please clarify the problem which you are facing – Yogesh Arora Apr 7 '10 at 22:55 Sounds like a memory leak. A better design would be to let the caller allocate a buffer, which it would pass along with the buffer's size into your custom function. – Dan Olson Apr 7 '10 at 22:57.

The problem with the former way is that conceptually incorrect you couldnt return a pointer to a local variable, bc local variables lives on stack you have two aproachs 1) pass by parameter the buffer (eout) and its size, and then populate it inside the function 2) allocate the buffer (eout) inside the function, a give the invoquer the responsability to deallocate the buffer once its done with it. Regards.

As others have pointed out, you can't return a pointer to an object with automatic storage duration - when eout goes out of scope, it no longer exists. GCC actually warns you about this: ether_ntoa. C: In function ‘ether_ntoa’: ether_ntoa.

C:26: warning: function returns address of local variable The usual way to achieve the desired result is to have the caller responsible for allocating the destination. For example: int ether_ntoa(unsigned char etheraddrETHER_ADDR_LEN, char *dest, size_t len) { return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned)etheraddr0, (unsigned)etheraddr1, (unsigned)etheraddr2, (unsigned)etheraddr3, (unsigned)etheraddr4, (unsigned)etheraddr5); } (Note also that your handcoded conversion routine can be replaced with a simple snprintf() call). You'd call it like so: char eout32; ether_ntoa(etheraddr, eout, sizeof eout); /* Converted address is now in eout */ The ether_ntoa() function on Linux uses a different approach - it declares the buffer in the function as static.

If you did that, then your eout would live for the life of the program, so you can return a pointer to it. The downside is that there is only one eout - each time you call ether_ntoa, it will overwrite the previous one.

Your problem was that when you declare char eout32 inside a function, the memory allocated to that array is on the stack. After your function returns, the stack frame is popped, and the memory address formerly assigned to the array can be reassigned to other stack variables. As you have figured out yourself, the proper way to fix it is to malloc() the space for the array so that it persists after the function returns.

Just remember to free() your array after you are done so you don't have a memory leak. If you are using C++, you can use boost::shared_array instead so you don't have to manually free the memory. boost.org/doc/libs/1_42_0/libs/smart_ptr....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions