C++ :: Pointer Aliasing And Reference?
Jul 7, 2014
I am used to the restrict keyword to hint the compiler that no overlap is going to happen in the values passed to the function.
void foo( int * restrict a, char * restrict b)
I understand that I can pass by reference in c++.
void foo( int &a, char &b)
Using it, will it automatically restrict it or not ? It is very important for performance reasons (no checks at each iterations/steps)...
View 1 Replies
ADVERTISEMENT
Jun 20, 2014
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
View 6 Replies
View Related
Aug 20, 2014
Can I a have one pointer with two reference in it. Here's what I've got.
Code:
char* c;
char x='x' , y='y';
c = &x;
c = &y; -- or --
Code: char* c[2];
char x='x' , y='y';
c[0] = &x;
c[1] = &y;
If it's possible I want to apply it to make AST.
View 8 Replies
View Related
Jan 7, 2015
I would like to get the this pointer by call by reference. Is this possible? I hoped to get it with this code, but it doesn't work:
Code:
"cpp"]class DemoClass {
public:
DemoClass();
int x;
void setParam(const DemoClass ¶m){
param=this;
[Code] ....
I get always the error code "C2678". But I don't understand how I should change my code to avoid this.
View 4 Replies
View Related
Feb 9, 2015
//program to form a header file
/* using pass by reference in pointer */
#include <iostream>
#include<math.h>
[Code].....
View 14 Replies
View Related
May 12, 2012
I am unable to get "reference to a pointer".
Here:
char*p = "hello";
char*&k = p;
while creating reference to a pointer why we have to write *&k , why not &*k?
View 6 Replies
View Related
Jul 12, 2014
I've read about the difference between the two, what one can do that the other can't syntactially, but I'm still very confused by the concept of references in C++. I'm trying to understand what is the difference in terms of undelying implementation.
Take, for example, the following code:
void foo(int *bar) {
++*bar;
}
int main(void) {
int n = 0;
foo(&n);
return 0;
} Translated into: Code: __Z3fooPi:
[Code] ....
Considering the above assembly code, and the memory that was used in both cases, I'm tempted to say that references was added to C++ to make the synax looks prettier... Where would I be wrong?
View 14 Replies
View Related
Jan 24, 2013
What is the difference in pass by pointer and pass by reference? As per my understanding, there is no difference much.If a function accepts pointer, then NULL check can be performed.other than this i'm not able to see any big difference..
View 3 Replies
View Related
Mar 14, 2013
I have a struct which has an array inside of it:
struct someStruct{
int structArray[999];}
Now when I want to access that array, I have the following:
ptrSomeStruct->structArray[someIndex];
But now I want to pass structArray to this function by reference so that it can adjust the array as needed and I can continue to use the array back in my caller function:
void adjustArray(void *& someArray){}
How do I accomplish this?
View 2 Replies
View Related
Jul 30, 2014
I just want to know if there is any real difference between the two below, if yes, when would i use one over the other? I would thought the "&" is pointless in below function, as far as the data is concerned.., the only things is with "&", if the pointer address value is changed in Test function, it will affect the caller's copy of data. Both function should behave the same if data is changed.
Code:
Between
void Test(QSharedPointer<Data> data)
{
}
and
void Test(QSharedPointer<Data> & data)
{
}
View 6 Replies
View Related
Mar 15, 2012
#include "vehicle.h"
...
void exchange(vehicle *&v1, vehicle *&v2) {
vehicle *tmp = v2;
v2=v1;
v1=tmp;
}
Is it right?
How about: void exchange(vehicle *v1, vehicle *v2){...}
What is the difference between *&v1 and *v1 ?
View 4 Replies
View Related
Jun 10, 2013
what is the advantage of using reference over constant pointer?
Can they be used interchangeably?
View 6 Replies
View Related
Dec 26, 2014
So, one can do stuff like this using #defines:
#include <iostream>
#include <array>
#define x arr[0]
#define y arr[1]
#define z arr[2]
class Point {
[code]....
... that is, to be able to reference the same data by "member variables" as by referencing a stl container. But defines are the devil's work - adding in a "#define x arr[0]" is a dangerous statement. I'd really like some nice clean C++ method (C++11 or C++14 are just fine) to do this without defines, but so far I'm drawing a blank. If arr wasn't an STL container, if we just wanted a pointer-based array, I could do it this way:
class Point
{
...
float x __attribute__ ((aligned (sizeof(float))));
float y __attribute__ ((aligned (sizeof(float))));
float z __attribute__ ((aligned (sizeof(float))));
float*const arr = &x;
};
... but you obviously can't do that if arr is an STL container.
The best I've come up with is to make x, y, and z function pointers, but then you can't call them like p.x, you have to call them like *p.x(), it's not very clean and I'd expect some added overhead. One could go even uglier and make x, y, and z be instances of some custom class with overridden operators that reference arr[], but that seems like it'd be just getting ridiculous in terms of overhead (both coding and performance)
View 4 Replies
View Related
Oct 15, 2014
I have been playing about with Vertices and Colors and have ended up with this:
template < std::size_t SIZE, typename T >
struct Pack
{
[Code].....
Is there anyway to make an alias for Pack's data field? Something that would allow me to write:
Color3f my_color;
my_color.red = 0.1f;
View 3 Replies
View Related
Mar 9, 2013
i'm still unclear between the difference between using pointer and a reference
I understood the concept of pointers in c in the class i took last year
and that was to change the actual value stored in the memory address Code:
void change_a(int a*){
a=6;
}
int main(){
int a=5;
change_a(&a);
}
but in c++ I've been using references in all my assignments because I don't know how to correctly use pointers in c++ I may have missed a class but I'm on spring break and would like to clear things up
so in c++
in my assignments I would call it like this Code:
void change_a(int &a){
a=6;
}
int main(){
int a=5;
change_a(a);
}
so does this change the value in the address or does it make another copy of a in my c++ code and stores 6 in that copy
View 2 Replies
View Related
Dec 8, 2014
I have to write an example in which you use a function having as argument a pointer passed by reference in C++. Can you provide an example like this:
funz.h : void funz( int *&a );
funz.cpp : ? (1)
main.cpp:
#include "funz.h"
#include <iostream>
[Code]...
as I write in (1) and (2) ?
View 5 Replies
View Related
Oct 4, 2013
I don't understand how my code not run.
#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;
[Code] ....
View 3 Replies
View Related
Apr 17, 2012
I just started learning about pointer and reference. * and &
The assignment is " Write a program that stores the following numbers in the array named miles:15,22,16,18,27,23, and 20. Have your program copy the data stored in miles to another array named dist, and then display the values in the dist array. YOur program should use pointer notation when copying and displaying array elements.
And this is what i have so far. But there is an error. I highlighted it with red. It says it's incompatible...
#include <iostream>
using namespace std;
const int arraynumb = 7; // declaration of keys: number of characters of keys
void copyfunc(int *[], int); // function initialized
int main() {
int miles[arraynumb] = {15, 22, 16, 18, 27, 23, 20};
[Code] ....
View 1 Replies
View Related
Apr 21, 2014
I am trying use a print function to print out data in a struct. My questions are:
1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?
2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Inventory {
[Code] .....
View 9 Replies
View Related
May 16, 2014
I know that you're allowed to use a char pointer to access any object but are you allowed to inspect a char array with a different type, say an unsigned integer without breaking the strict aliasing rule? My understanding is that it's not legal and could lead to trouble with trap representations but I just wanted to make sure.
View 4 Replies
View Related
Sep 19, 2014
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
Obj(string name): name(name) {cout << "create " << this << endl;}
[code]....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 3 Replies
View Related
Oct 3, 2012
I have encountered a problem I can't see to solve. I want to access a function and can't seem to find the right combination to get me there. Here is what I am looking at:
CFoo1::CFoo2::GetStrDataC(int nRow) const
How do I call the GetStrDataC function from another class?
View 6 Replies
View Related
Jan 17, 2014
I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this
bool deleteNode(struct node *head, struct node *delptr)
I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.
Would also like to know why the argument call needed &head instead of just head.
remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);
#include "stdafx.h"
#include<iostream>
struct node{
[Code].....
View 1 Replies
View Related
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related
Nov 27, 2013
I have to write a program to print pascals triangle and stores it in a pointer to a pointer , which I am not entirely sure how to do. I also have to write the file and read it, then create a binary file. Assignment is attached. I am not the best with programming and especially with pointers. I will post my code below.
Code:
#include <stdio.h>
#include <stdlib.h>
void writePascalTriangle(char *fileName, int heightOfTriangle, int **triangle) {
FILE *fp;
fp=fopen("writePascalTriangle.txt", "w");
[Code] ....
View 4 Replies
View Related
Apr 19, 2014
I'm making a system like twitter for class called ShoutOut.com I want to be able to get the PublicShoutOut pointer pointed to by the start iterator and assign it to firstShoutOutToDisplay and secondShoutOutToDisplay because I need that in order to pass the pointers to one of my functions. When I step through the debugger the values in start are all default values like "" and so are the values in this->firstShoutOutToDisplay but the message that start points to is being output just fine.
EDIT: got rid of irrelevant code. Am I using the correct syntax to do this?
if (start != finish) {
//getting these because a shoutout needs to be passed to the function that displays
//options for a shoutout
this->firstShoutoutToDisplay = (*start);
[Code] ....
View 2 Replies
View Related