c++ how to point to a member of an array - c++

Wondering how to assign a pointer to an array member:
struct foo {
int INT;
}
int main() {
foo bar[10];
foo *baz;
baz = bar[5];
}
This does not work, but I am wondering what would.
Thank you much for any help.

You want to do baz = &bar[5];. bar[5] Refers to the 6th foo object instance itself, so just take the address (with the & operator) to assign to the pointer, same as any other situation;
Alternatively, you can also do baz = (bar + 5); since here bar used without a number is a pointer to the first element and +5 gives the 6th element.

Related

C++ use non pointer values in object

I know this is super basic, but will a non pointer value hold its value when it is initialized within a constructor?
class foo {
private:
int bar;
int cool;
public:
foo(int tBar);
~foo();
}
foo::foo(int tBar) {
bar = tBar;
cool = -5;
}
Or in other words, will bar hold tBar's value later on and will cool still be equal to 5 after the construction of the object?
Answer is Yes. I had a misconception that every member variable had to be a pointer!
BUT
When I use more complicated object types (like my own objects) it doesn't work this way. Why is that?
Answer is yes, the object will hold its value
Also I was using crappy objects. That is why they didn't hold value.

Assign value to member of struct using double pointer

I have a function that takes a double pointer to a struct and assigns a value. However I get an "access violation writing location ..." when trying to access the member member1.
This is my code:
struct mystruct{
unsigned int member1;
void * data;
};
int main(){
mystruct **foo = new mystruct *;
bar(foo);
}
void bar(unsigned int val, mystruct ** foo)
{
(*foo)->member1 = val;
}
You just created a new mystruct pointer. That means:
You get allocated a memory block, large enough to hold a address, and assign it to the pointer which is pointing to a pointer that points to a mystruct member.
That doesn't mean, that there is a valid address hold in the pointer which you expect to be pointing to a mystruct element. Even more currently there isn't even a valid address, where the pointer to the pointer is pointing on, as you just assigned a valid memory area to it, what doesn't mean there is a usefull address stored in.
So at all, what you want is:
You want a pointer which has a valid memory block to store a address in of another pointer, which is pointing to a valid memory area, where is a (probably valid) mystruct stored in.
What you are doing is: you are requesting a memory area where you COULD (what you aren't even doing) store a poitner to another pointer... and so on.
so what you should do is:
mystruct **foo = new mystruct *;
*foo = new mystruct;
I have a function that takes a double pointer
That's weird. If you can, simplify it to take a reference:
void bar(unsigned int val, mystruct & foo) {
foo.member1 = val;
}
mystruct foo;
bar(42, foo);
If you don't have control over the function, then you'll need an object at the end of the pointer trail:
mystruct foo;
mystruct * pointless = &foo;
bar(42, &pointless);
You can, of course, mess around with new if you really want to; but that's almost certainly a bad idea.
Your code allocates and leaks a pointer, but doesn't initialise it to point to a valid object; so dereferencing it gives undefined behaviour.
This C-style function:
void bar1(mystruct* foo) {
foo->member1 = val;
}
takes an argument of type mystruct* in order for changes made to object that foo points to to be visible to the caller. However this function:
void bar(unsigned int val, mystruct ** foo) {
(*foo)->member1 = val;
}
takes pointer to mystruct* (most likely) in order to to modify the pointer itself, i.e. in order for changes made to the pointer to be visible to the caller and thus probably meant to be used this way:
mystruct* foo = new mystruct;
bar(&foo);
... yet usually it is reasonable to avoid dynamic allocation and passing pointers should be rather rarity than a common practice. Prefer objects with automatic storage duration over the dynamically allocated ones and prefer passing by reference over passing by pointer (when possible).
The other answers are good advice. But also, if you have control over the bar function, and need to be able to change in bar to what object your mystruct * pointer points to (which is probably the reason why you have a double pointer in the first place), then the cleanest approach is to use the following signature for bar:
void bar(unsigned int val, mystruct * &foo);
It passes by reference a pointer, so you are able to change to what object the pointer points to, without sacrificing readability of the code, for instance:
int main()
{
mystruct * foo = new mystruct;
bar(42, foo);
}
void bar(unsigned int val, mystruct * &foo)
{
foo->member1 = val;
foo = new mystruct;
}
A complete usage scenario without memory leak could be:
int main()
{
// allocate dynamically a mystruct whose member1 is equal to 1.
mystruct * foo1 = new mystruct;
mystruct * foo2 = foo1;
foo1->member1 = 1;
// pass by reference foo1
bar(42, foo1);
// here, foo1->member1 == 42 and foo2->member1 == 10
// free memory
delete foo1; // the one allocated in bar()
delete foo2; // the one allocated in main()
}
void bar(unsigned int val, mystruct * &foo)
{
// modify the object allocated in main()
foo->member1 = 10;
// allocate dynamically a mystruct, store its address in foo
foo = new mystruct;
foo->member1 = val;
}

C++ pointer and address clarification in relation to the current object

So far to my understanding, when defining a pointer variable, we are allocating space in RAM for that variable.
int *p;
Would define a space in RAM. Then we assign a memory address to that pointer using `&variable'.
I'm looking over at an example on: *this vs this in C++
The code is:
#include <iostream>
class Foo
{
public:
Foo()
{
this->value = 0;
}
Foo get_copy()
{
return *this;
}
Foo& get_copy_as_reference()
{
return *this;
}
Foo* get_pointer()
{
return this;
}
void increment()
{
this->value++;
}
void print_value()
{
std::cout << this->value << std::endl;
}
private:
int value;
};
int main()
{
Foo foo;
foo.increment();
foo.print_value();
foo.get_copy().increment();
foo.print_value();
foo.get_copy_as_reference().increment();
foo.print_value();
foo.get_pointer()->increment();
foo.print_value();
return 0;
}
I don't understand what the purpose of putting the * operator in front Foo* get_copy() and Foo* get_pointer() does. Why do I get an error if I removed the * from the Foo* functions while returning this not *this?
edit:
Also, why is:
foo.get_copy().increment();
foo.print_value();
yielding 1 not 2?
I don't understand what the purpose of putting the * operator in front Foo* get_copy() and Foo* get_pointer() does
Foo* get_pointer()
Foo* is a pointer that points to Foo object.
this is also a pointer that implicitly bind to the calling object of the member function. That's why the return type of those functions are Foo* not Foo.
The * is part of the type. So int means type int, and
int* type pointer to int. If the function returns pointer
to int, it is int* foo(), and if it retu rns a pointer to
Foo, it is Foo* Foo::get_pointer().
The definition reserves space for the defined object. A
declaration doesn't reserve any space, and definitions of things
that aren't objects (e.g. references or functions) don't reserve
any space either, at least not that you can see. (Obviously, a
function does exist somewhere in memory, and in many cases, the
compiler will need space as well for its implementation of a
reference. But they are invisible within the scope of C++.)
this, is always a pointer in C++, though you don't mention it explicitly anywhere. So while returning a this pointer, should use Foo*
this is actually an implicit object passed during function call, which is a pointer to the object which calls the function
It seems that you have changed the code from the example that you refer to so that get_copy() no longer returns a copy.
There are 2 ways of using * in your code example. One is for type declaration and the other is the dereferencing operator.
First the type declarations:
int *p means declaring p as a variable of type "pointer to an int".
Foo *get_pointer() means that the function get_pointer will return a value of type "pointer to a Foo object".
Now the dereferencing:
*p means "the value that p points to".
int a = 42;
int *p; // p is of type "pointer to an int"
p = &a; // set p to the address of a (p now "points to" a)
a = 117; // change the value of a
int x = *p; // set x to the value that p points to (which is a) - x will be 117
this is just a pointer to the object. *this means "the object that this points to". In your example this is of type Foo* (pointer to a Foo object) while *this is of type Foo (a Foo object).
"this" is a pointer.
you want to return a pointer to the instance (a specific allocated object).
Foo* get_pointer(){
return this;
}
or you want to return a pointer to the copy, allocate a new object.
//need to implement the copy here
Foo* get_copy(){
return this;
}
and not to the reference (address of the instance). this is why you need to return the pointer.

beginner reference type parameter confusion [duplicate]

This question already has answers here:
Closed 11 years ago.
foo(int &bar)
{
bar = 5;
}
The call to this function would be
int foobar = 2;
foo(foobar);
Am I right in thinking that the function parameter essentially 'gets' the memory address of the variable, but does not then have to be dereferenced in foo, and that I would be changing the original value of foobar? Before this I was under the Impression that you would have to pass in a memory address like this:
foo(&foobar);
and then use the variable inside foo like this:
*bar = 5;
Am I right in thinking that this is wrong? I think, like a lot of beginners, the confusion came from thinking that a reference was like a pointer in that it held a memory address, but it's never really a type is it? Just an operator.
References are usually implemented with underlying pointers (although that is not mandated by the standard), but they are a totally different beast than pointers. A reference is simply a new name or alias for an existing variable. When you do
void foo(int& bar)
{
bar = 5;
}
int foobar = 2;
foo(foobar);
you are invoking foo to be evaluated with the variable foobar, so essentially bar within foo becames foobar. The usual compiler impementation for this is to actually implement this code like this:
void foo(int* bar)
{
*bar = 5;
}
int foobar = 2;
foo(&foobar);
If you did this:
void foo(int& bar) { bar = 6; }
int main() {
int x = 3;
foo(x);
}
x in main would equal 6. This is because a reference/alias (a different name for the same variable) is passed into foo. So, bar in function foo is the same variable as x in main, same memory location, and everything.
This is different from normal passing like this:
void foo(int bar) { bar = 6; }
int main() {
int x = 3;
foo(x);
}
where bar would be a new copy constructed value copied from x in main (note that copy constructor would better apply to a non-scalar type, but the idea is the same).
And lastly, if you did:
void foo(int* bar) { *bar = 6; }
int main() {
int x = 3;
foo(&x);
}
It would say "I want a pointer to an integer" for foo's parameter. In main, we take the "Address of stack variable x" which is in essence the same thing as a pointer to x, and we pass it into bar. Then dereferencing bar and assigning 6 to it would make x in main equal to 6 too :)

C/ObjC - parameter size. Using a pointer vs value

at what point should I be passing a pointer to data in my functions/methods, rather than just passing the value?
Obviously there's the cases where I want the function to operate on the given data, but what if I'm just passing the value for info/copying purposes?
For example, foo as a basic type:
void setFoo(int foo);
...
int foo = 1;
setFoo(foo);
Now foo as a simple structure:
typedef struct {
int x;
int y;
} Foo;
void setFoo(Foo foo);
...
Foo foo = {1, 2};
setFoo(foo); // Apple code does this kind of thing with CGSize, CGPoint...
But what if foo is a bigger struct...
typedef struct {
int x;
int y;
int z;
char str[256];
} Foo;
void setFoo(Foo *foo); // Now taking a pointer instead.
...
Foo foo = {1, 2, 3, etc ... };
setFoo(&foo);
Q. At what point here should I start using a pointer when providing data to a function?
Thanks
When on embedded systems, I think it's good practice to pass pointers (or references) for anything that's not a primitive type. This way your struct can grow and add members as needed without affecting the amount of data that is copied. Needless copying is a good way to slow down your system, so avoid it when you can. Getting into that habit will help in the long run I think.
If you use references and/or pointers to [const] objects as appropriate, "when" becomes "all the time"