Ajay Yadav Aricent
BAN USER- 0of 0 votes
AnswersExample1:
- Ajay Yadav Aricent in India
const int a = 0;
int *ptr = const_cast<int *>(&a);
*ptr = *ptr + 10;
cout <<a<<" Address a "<<&a<<endl;
cout <<*ptr<<" Address in ptr "<<ptr<<endl;
&a and ptr contain the same but there values are different. can anyone tell how C++ interpret const_cast internally and why the value different and address is same of a and *ptr
Example2:
int a1 = 0;
int *ptr1 = &a1;
*ptr1 = *ptr1 + 10;
cout <<a1<<" Address a "<<&a1<<endl;
cout <<*ptr1<<" Address in ptr "<<ptr1<<endl;
in this example address and value are same.| Report Duplicate | Flag | PURGE
Algorithm
Thanks for your value added comment but i do not get why the "*ptr = *ptr+10;" get changed and its still hold the same address as 'a' variable.
- Ajay Yadav Aricent July 10, 2012#include<iostream>
using namespace std;
#include<stdio.h>
int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
int i;
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0)
max_ending_here = 0;
if(max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
/*Driver program to test maxSubArraySum*/
int main()
{
int a[] = {-2, 10,-13, 4, 7, -6, 1, 5, -3};
int max_sum = maxSubArraySum(a, 8);
printf("Maximum contiguous sum is %d\n", max_sum);
return 0;
}
static has different meanings depending on what you are talking about - variables, classes, functions, etc...
In regards to functions, the static keyword does not affect storage at all but is rather simply a guide to the linker stating that only other functions in the source file where the static function is defined may call it. Declaring a function static simply makes the linker enforce this rule and error will result if you try to circumvent the rule. So, that said, the code for the function exists in the text/code portion along with all the other code.
Vectors are good at:
Accessing individual elements by their position index (constant time).
Iterating over the elements in any order (linear time).
Add and remove elements from its end (constant amortized time).
after correcting return 0; it will print infinite time till stack overflow. error "Unhandled exception at 0x7712c6d3 in program.c.exe: 0xC00000FD: Stack overflow."
- Ajay Yadav Aricent June 14, 2012after correcting return 0; it will print infinite time till stack overflow. error "Unhandled exception at 0x7712c6d3 in program.c.exe: 0xC00000FD: Stack overflow."
- Ajay Yadav Aricent June 14, 2012
Thanks Eugene Yarovoi
- Ajay Yadav Aricent July 10, 2012