ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hoping to get some help with a function I have written. It takes a string which is an IP address in dotted decimal notation and returns the equivalent integer value, i.e. given "1.0.0.1" it should return 16777217.
The problem I am having is memory allocation (standard n00b problem). The string gets tokenized with strtok() into its four octets, each of which is char*. I malloc memory to each octet, and strtok() the address into them. For example, when I enter the IP address "1.2.3.4", output in the debugger is as follows:
Oops. octet1 and octet2 suddenly have no value. What the...?
Finally, when I try to free the memory allocated to the octets, it seems that kernel says "No way!" and the program segfaults on free( octet2 ):
Code:
(gdb) step
105 free( octet1 );
(gdb)
106 free( octet2 );
(gdb)
Program received signal SIGSEGV, Segmentation fault.
0x00e78057 in _int_free () from /lib/tls/libc.so.6
I'm lost. I have copied and pasted this code from another function I have that needs to parse an IP address into octets, and it works fine in that function.
Looking at the above output, the addresses look right. The allocation is
Code:
octetX = ( char * ) malloc( 4 );
and it looks like they have each gotten 4 bytes.
I have posted the code at http://zaichik.org/convert_ip.c along with a few sample runs in a comment block at the bottom. Let me know if that is not kosher and I will post it here.
IIRC strtok() returns a pointer to the substring that it finds, which means that free()ing the memory for the original string (tempAddr) will make those pointers invalid - there is no guarantee that the data will remain there, and it is possible that accessing that area of memory will raise a segmentation fault.
When you call strtok() and put the return value in one of your octet pointers, you are overwriting the address stored in those pointers, not storing the octet characters in the memory that you have allocated. This creates two problems; first, it makes a memory hole, as you've allocated memory and no longer have the address to it, thus the memory cannot be freed until the process is finished. Secondly, a call to free() on one of your octet pointers will be invalid, because they are pointing to an area of memory that has not been allocated by a call to malloc() (you might get away with it for octet1 because it will likely point to the address allocated by malloc() for tempAddr - Edit: although seeing as you have already free()d that memory this can cause even more trouble).
I hope that that makes some kind of sense... I'm a bit tired today. I didn't look at your code thouroughly so there might be some other problems...
I feel stupid now. I was even using sscanf in the original code. I used strtok to break the IP into string octets, and then
sscanf to read the string octets into int octets. Original code has gone from:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.