Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
#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;
}
Posted
Updated 4-Jun-15 22:52pm
v2

This is OK but it is bad style, because your func is poking in some memory without knowing whether it is OK.

It depends on the input pointer: the size must be great enough and you must have write rights. Else it crashes.

Better is for instance:

C++
void func(char * buffer, int count)
{
if( count > 100) 
{
 //manipulate all valid bytes 
}
}
 
Share this answer
 
Comments
Mohamad128 5-Jun-15 5:09am    
If that is OK and we do not have potentially error when derefrencing pointer X, after shift +98, it means in function scope , we(machine) know how much memory allocated to X (100*size of char), but how to retrieve it in function scope?
KarstenK 5-Jun-15 13:44pm    
I added the size as second parameter to the function. Didnt you read that? (You must learn to read precisly) ;-)
Mohamad128 5-Jun-15 14:10pm    
Thank you, I got the point, :)
Your question is not very clear to me, but I assume that you perplexity is related to arrays.
In C the name of an array is it's address (a pointer to its first element). So if you pass an array to a function in reality you're passing the address of the very first element and no other information. No memory copy of the data is created. So you don't have a copy and you don't know the dimensions of the array. And if you use the 'sizeof' operatoor on the array name you'll get the size of a pointer (not the size of array).
The answer to your question in case of arrays is: No, the function doesn't know what is the size of the parameter.
The case of other types of data is handled differently, but the function still doesn't know the dimension of the parameter.
Consider a structure. For structures passing data by reference or by value makes the difference.
In this case the compiler cannot allocate the structure on the calling stack frame (because this would create problems firstly with stack frames handling, then with memory access boundary), the compilers then, by common conventions, allocates a private copy of the structure someway (normally on stack memory using alloca()), then passing a pointer to that structure to the called function. The compiler handles the whole thing transparently as if the real data value was passed instead of a pointer to the copy, and anyway for the user accessing a private copy is equivalent to a passage by value.
For the sake of completeness, the compiler before to act checks the structure dimension and if it can fit in a basic type dimension pass the structure directly. I.e.
C++
struct _tagMyStr
{
  short a;
  short b;
};

This struct made of 2 shorts can fit in one integer than it is passed directly on the stack (or register in 64bits __fastcall).
 
Share this answer
 
v6
If you mean to ask: Does a function know, how large the memory is that one of its parameters points to, the answer is a clear "no". A pointer that is passed via a parameter is just like any pointer and the program has no way of telling, whether it
points to a single element or many such elements, nor how many.

And so your example is strictly speaking illegal C. With
C++
X +=98;
*X='C'; //is this really OK? or we have hidden memory error?
*++X='\0';
 --X;

you are accessing memory that you have no control over. In the given example, in which the main program provides an array of length 100, all goes well. But someone else could call your function with a smaller array and then your program might crash.

The very least you had to do is to comment the interface of your function in a way that makes it clear that X must point to an array of at least 100 chars.

Better would be to introduce a second parameter that specifies the length of the
array passed by X, as was suggested in solution 1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900