CSstudZ
CSstudZ

Reputation: 121

Strcpy behavior with stack array c++

Here is my program :

#include <cstring>
const int SIZE =10; 

int main() 
{ 
    char aName [SIZE]; // creates an array on the stack
    std::strcpy(aName, "Mary");
    return 0;
}

This program is obviously useless, I am just trying to understand the behavior of the strcpy function.

Here is it's signature : char * strcpy ( char * destination, const char * source )

so when I do : std::strcpy(aName, "Mary");

I am passing by value the variable aName. I know that the aName (in the main) contains the address of the array. So is this assertion correct : strcpy creates a local variable called destination that has as value the address of the array aName that I have created on the stack in the main function?

I am asking this because it is very confusing to me. Whenever I have encountered addresses it usually was to point to a memory allocated on the heap...

Thanks!

Upvotes: 0

Views: 312

Answers (6)

eerorika
eerorika

Reputation: 238361

I know that the aName (in the main) contains the address of the array.

You knew wrong. aName is an array. It contains the elements, not an address.

But when you use the name of the array as a value such as when passing it to strcpy, it is implicitly converted to a pointer to first element of the array (the value of a pointer is the memory address of the pointed object). Such implicit conversion is called decaying.

So is this assertion correct : strcpy creates a local variable called destination that has as value the address of the array aName that I have created on the stack in the main function?

This is correct enough. To clarify: It is a function argument rather than a local variable. But the distinction is not important here. Technically, it is the caller who is responsible for pushing the arguments onto the stack or storing them into registers, so it could be considered that main "creates" the variable.

Whenever I have encountered addresses it usually was to point to a memory allocated on the heap

Pointers are not uniquely associated with "heap". Pretty much any object can be pointed at, whether it has dynamic, static or automatic storage or even if it is a subobject.

Upvotes: 0

user10957435
user10957435

Reputation:

So is this assertion correct : strcpy creates a local variable called destination that has as value the address of the array aName that I have created on the stack in the main function?

Yes. That is correct. Though I probably wouldn't call it a local variable. It is a parameter. Local variable usually means something like this:

int localVariable;

The word'parameter" is often associated with things like this:

int myFunction(int parameter) {
    // use parameter some where...
}

The point is roughly the same though: it creates a variable that will go out of scope once the function exits.

I am asking this because it is very confusing to me. Whenever I have encountered addresses it usually was to point to a memory allocated on the heap...

Yes, this is the most common use case for them. But it isn't their only use. Pointers are addresses, and every variable has an address in memory regardless of whether it is allocated on the "heap" or "stack."

The use here probably because pointers to a char are commonly used to store strings, particularly on older compilers. That combined with the fact that arrays "decay" into pointers, it is probably easier to work with pointers. It is also certainly more backwards compatible to do it this way.

The function could have just as easily used an array, like this:

char * strcpy ( char destination[], const char source[ )

But I'm going to assume it is easier to work with pointers here instead (Note: I don't think you can return an array in C++, so I'm still using char *. However, even if you could, I would imagine it is still easier to work with pointers anyway, so I don't think it makes a lot of difference here.).


Another common use of pointers is using them as a way to sort of "pass by reference":

void foo(int * myX) {
    *myX = 4;
}

int main() {
    int x = 0;

    foo(&x);

    std::cout << x; // prints "4"

    return 0;
}

However, in modern C++, actually passing by reference is preferred to this:

void foo(int & myX) {
    myX = 4;
}

int main() {
    int x = 0;

    foo(x);

    std::cout << x; // prints "4"

    return 0;
}

But I bring it up as another example to help drive the point home: memory allocated on the heap isn't the only use of pointers, merely the most common one (though actually dynamically allocated memory has been mostly replaced in modern C++ by things like std::vector, but that is beside the point here).

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

So is this assertion correct : strcpy creates a local variable called destination that has as value the address of the array aName that I have created on the stack in the main function?

Yes.

Whenever I have encountered addresses it usually was to point to a memory allocated on the heap...

Yep, usually. But not always.

Pointers to non-dynamically-allocated things are fairly rare in C++, though in C it's more common as that's the only way to have "out arguments" (C does not have references).

strcpy is a function from C's standard library.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311018

Function parameters are its local variables.

In this call

std::strcpy(aName, "Mary");

the two arrays (one that is created in main with the automatic storage duration and other is the string literal that has the static storage duration) are implicitly converted to pointers to their first elements.

So you may imagine this call and the function definition the following way

std::strcpy(aName, "Mary");

 // …

char * strcpy ( /* char * destination, const char * source */ )
{
    char *destination = aName;
    const char *source = "Mary";

    // …

    return destination;
}

Or even like

char *p_to_aName = &aName[0];
const char *p_to_literal = &"Mary"[0];
std::strcpy( p_to_aName, p_to_literal );

 // …

char * strcpy ( /* char * destination, const char * source */ )
{
    char *destination = p_to_aName;
    const char *source = p_to_literal;

    // …

    return destination;
}

That is within the function its parameters are local variable of pointer types with the automatic storage duration that are initialized by pointers to first characters of the passed character arrays

Upvotes: 0

Fred Larson
Fred Larson

Reputation: 62083

Maybe it would help to look at an example implementation of strcpy():

char* strcpy(char* d, const char* s)
{
    char* tmp = d;

    while (*tmp++ = *s++)
        ;

    return d;
}

That's really all there is to it. Copy characters from the source to the destination until the source character is null (including the null). Return the pointer to the beginning of the destination. Done.

Pointers point to memory. It doesn't matter if that memory is "stack", "heap" or "static".

Upvotes: 0

M Hamza Razzaq
M Hamza Razzaq

Reputation: 472

Whenever you encounter addresses it doesn't mean it will always point to memory allocated to heap.

You can assign the address of a variable to a pointer like this

int a=5;
int *myPtr= &a;

Now, myPtr is a pointer of type integer which points to the memory of variable which is created on stack which is a have value 5.

So, whenever you create a pointer and assign the (address of) memory using new keyword, it will allocate the memory on heap. So, if I assign the value like this it will be on stack

int *myPtr= new int[5];

Upvotes: 1

Related Questions