LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2005, 06:24 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Two variables sharing memory?


I was just playing around with some C code, trying to get the hang of it, and ended up with this:

Code:
#include <string.h>
#include <stdio.h>

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

        if (argc == 3) {

                // get the first argument
                char first[strlen(argv[1]+1)];
                strncpy(first, argv[1], sizeof(first)+2);

                // get the second argument
                char second[strlen(argv[2]+1)];
                int i;
                for (i = 0; i < strlen(argv[2]) && argv[2][i] != ':'; i++)
                        second[i] = argv[2][i];
                second[i] = '\0';
                printf("0: check: %s\n", second);

                char port_str[strlen(argv[2]+2)-strlen(second)];
                printf("1: check: %s\n", second);

                for (int c = 0; c < sizeof(port_str); c++)
                        port_str[c] = '\0';
                printf("2: check: %s\n", second);

                int j;
                printf("3: check: %s\n", second);
                port_str[0] = 'a';
                port_str[1] = '\0';
                printf("4: check: %s\n", second);

        } else {
                printf("wrong syntax\n");
        }
}
I wanted to make a program that takes the syntax "./prog ip1 ip2ort". The wierd this is that when I change the port_str into just an "a" the last (4th) check of the "second"-variable shows that it has changed. Here is the output:

Code:
$ gcc blahfan.c -o temp -std=c99
$ ./temp 10.11.1.11 22.222.2.22:1  
0: check: 22.222.2.22
1: check: 22.222.2.22
2: check: 22.222.2.22
3: check: 22.222.2.22
4: check: a
I want "second" to stay intact. It is done, it was perfect, but then it got messed up when I changed port_str. What am I doing wrong?
 
Old 03-31-2005, 11:47 PM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
port_str and second are pointing to the same place in memory. I'll bet that the port_str[c] = "\0" isn't executing at all, and that the length of that array is 0. I'll go see...

Last edited by puffinman; 03-31-2005 at 11:54 PM.
 
Old 04-01-2005, 12:18 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
char first[strlen(argv[1]+1)];

First of all, the length should be known at compile time to be able to determine what the length of first should be. Otherwise you need to have first and second be dynamically allocated, which will probably mean that they should be pointers to an array of char.

Second, I think you wanted [ strlen(argv[1]) + 1 ]
You are checking the length of the string starting with the second character, rather then adding one to the string length.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Are PHP session variables held in memory when you leave a PHP site? Locura Programming 11 11-16-2008 08:37 PM
sharing memory between processes spuzzzzzzz Programming 2 10-13-2004 07:18 AM
C - sharing variables between forked processes? ocularbob Programming 5 03-14-2004 03:29 AM
Help!?! RH 8 Memory Mapping -High Memory-Virtural Memory issues.. Merlin53 Linux - Hardware 2 06-18-2003 04:48 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration