Raised This Month: $51 Target: $400
 12% 

C++ Questions


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-01-2016 , 13:17   C++ Questions
Reply With Quote #1

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.

Last edited by TheDS1337; 01-01-2016 at 13:18.
TheDS1337 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-01-2016 , 14:45   Re: C++ Questions
Reply With Quote #2

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;
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-01-2016 , 15:07   Re: C++ Questions
Reply With Quote #3

Quote:
Originally Posted by Powerlord View Post
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 View Post
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 ?

Last edited by TheDS1337; 01-01-2016 at 15:09.
TheDS1337 is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-01-2016 , 15:38   Re: C++ Questions
Reply With Quote #4

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.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 01-01-2016 at 15:39.
Powerlord is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 01-01-2016 , 15:42   Re: C++ Questions
Reply With Quote #5

Quote:
Originally Posted by DeagLe.Studio View Post
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 View Post
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 View Post
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 View Post
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
Miu is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-01-2016 , 15:43   Re: C++ Questions
Reply With Quote #6

Quote:
Originally Posted by Powerlord View Post
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 View Post
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!

Last edited by TheDS1337; 01-01-2016 at 16:02.
TheDS1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-01-2016 , 16:41   Re: C++ Questions
Reply With Quote #7

Quote:
Originally Posted by DeagLe.Studio View Post
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.
klippy is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-01-2016 , 18:01   Re: C++ Questions
Reply With Quote #8

I'll try it tomorrow since I am playing a little bit now, and I'll tell you man, thank you for your work!
TheDS1337 is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-01-2016 , 18:31   Re: C++ Questions
Reply With Quote #10

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.
TheDS1337 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:34.


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