1

When we call function passing by value, we are making a copy in memory of the actual parameters' value.

The question is: does the function know how much space its parameters occupy in memory? If the answers is yes, how we can retrieve it in function scope?

If the answers in no, do we have a potentially hidden memory error?

consider this example:

#include <stdio.h>
void func(char * X)
{
    X +=98;
    *X='C'; //is this really OK? or we have hidden memory error?
    *++X='\0';
     --X;
    puts(X);     }

int main()
{
    char A[100];
    char *B =A;
    func(B);
    return 0;
}
MD128
  • 501
  • 4
  • 12
  • 4
    Your question is confusing, since you ask about passing by value - and then in the example, you are passing a pointer and manipulating the value it points at. Can you rephrase the question to make it clearer? – Kif Jun 01 '15 at 08:19
  • 1
    Seems like this should be tagged C, not C++. – Lundin Jun 01 '15 at 08:23

4 Answers4

4

You have a potentially hidden error. Don't manually do that crap yourself always use something like std::string.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • The question is about function and its parameter, if we have hidden error for this example- char* - then we have hidden error for any type * , too and we can not use from type++. or any pointer shift in the function scope as same reason. – MD128 Jun 03 '15 at 12:42
  • 1
    @alwaystudent: Yes, you do, and it's a terrible idea to take a random pointer and start indexing into it by random amounts. – Puppy Jun 05 '15 at 10:58
  • Dear all thank you, i think i got the point, based on you explanation, or correct me: the function dose not know, and if we call it improperly/properly, we do/do not have potentially error – MD128 Jun 05 '15 at 11:14
  • 1
    @alwaystudent: The problem is that it's possible to call the function improperly. That's what makes it a terrible function. – Puppy Jun 05 '15 at 21:00
1

The function parameter is a pointer to a char value. The function call makes a copy of the pointer, i.e. a copy of the address to the char value.

The function has no way of knowing how much memory, if any, has been allocated at that address.

For all the function knows, the pointer could ne null, it could be pointing at a single char, or it could be pointing at an array of chars, which has already been allocated in memory.

Depending on what you are trying to do in the function, you could e.g. 1) Tell the function about the size of the allocated memory in an additional parameter. 2) Use a different data type. E.g., if the intention is for the parameter to point at a string, then consider passing a reference to an object of string type instead.

Kif
  • 265
  • 1
  • 8
1

In C++, you probably shouldn't be doing manual string manipulations like these at all, std::string is preferred. I'll assume you are using C, since #include <stdio.h> is obsolete style in C++ anyhow.

When we call function passing by value, we are making a copy in memory of the actual parameters' value.

Indeed. In this case, a copy of the pointer to the string is made.

The question is: does the function know how much space its parameters occupy in memory?

Sure, simply take sizeof(X) and you'll get the size of the pointer (4 bytes on 32 bit system). As for whether a "function knows" the nature of the data which that pointer points at... it depends on the programmer.

If the programmer of the function has properly documented that the pointer must point at an allocated array of 100 bytes, then the function is free to assume that it was passed such a pointer.

If the programmer haven't documented a thing, then nobody would know how to use the function, in which case nothing in the program makes any sense. Unless the function definition is available to the caller.

Alternatively, you could write self-documenting code: void func(char X[100]). This code is 100% equivalent to what you have: it also passes a pointer. But there are still no guarantees that this pointer points at an array of 100 bytes, we have merely added a hint to the caller that we expect one. If they pass something else, the program will still likely halt and catch fire.

If you are wondering how to find out the size of the data pointed-at, without actually passing it as parameter, read this.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
1

The question is: does the function know how much space its parameters occupy in memory?

Yes it does. In your case, you pass a pointer to the function. Pointers of any type all have the same size and on a 32 bit machine its 32 bits (4 bytes).

If the answers is yes, how we can retrieve it in function scope?

You do so by calling sizeof() operator. However in your case I assume you want to get the number of elements of the array the pointer points to. That you cannot do in your case.

void func(char * X)
{
    X +=98;
    *X='C'; //is this really OK? or we have hidden memory error?
    *++X='\0';
     --X;
    puts(X);     
}

This code is error prone. There is no guarantee that X points to an array of at least 100 elements, however your code assumes it does. If you write the following code you indeed do have a memory leak.

int main()
{
    char A[50];
    char *B =A;
    func(B);
    return 0;
}

What exactly are you trying to archive with your code?

Side note: if this is indeed a C++ question, I would also suggest to use std::string for string manipulation. It makes a lot of things much easier :)

rozina
  • 4,120
  • 27
  • 49