AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Off-Topic (https://forums.alliedmods.net/forumdisplay.php?f=15)
-   -   C++ Questions (https://forums.alliedmods.net/showthread.php?t=277017)

TheDS1337 01-01-2016 13:17

C++ Questions
 
Hello :)

I started learning some C++ and I think that I have problem of either understanding the real meaning.

So my first question, I know pointers refers to variable address in memory like:

PHP Code:

int *pVar;
int var2 = *pVar// Gets the value and not the address of pVar. 

We can also do:
PHP Code:

int var;
int *pVar2 = &var; // Gets the address and not the value var. 

I got confused of when to use pointers ?

because when I checked some HL or HL2 SDK files, I found out that for example they declare Entity like this:
PHP Code:

class CBaseEntity {
// Members etc...
}

CBaseEntity *pEntity

What's the purpose of this ? why not like this ?
PHP Code:

class CBaseEntity {
// Members etc...
}

CBaseEntity Entity

And the second question is: when to use dynamic memory ?

What I understand is that we should use it when we have an Array and dont know it's size so we do for example like this:
PHP Code:

int i;

cout << "How many IDs you want ?" << endl;
cin >> i;

// Then we create our array with the dynamic selected size here
int *pArray = new[i];

// Afte that we destroy it when we finish
delete[] pArray

I can understand this but I dont get why would we do that with a normal variable and not an array for example doing this:
PHP Code:

int *= new(maybe even give it a value here);

// After that we destroy it when we finish
delete p

Instead of:
PHP Code:

int p// maybe even give it a value here

// Boom, p destroyed after the function returns the value. 

I see these two examples have the same meaning ? please explain to me I really want to know this so badly since it got me really confused.

Powerlord 01-01-2016 14:45

Re: C++ Questions
 
To answer the first question:

In C++, to do most meaningful things with objects, you must use pointers. This includes fancy things like calling constructors that take arguments.

This is because C++ uses the Resource Aquisition Is Initialization. That is, just declaring a variable of a class instantiates it using the no argument constructor. Unless it's a pointer.

It's also not possible to do Polymorphism in C++ without using pointers...

Code:

// This is always a CBaseEntity class
CBaseEntity Entity;

// This can be CBaseEntity or any of its children.
CBaseEntity *pEntity;


TheDS1337 01-01-2016 15:07

Re: C++ Questions
 
Quote:

Originally Posted by Powerlord (Post 2378797)
To answer the first question:

In C++, to do most meaningful things with objects, you must use pointers. This includes fancy things like calling constructors that take arguments.

Can you give me an example on this ? since I dont get you. do you mean things like virtual functions ?
Quote:

Originally Posted by Powerlord (Post 2378797)
Code:

// This is always a CBaseEntity class
CBaseEntity Entity;

// This can be CBaseEntity or any of its children.
CBaseEntity *pEntity;


if you mean the class members by children, cant we just access Entity by a . instead of -> since it's not a pointer ?

Powerlord 01-01-2016 15:38

Re: C++ Questions
 
When I said "or any of its children" I meant child classes.

Like how CBaseCombatWeapon or CBasePlayer are child classes of CBaseEntity.

Code:

CBaseEntity *pEntity;
pEntity = new CBaseCombatWeapon(args here);

is perfectly valid, although you can only call CBaseWeapon methods on pEntity unless you cast it.

Miu 01-01-2016 15:42

Re: C++ Questions
 
Quote:

Originally Posted by DeagLe.Studio (Post 2378776)
I got confused of when to use pointers ?

When you want to reference an object that already exists rather than create a new one, or when you want to pass an object that is larger than the pointer size to a function (e.g. passing a struct of 100 ints by-val would require you to copy 400 bytes, but passing it by-ref, with a pointer, only requires 4 bytes on x86, or 8 bytes on x86-64)

Quote:

Originally Posted by DeagLe.Studio (Post 2378776)
And the second question is: when to use dynamic memory ?

outside of dynamic arrays, it's used for memory that needs to survive beyond the current scope, e.g. you need to return an object, or for memory that is larger than the stack could contain, e.g. allocating 100 MB to store a large file entirely in memory

Quote:

Originally Posted by Powerlord (Post 2378797)
This includes fancy things like calling constructors that take arguments.

you can do this without pointers, e.g. std::string a("hello");

Quote:

Originally Posted by DeagLe.Studio (Post 2378799)
if you mean the class members by children, cant we just access Entity by a . instead of -> since it's not a pointer ?

by children he means other classes that derive from CBaseEntity, which can be referred to with a CBaseEntity pointer. if you then call a virtual method that the child class overrides, it'll be dispatched to the override belonging to the child class rather than CBaseEntity's implementation

TheDS1337 01-01-2016 15:43

Re: C++ Questions
 
Quote:

Originally Posted by Powerlord (Post 2378804)
When I said "or any of its children" I meant child classes.

Like how CBaseCombatWeapon or CBasePlayer are child classes of CBaseEntity.

Code:

CBaseEntity *pEntity;
pEntity = new CBaseCombatWeapon(args here);

is perfectly valid, although you can only call CBaseWeapon methods on pEntity unless you cast it.

Oh! that's something I didnt know, btw can you give examples like I said in the post above please ? and can you give me an idea about the Dynamic Memory ?

Quote:

Originally Posted by Miu (Post 2378806)
When you want to reference an object that already exists rather than create a new one, or when you want to pass an object that is larger than the pointer size to a function (e.g. passing a struct of 100 ints by-val would require you to copy 400 bytes, but passing it by-ref, with a pointer, only requires 4 bytes on x86, or 8 bytes on x86-64)



outside of dynamic arrays, it's used for memory that needs to survive beyond the current scope, e.g. you need to return an object, or for memory that is larger than the stack could contain, e.g. allocating 100 MB to store a large file entirely in memory



you can do this without pointers, e.g. std::string a("hello");



by children he means other classes that derive from CBaseEntity, which can be referred to with a CBaseEntity pointer. if you then call a virtual method that the child class overrides, it'll be dispatched to the override belonging to the child class rather than CBaseEntity's implementation

Can you give me some examples about the dynamic memory and the pointers please ? so I can understand it clearly, thank you!

klippy 01-01-2016 16:41

Re: C++ Questions
 
Quote:

Originally Posted by DeagLe.Studio (Post 2378807)
Can you give me some examples about the dynamic memory and the pointers please ? so I can understand it clearly, thank you!

This is probably a bad example, can't think of any good one right now, but you can try running this code:
PHP Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char *newString() {
    
char str[64];
    for (
int i 063i++) {
        
str[i] = 'A' + (rand() % ('Z' 'A'));
    }
    
str[63] = '\0';

    return 
str;
}

char *newString2() {
    
char *str = new char[64];
    for (
int i 063i++) {
        
str[i] = 'A' + (rand() % ('Z' 'A'));
    }
    
str[63] = '\0';

    return 
str;
}


int main(int argcchar *argv[]) {
    
srand(time(NULL));
    
    
char *str newString();
    
char *str2 newString2();

    
printf(str);
    
printf("\n\n");
    
printf(str2);
    
printf("\n\n");

    
delete str2;

    return 
0;


This one shows what Miu meant by
Quote:

it's used for memory that needs to survive beyond the current scope.
You aren't able to return address of a variable allocated on the stack (local variables) (even the compiler should warn you about this; the behavior is undefined), so that's why we in the second function allocate array on the heap (dynamic memory allocation). That way, it persists until it is freed using delete.
Also, if you don't free dynamically allocated memory, you get the so-called memory leak (I am certain you've heard of it).
PHP Code:

int *arr = new int[1337];
arr nullptr

The code above would produce a memory leak, because by doing arr = nullptr;, we just lost all references to the allocated memory block, meaning that we would never be able to free the allocated memory.

TheDS1337 01-01-2016 18:01

Re: C++ Questions
 
I'll try it tomorrow since I am playing a little bit now, and I'll tell you man, thank you for your work!

addons_zz 01-01-2016 18:09

Re: C++ Questions
 
Try to see these pages:
  1. http://www.cplusplus.com/doc/tutorial/pointers/
  2. http://www.cplusplus.com/doc/tutorial/dynamic/
  3. http://www.drbio.cornell.edu/pl47/programming/TICPP-2nd-ed-Vol-one-html/Chapter11.html
  4. http://programmers.stackexchange.com/questions/126895/need-help-understanding-reference-operatorc-in-specific-functions
  5. http://os.camden.rutgers.edu/c_resources/c_manual/C/CONCEPT/storage_class.html

TheDS1337 01-01-2016 18:31

Re: C++ Questions
 
z

I saw these, these are just a definition tbh, I understand what a pointer is but I dont understand what is the point of using it if you know whay I mean, we can simple get adress of variable by using & operator instead of make in it a pointer, I mean we can convert anything we want.


All times are GMT -4. The time now is 08:50.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.