Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Pointers!

pointers objects variables memory arrays arithmetic c++ c address

  • Please log in to reply
30 replies to this topic

#25 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts
  • Programming Language:Java
  • Learning:Java, C++, Python

Posted 10 January 2013 - 04:39 AM

Compared to any minor (or non - existent) slips in a beginner level tutorial, incomprehensible syntactical jargon is far more likely to drive a beginner away, and cause lack of self confidence in his programming style. Anyway, Evan's straight answer solved my problem, so thanks to him.
  • 1

I can believe, but why should I?


#26 object

object

    CC Regular

  • Member
  • PipPipPip
  • 35 posts
  • Programming Language:C, C#, JavaScript

Posted 10 January 2013 - 01:04 PM

What?! You don't care if you invoke undefined behaviour? In that case, don't come to me when the compiler gods make adjustments your compiler and you notice segfaults galore in your code... As I have said previously, he could simply remove the incorrect content and the rest of the tutorial would still make perfect sense. Since you don't care, I'll leave you be. Good luck with your studies!
  • 0

#27 Zer033x

Zer033x

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 50 posts
  • Programming Language:C++, C#, Lua
  • Learning:C++, C#, JavaScript, Lua, Others

Posted 20 January 2013 - 04:27 AM

There's a couple things in this tutorial that I noticed. The reason given for using pointers isn't really a reason at all since function parameters can be passed by value or by reference without pointers. By including the & sign in a parameter list a variable passed will not be copied and the variable at the address will be worked on instead, same functionality you talked about using a pointer for. The main reasons to use pointers are to dynamically allocate memory, create custom memory management allocation, potentially any other memory specific reason you can think of, to point to functions so they can be passed around, and I'm sure there are some reasons from a code efficiency view that could be devised on a case by case basis to speed certain parts of code. The part about not being able to add pointers is wrong since it will work, but it will not work as you may think, instead of adding 1 it will add the size of the memory pointed to times 1.
  • 0

#28 Yannbane

Yannbane

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 238 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, PL/SQL, Lisp, Assembly, Bash, Others
  • Learning:Lisp, Scheme

Posted 21 January 2013 - 06:44 AM

References are implemented as pointers, and there are languages which feature pointers but not references. Therefore, usage of pointers in that way can be useful to know, and it is rather simpler to understand than, say, advanced memory management or something like that.

The point about actually being able to add an integer pointer and an integer is wrong too, at least in C++:

a.cpp: In function ‘int main()’:
a.cpp:9:17: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]


  • 0

My blog: yannbane.com. I post about programming, game development, and artificial intelligence.


#29 Zer033x

Zer033x

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 50 posts
  • Programming Language:C++, C#, Lua
  • Learning:C++, C#, JavaScript, Lua, Others

Posted 21 January 2013 - 08:06 PM

References are implemented as pointers, and there are languages which feature pointers but not references. Therefore, usage of pointers in that way can be useful to know, and it is rather simpler to understand than, say, advanced memory management or something like that.

The point about actually being able to add an integer pointer and an integer is wrong too, at least in C++:


What you're trying to do in your tutorial is add an integer to an integer pointer then assign it to an integer, that gives you a type mismatch error, but adding to a pointer is definitely possible as long as you get your types right. The below program shows how this works. The tutorial made it seem like it was not possible to do math on pointers, but it is.

#include <iostream>
using namespace std;
int main()
{
int *mypointer = new int;//int is 4bytes
int x = 42;
int y = 0;
//the memory pointed to by mypointer is the location that x's value is stored
mypointer = &x;
//have to use * before mypointer to get the data mypointer points to (which is x's value of 42)
//without the * before mypointer you will be trying to access the memory location of mypointer
//then adding 1 to it and since y is an int (not an int pointer) you will get a type mismatch error.
y = *mypointer + 1;
cout << y << "\n";
//see that an int is 4 bytes
cout << sizeof(*mypointer) << "\n";
//check initial memory address of mypointer
cout << (int)mypointer << "\n";
//incrememnt mypointer's memory address
mypointer++;
//display the new memory address of mypointer
cout << (int)mypointer << "\n";
//same syntax as above but left value is an int pointer this time and not an int so no type mismatch
mypointer = mypointer + 1;

//display the new memory address of mypointer once again
cout << (int)mypointer << "\n";

mypointer = NULL;
delete mypointer;
}

Edited by Zer033x, 22 January 2013 - 07:09 PM.

  • 0

#30 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 21 January 2013 - 09:30 PM

Hey guys, given object's posts in this and other topics, I have decided to remove him from this forum. Sorry if this disrupts this discussion.

Edited by Roger, 21 January 2013 - 09:30 PM.

  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.


#31 Yannbane

Yannbane

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 238 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, PL/SQL, Lisp, Assembly, Bash, Others
  • Learning:Lisp, Scheme

Posted 26 January 2013 - 03:34 AM

@Zer033x: I guess it was a misunderstanding. I didn't really cover pointer arithmetic here, so I meant that the exact example I have shown will not compile.

I see your point though, I could've lead people to believe that it is not possible to do pointer arithmetic, I'll edit that part out.
  • 0

My blog: yannbane.com. I post about programming, game development, and artificial intelligence.






Also tagged with one or more of these keywords: pointers, objects, variables, memory, arrays, arithmetic, c++, c, address