assign value to variable on variable declaration? BC AL - nav

Is it possible to assign a value to a variable on its creation? Something like this:
var ProvisionHeaderCounter: Integer := 1;

No, you can't. If you want to do this you will need to assign the value in the first line of your function.

Related

Explaining Pass by Pointer in C++?

I have a C++ function in which I have two int's, who's purpose is to serve as counters, that are declared outside of the function in my main code. My goal is to update the counter variables with the result from the execution of the function.
I have them declared as such
int cor_letters = 0;
int cor_place = 0;
and then call my function like
res = compare(input, secret_word, &cor_letters, &cor_place);
My compare function header is:
bool compare(string user_input, string secret, int * correct_letters, int * correct_place)
and in my compare code, when I get the final values of the counters, I update them as such:
correct_letters = &cor_l;
correct_place = &cor_p;
I arrived at this solution after carefully reading through my compiler errors, and this seems to work. However, I don't quite understand why this works. In the beginning, I take the address of the two variables and pass them into the function. But the function takes two pointers. So the pointers point to the address of the passed in variables.
Up to this point I seem to grasp what's going on. But its the final assignments that I'm confused by - the pointers (note they're the var names from the function header) are then being updated to the address of the temporary inner function variables that I'm using. Why does this get me the values?
I'm more of a visual learner, and pointers are hard to grasp by just reading some text, so if you wouldn't mind making some quick text diagram to represent what's going on, that would be great. Thank you
I guess you ended up with
correct_letters = &cor_l;
correct_place = &cor_p;
in order to make the compiler stop complaining.
Your analyse about taking the address of local variable is correct.
You probably want to do this
*correct_letters = cor_l;
*correct_place = cor_p;
in order to assign the correct values to the variables
which are outside the function.
A brief memo about reference (&) and dereference (*) operations.
TYPE var_a=..., var_b=...; // some variables of a chosen type
TYPE *ptr=NULL; // a pointer on such a type, but not referencing anything yet
ptr=&var_a; // now ptr memorises the address of var_a (reference operation)
var_b=*ptr; // access the value which is stored at the address memorised
// by ptr (dereference operation) in order to read it
// (here, this has the same effect as var_b=var_a; )
*ptr=var_a+var_b; // access the value which is stored at the address memorised
// by ptr (dereference operation) in order to alter it
// (here, this has the same effect as var_a=var_a+var_b; )

LLVM IR: get the return value of the callsite

Here is a quick question on analyzing LLVM IR. So basically I am trying to fetch the return value of the LLVM IR function call statement, something like this:
%47 = call i256 #test(i256 %46)
I want to get access %47.
And this is the code I have been using to access the parameter.
else if (funcName.contains("test")) {
Value *op = CI->getOperand(0);
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(op))
// get the first function parameter
op = GEP->getPointerOperand();
}
The very suprising finding is that I just cannot find something like "get return value" or so in the document: http://llvm.org/doxygen/classllvm_1_1CallInst.html
Could anyone shed some lights here? Thanks a lot.
CI (The call instruction) is its return value. It has a type that inherits Value.
If you want to do 1 + %47, just as an example, you make an add like this: Value * Add = BinaryOperation::Create(Instruction::Add, CI, ConstantInt::get(i256, 1), ...); The add instruction, in turn, is its result, and Add->getType() == i256 since it is the sum of two values that both have type i256.
As a different answer in case you need the string of the "return value" name, you can use inst.getName() or inst.getNameOrAsOperand() on the Instruction object, which is derived from the Value class.

gdb get current value of variable by it name and change it

I have global variable eg int NUM = 4;. When I attached to the working application with gdb - its hang. Now, I want to get value of NUM and change it. How to do this?
Maybe, possible examples for other data type like char[] and std::string ?
thanks
In gdb you should use
print INT
to show the value of INT and
set variable INT = value
to set INT equal to value.
In an array use
set variable array_variable[element_index] = value
to set the element at index element_index in the array array_variable to value.

C++ How can I tell if a value is assigned into a variable?

I'm making a short database application where people can assign variables to something.
like
cout << "Enter song ID#":
cin >> x;
But I want to prompt the user if there was already a value stored in x. Is there a way to discern between the garbage values initially stored in x and other values?
that way I can do something like
cout << "Enter song ID#":
cin >> x;
if (isUsed(x))
cout << "Do you want to overwrite this value?";
EDIT
x is actually a dynamic array so I cannot initialize it..or can I?
I believe you need this:
http://www.codeproject.com/KB/cpp/value_t.aspx
So your code will look like:
if( x.defined() )
...
You must initialize x to hold some recognizable value which the user cannot produce, or use a boolean variable which is flipped the first time x is set (then check that variable to determine if your value is meaningful).
If you are able to deviate from the standard you could decide to use boost::optional for this.
No. All you can do is use another variable as a sentinel to track whether a value has been assigned to it.
Unless you know a priori what sort of values will be stored in x by your program, there's no way to tell. And of course there's always the chance that the garbage will randomly be in the acceptable range.
But you're not required to live with garbage. Initialize x to some known value that can signal "unused" to you.
There is no way you can discern between the garbage values initially stored in x and other values. But you can write your program in such a way that ensures that when you read the value from the variable, the variable is either initialized or assigned with some value.
Initialize x..
Say,
int x = -1000;
Now every time check for that value -1000.
And of course the value you initialize with x should be a non probable value for the user..

Is this code valid C++?

Is the following code valid C++?
const int var = 10;
{
int var[var]; // why doesn't this give any error ?
}
Note : The code compiles on my g++ compiler.
As-is? No. If it were in the body of a function? Yes.
The first line declares an integer constant named var with a value of 10.
The braces begins a new block. Within that block, a new variable is declared, named var, which is an array of int with a size equal to the value of the integer constant previously declared as var (10).
The key is that var refers to the first variable until after the second variable named var is fully declared. Between the semicolon following the second declaration and the closing brace, var refers to the second variable. (If there was an initializer for the second variable, var would begin to refer to the second variable immediately before the initializer.)
Yes the code is valid C++. Non-local var is visible up to the point of declaration of local var.
So int var[var] defines a local array of 10 integers.
Yes the code is valid C++
It is a concept of SCOPE : Hiding Names
const int var = 10;
{
int var[var]; // why doesn't this give any error ?
}
I think this link clears your doubt
IN C++:
http://msdn.microsoft.com/en-US/library/9a9h7328%28v=VS.80%29.aspx
In C :
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fzexscope_c.htm
If u want deep knowledge On this : Go for this link,here the information is about Lexical versus dynamic scoping
http://en.wikipedia.org/wiki/Scope_%28programming%29
but in your code : "Scope ::" of visibility of var .Here It differs like local and non-local variable.
Inside braces { x=1; } local variable . where as here {y=1;{x=1;}} ,here it differs.
helpful links
http://msdn.microsoft.com/en-us/library/b7kfh662%28VS.80%29.aspx
http://www.awitness.org/delphi_pascal_tutorial/c++_delphi/c++_scope_variables.html