Thread: mysterious shared memory

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    mysterious shared memory

    This is my program:
    Code:
    char* arg_list[7];
    
        for(int j=0; j<7; j++) 
            arg_list[j] = new char(100);
    ................
    string temp;
        vector<string> files;
        if(d) 
            while(dir=readdir(d)) {
                temp = dir->d_name;
                if(temp.length()>=4) {
                    temp = directory + temp;
                    files.push_back(temp);
                }
            }
    ................
    strcpy(arg_list[0],"gconftool-2");
        strcpy(arg_list[1],"-t");
        strcpy(arg_list[2],"string");
        strcpy(arg_list[3],"-s");
        strcpy(arg_list[4],"/desktop/gnome/background/picture_filename");
        int child;
                
        while(1) {
            for(int i=0; i<files.size(); i++) {
    
    
    
                //this is the problem
                strcpy(arg_list[5],files[i].c_str());
                arg_list[6] = NULL;
                strcpy(arg_list[4],"/desktop/gnome/background/picture_filename");
                //end of this
    
    
    
                for(int k=0; k<6; k++) cout << arg_list[k] << endl;
                spawn("gconftool-2", arg_list);
                wait(&child);
            }
        }
    The program should behave like this:
    gconftool-2
    -t
    string
    -s
    /desktop/gnome/background/picture_filename
    /home/knight/wallpaper/grotto.jpg

    But I got this behaviour:
    gconftool-2
    -t
    string
    -s
    /desktop/gnome/background/picture_filename
    ackground/picture_filename

    If I comment this statement:

    Code:
    strcpy(arg_list[4],"/desktop/gnome/background/picture_filename");
    so that my source code like this:

    Code:
                //this is the problem
                strcpy(arg_list[5],files[i].c_str());
                arg_list[6] = NULL;
     //         strcpy(arg_list[4],"/desktop/gnome/background/picture_filename");
                //end of this
    
                for(int k=0; k<6; k++) cout << arg_list[k] << endl;
    my program behave like this:
    gconftool-2
    -t
    string
    -s
    /desktop/gnome/b/home/knight/wallpaper/grotto.jpg
    /home/knight/wallpaper/grotto.jpg


    Could u explain this stupid behaviour of arg_list[4] and arg_list[5] that share the same memory?
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > arg_list[j] = new char(100);
    Check your filename lengths - this looks like a fairly typical buffer overrun problem

    > arg_list[6] = NULL;
    This is a memory leak - you allocated it with new, use delete to return it to free memory
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    > arg_list[6] = NULL;
    This is a memory leak - you allocated it with new, use delete to return it to free memory

    Thanx for this advice. I'll keep in my mind.

    NOw I solved problem.
    > arg_list[j] = new char(100);
    it should be like this:
    arg_list[j] = new char[100];

    I forgot that char(100) means that I only allocated for one char and assigned the value of arg_list[j] to 100.

    But hey, if only one char allocated, why arg_list[0], arg_list[1], arg_list[2] and arg_list[3] do not encounter the problem like arg_list[4] and arg_list[5] do?

    Weird, huh???
    Last edited by gandalf_bar; 05-03-2004 at 04:38 AM.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > why arg_list[0], arg_list[1], arg_list[2] and arg_list[3] do not encounter the problem
    Most memory allocators do not actually allocate down to 1 byte - they have a minimum size of say 16 bytes. This is to reduce fragmentation when you're doing lots of allocating and freeing.

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        char    *a, *b, *c;
        a = new char;
        b = new char;
        c = new char;
        cout << "a= " << static_cast<void*>(a) << endl;
        cout << "b= " << static_cast<void*>(b) << endl;
        cout << "c= " << static_cast<void*>(c) << endl;
        return 0;
    }
    
    a= 0x8049b90
    b= 0x8049ba0
    c= 0x8049bb0
    On my machine, "adjacent" memory allocations of a single char are actually 16 bytes apart.

    This has two 'effects'
    1. the unused dead space at the end of your actual allocation which is added as padding by the allocator can apparently be used without any fault ever being detected in your code. But it is still wrong to use it non the less. Port your code to a different OS/Compiler, and the padding could change (or disappear), and your code breaks.
    Debug allocators fill this padding with a pattern which can detect if you step on it, but that is not normally enabled.

    2. lots of allocations of small memory blocks use up memory very quickly, if there is like a 20+ byte overhead on allocating a single char. If you're allocating large numbers of small objects, you might be better off allocating an array of them and managing a pool of that object yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Thanx Salem. My teacher never teach me that!!!!
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Share segment
    By ch4 in forum C Programming
    Replies: 7
    Last Post: 12-30-2008, 02:19 AM
  2. RE: client/server shared memory problem
    By hampycalc in forum C Programming
    Replies: 0
    Last Post: 03-10-2006, 02:26 PM
  3. shared memory can not read value over 255
    By jbsloan in forum C Programming
    Replies: 4
    Last Post: 04-03-2005, 11:56 AM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:15 PM