Thread: Passing an array of strings without copying

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Passing an array of strings without copying

    Hi,

    I have two structs say:

    Code:
    struct a
    {
    int xyz;
    char aStringArray[20][80];
    };
    
    struct b
    {
    bool b;
    char ** aStringArray2;
    };
    Then I have a function that receives first struct as input and calls another function passing the second struct.

    Code:
    void exampleFunc1(struct a * mem_a)
    {
    struct b mem_b; 
    if (mem_a->xyz) mem_b.b = true;
    else mem_b.b = false;
    
    /* update mem_b.aStringArray2 using the info from mem_a*/
    
    /* should I allocate the memory or I can use the memory already allocated for mem_a ? */
    
    /* I know that mem_a will survive longer than mem_b */
    
    /* call second function passing mem_b */
    
    exampleFunc2(&mem_b);
    
    /* if I allocated memory explicitly then destroy it now as we no longer need it */
    
    /* mem_a memory is not headache of this function so let's return */
    return;
    }
    Thanks for any ideas

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you change this
    char ** aStringArray2;
    into this
    char (*aStringArray2)[80];

    Then you can do
    mem_b.aStringArray2 = mem_a->aStringArray;

    And you can just use your regular
    mem_b->aStringArray2[x][y]
    to access the array inside your second level function.
    Last edited by Salem; 12-14-2012 at 01:30 PM. Reason: s/20/80
    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
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I agree with Salem's approach. But I want to ask, why are you using two different structs here? If the array in struct b points to the exact same memory in struct a, can you not just pass struct a around? If you want a boolean representation of whether mem_a->xyz is zero or non-zero, then just use it in an if statement by itself, like you do on line 4. Beside, if mem_b's array is referring to the exact same memory as mem_a's, then why bother with the other struct. Just have your second function take a struct a:
    Code:
    void exampleFunc2(struct a *mem_a)
    {
        if (mem_a->xyz)  // instead of mem_b->b
        ...
        mem_a->aStringArray[i][j] = 'x'
    }
    It amounts to the same thing, and avoids the complication of an extra struct.

    If you have really good justification for needing a struct a and struct b, then go ahead with Salem's suggestion, but based on what you posted, I don't see it.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Thanks Salem and anduril462. Actually the struct a and struct b were just examples. In real, the two structures are very different and they cannot be modified as they are provided by a 3rd party library interface. So seems that I must allocate the duplicate memory in this scenario.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Lesson for the day - present the actual facts if you want an actual answer.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    That may be a lesson but I got the answer to my question (and some clarity in my mind how to re-use the existing memory in case of double pointers)

    That will be useful for me in future so thanks anyway!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying strings to array of pointer to char
    By yyzhitcher in forum C Programming
    Replies: 1
    Last Post: 02-12-2012, 05:17 PM
  2. Replies: 3
    Last Post: 12-01-2011, 04:00 AM
  3. Copying into an array of strings
    By mettle in forum C Programming
    Replies: 5
    Last Post: 06-14-2006, 02:18 PM
  4. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  5. Passing an array of strings from VB to C++
    By uman in forum Windows Programming
    Replies: 4
    Last Post: 02-25-2005, 05:38 PM

Tags for this Thread