Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this C structure assignment statement legal?

Tags:

c

Here is a code example followed by my question:

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

struct st {

    char stringField[100];
    int intField;
};

typedef struct st st;

void test(st *parameterStruct)
{
    st localStruct;
    strcpy(localStruct.stringField, "HELLO");
    localStruct.intField = 5;

    *parameterStruct = localStruct;
}

int main()
{
    st myStruct;
    strcpy( myStruct.stringField, "XXX" );
    myStruct.intField = 9;

    printf("%s,%i\n", myStruct.stringField, myStruct.intField );

    test(&myStruct);

    printf("%s,%i\n", myStruct.stringField, myStruct.intField);

    return 0;
}

OUTPUT:

XXX,9
HELLO,5

I was thinking that since the structure 'localStruct' was created inside a function (NOT using malloc) it had local scope and thus the memory locations where it was stored were free to be overridden once the function stopped executing. However, I tried running this sample program and it executed with no issues. I was thinking that the second print statement was going to print gibberish to the screen since I assigned 'myStruct' to local variable 'localStruct' (versus 'localStruct' being dynamically allocated). I know if 'localStruct' had been created using malloc there would be no such issues.

My question: is assigning the structure variable 'myStruct' (a non dynamic local variable) to y by the use of a pointer in the function test okay and safe to do? I hope that the question is clear.

like image 783
Rob L Avatar asked Jun 08 '14 12:06

Rob L


People also ask

What is structure assignment in C?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.).

Can we copy one structure to another structure?

If the structures are of compatible types, yes, you can, with something like: memcpy (dest_struct, source_struct, sizeof (*dest_struct)); The only thing you need to be aware of is that this is a shallow copy.

Can we assign one structure variable to another in C++?

In C/C++, we can assign a struct (or class in C++ only) variable to another variable of same type. When we assign a struct variable to another, all members of the variable are copied to the other struct variable.


2 Answers

Assignment always copies.

If you did something like *x = &y (assuming the types matched - if the parameter was declared as st** x, for example), you would be copying the address of y, but since y will go out of scope soon, that assignment would be unsafe, as you feared.

But since you're doing *x = y instead (where the parameter is declared st* x), you are copying the content of y to *x, so even after y goes out of scope, the data stored in *x should be valid.

like image 106
Theodoros Chatzigiannakis Avatar answered Oct 28 '22 19:10

Theodoros Chatzigiannakis


Yes. It's safe.

When you assign:

*x = y;

The members of y are copied into the corresponding members of *x. It works as if you did a member to member copy yourself.

like image 7
P.P Avatar answered Oct 28 '22 20:10

P.P