• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 11:40
CEST 17:40
KST 00:40
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Code S Season 1 - RO8 Preview4[ASL21] Ro8 Preview Pt2: Progenitors8Code S Season 1 - RO12 Group A: Rogue, Percival, Solar, Zoun13[ASL21] Ro8 Preview Pt1: Inheritors16[ASL21] Ro16 Preview Pt2: All Star10
Community News
Maestros of The Game 2 announcement and schedule !7Weekly Cups (April 27-May 4): Clem takes triple0RSL Revival: Season 5 - Qualifiers and Main Event12Code S Season 1 (2026) - RO12 Results12026 GSL Season 1 Qualifiers25
StarCraft 2
General
Code S Season 1 - RO8 Preview Behind the Blue - Team Liquid History Book Weekly Cups (April 27-May 4): Clem takes triple Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Code S Season 1 (2026) - RO12 Results
Tourneys
Maestros of The Game 2 announcement and schedule ! GSL Code S Season 1 (2026) Sea Duckling Open (Global, Bronze-Diamond) RSL Revival: Season 5 - Qualifiers and Main Event Sparkling Tuna Cup - Weekly Open Tournament
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players
External Content
Mutation # 524 Death and Taxes The PondCast: SC2 News & Results Mutation # 523 Firewall Mutation # 522 Flip My Base
Brood War
General
Do we have a pimpest plays list? Why there arent any 256x256 pro maps? BGH Auto Balance -> http://bghmmr.eu/ (Spoiler) Asl ro8 D winner interview BW General Discussion
Tourneys
Escore Tournament StarCraft Season 2 [ASL21] Ro8 Day 4 Small VOD Thread 2.0 [BSL22] RO16 Group Stage - 02 - 10 May
Strategy
Simple Questions, Simple Answers Fighting Spirit mining rates What's the deal with APM & what's its true value Any training maps people recommend?
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile Nintendo Switch Thread OutLive 25 (RTS Game) Dawn of War IV
Dota 2
The Story of Wings Gaming
League of Legends
G2 just beat GenG in First stand
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
European Politico-economics QA Mega-thread The Letting Off Steam Thread US Politics Mega-thread Canadian Politics Mega-thread Russo-Ukrainian War Thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread McBoner: A hockey love story Formula 1 Discussion
World Cup 2022
Tech Support
streaming software Strange computer issues (software) [G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
How EEG Data Can Predict Gam…
TrAiDoS
ramps on octagon
StaticNine
Funny Nicknames
LUCKY_NOOB
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1720 users

The Big Programming Thread - Page 193

Forum Index > General Forum
Post a Reply
Prev 1 191 192 193 194 195 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
tofucake
Profile Blog Joined October 2009
Hyrule19210 Posts
November 14 2012 23:20 GMT
#3841
VHDL is literally Hitler
Liquipediaasante sana squash banana
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
November 14 2012 23:28 GMT
#3842
On November 15 2012 08:20 tofucake wrote:
VHDL is literally Hitler

+1. The only thing worse than VHDL is the software used to write ti.
Any sufficiently advanced technology is indistinguishable from magic
dannystarcraft
Profile Blog Joined October 2011
United States179 Posts
November 14 2012 23:37 GMT
#3843
I accidentally forgot to deallocate some memory in some C code that I was running for CFD.

Needless to say, I was wondering why my console program was magically crashing! Turns out I had run out of physical memory. After deallocating, I am running under 4 MB of memory easy. I would say a vast improvement! ^^
Craton
Profile Blog Joined December 2009
United States17281 Posts
November 15 2012 00:23 GMT
#3844
On November 15 2012 02:43 white_horse wrote:
Show nested quote +
On November 12 2012 14:24 ninjamyst wrote:
I am currently a Software Architect at a mid-size Healthcare IT firm. Straight out of college, we pay $55-$60k. 2-3 years of experience will get you $75-$85k. 4-6 years of experience, you are looking at $90-$110k. That doesn't include year-end bonus of $5-$20k. This is in a major city, and we primarily use Microsoft technology....so C#, ASP.NET, SQL Server 2008, etc. We don't care if you memorized definitions, and we dont give you stupid brain teasers during our interviews. We hire based on your problem solving skills and your object oriented design experiences. We will ask you to take a simple every day concept and break it down into classes/methods/properties. For example, how would you represent a baseball game in code? We dont hire specifically for front end vs database vs business logic. As part of our small development team, you will do all of those things. You will learn how to create GUI, how to create business objects, how to maintain databases.

For those of you in college now, the best way to impress is to be involved in side projects outside of school. Most schools don't even teach C# or PHP or Ruby; they only teach Java or C++. When we look at college resumes, everyone takes the same classes, do the same senior projects, and etc. We look for people that wrote their own mobile app, engaged in engineering competitions, did side projects for fun, learned a language outside of class, and etc.

Most importantly, we look for people with strong communication skills. Too often, we get people obsessed with technical stuff only, and they lack social skills. In a team environment, you need to be able to express your ideas to your peers. In design sessions, you need to be able to clearly explain your logic and your reasoning. If we can't understand you, we can't hire you even if you can create the fastest algorithm.


How hard is it to learn SQL, PHP, C# after you learn C++? I'm an intermediate c++ programmer. It looks like it would be easier since you know a little bit about programming already.

Each successive language is easier to learn than its predecessor because you know more fundamentals / core concepts and have a better understanding of how things work. There is no way for someone else to quantify how hard it will be for you to learn a new language.

Just run through various tutorials and then start doing personal projects using the different technologies.
twitch.tv/cratonz
Nausea
Profile Joined October 2010
Sweden807 Posts
Last Edited: 2012-11-15 02:01:51
November 15 2012 01:51 GMT
#3845
Hey, I got a big problem. If anyone know why this is happening, please let me know.
I´m passing a pointer to a function and for some reason when inside the function, it got the wrong address. How is this even possible?

http://i.imgur.com/SaWNt.jpg
http://i.imgur.com/p5JGl.jpg

The program works, but getting real undefined behavior.

Hope someone can help, thanks.
Set it ablaze!
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
November 15 2012 02:07 GMT
#3846
On November 15 2012 10:51 Nausea wrote:
Hey, I got a big problem. If anyone know why this is happening, please let me know.
I´m passing a pointer to a function and for some reason when inside the function, it got the wrong address. How is this even possible?

http://i.imgur.com/SaWNt.jpg
http://i.imgur.com/p5JGl.jpg

The program works, but getting real undefined behavior.

Hope someone can help, thanks.

You are probably running debugger in release mode with optimizations on.
Nausea
Profile Joined October 2010
Sweden807 Posts
November 15 2012 02:12 GMT
#3847
Compiling as debug so I think the debugger would do the same? This problem has the effect that I can´t get any info from the object the pointer points to
Set it ablaze!
cowsrule
Profile Joined February 2010
United States80 Posts
Last Edited: 2012-11-15 08:15:16
November 15 2012 08:08 GMT
#3848
On November 15 2012 11:07 CecilSunkure wrote:
Show nested quote +
On November 15 2012 10:51 Nausea wrote:
Hey, I got a big problem. If anyone know why this is happening, please let me know.
I´m passing a pointer to a function and for some reason when inside the function, it got the wrong address. How is this even possible?

http://i.imgur.com/SaWNt.jpg
http://i.imgur.com/p5JGl.jpg

The program works, but getting real undefined behavior.

Hope someone can help, thanks.

You are probably running debugger in release mode with optimizations on.


There's unfortunately a lot of potential problems here. Is the pointer pointing to the correct location before going into the function call? Inside the function, does the pointer point to garbage, something on the heap, a constant, something on the stack?

Can you post a bit more code beyond the function signatures? [Edit: Specifically around the object init and calling of the functions]

If the problem ends up being stack corruption, it would be quite difficult to track down without looking through the debugger itself.
Abductedonut
Profile Blog Joined December 2010
United States324 Posts
Last Edited: 2012-11-15 09:20:39
November 15 2012 08:46 GMT
#3849
On November 15 2012 10:51 Nausea wrote:
Hey, I got a big problem. If anyone know why this is happening, please let me know.
I´m passing a pointer to a function and for some reason when inside the function, it got the wrong address. How is this even possible?

http://i.imgur.com/SaWNt.jpg
http://i.imgur.com/p5JGl.jpg

The program works, but getting real undefined behavior.

Hope someone can help, thanks.


HAHAHAHA, I knew someone would eventually run into this problem. C++ is the most trash language in the history of trash languages, and now I shall show you why. Observe:

Code #1:
+ Show Spoiler +


#include <cstdlib>
#include <iostream>
#include <stdio.h>

class kitty
{
int cppisTrash;
};

void kittyKiller(kitty *kittyBeingKilled);

using namespace std;

int main(int argc, char *argv[])
{
kitty *kittiesAreAwesome;
printf("Pointer to kitty: %p \n",kittiesAreAwesome);
printf("Address of kitty: %p \n",&kittiesAreAwesome);
kittyKiller(kittiesAreAwesome);
system("PAUSE");
return EXIT_SUCCESS;
}

void kittyKiller(kitty *kittyBeingKilled)
{
printf("Entered function! \n");
printf("Address of kitty: %p \n",&kittyBeingKilled);
printf("Pointer to kitty: %p \n",kittyBeingKilled);
}



Results:
+ Show Spoiler +

Pointer to kitty: 75F59E34
Address of kitty: 0028FF44
Entered function!
Address of kitty: 0028FF20
Pointer to kitty: 75F59E34


Modified Code #2:
+ Show Spoiler +


#include <cstdlib>
#include <iostream>
#include <stdio.h>

class kitty
{
int cppisTrash;
};

void kittyKiller(kitty &kittyBeingKilled);

using namespace std;

int main(int argc, char *argv[])
{
kitty kittiesAreAwesome;
printf("Address of kitty: %p \n",&kittiesAreAwesome);
kittyKiller(kittiesAreAwesome);
system("PAUSE");
return EXIT_SUCCESS;
}

void kittyKiller(kitty &kittyBeingKilled)
{
printf("Entered function! \n");
printf("Address of kitty: %p \n",&kittyBeingKilled);
}



Results:
+ Show Spoiler +

Address of kitty: 0028FF44
Entered function!
Address of kitty: 0028FF44


Thehehehe. Can you figure out what went wrong! Ruh roh!

*EDIT*

Okay... I feel bad for those less-experienced programmers who are going to look at my post and go "I don't get at all what's happening"...

Here's the deal:
Primitive types that C++ derives from C such as int,char,float etc etc... and ARRAYS/POINTERS to these primitive types always pass the ADDRESS of the variable, which means regardless of whether your function signature uses * or &, the changes occurring inside the function will remain permanent.
Example:
+ Show Spoiler +

#include <cstdlib>
#include <iostream>
#include <stdio.h>

void func( int *myInt );

using namespace std;

int main(int argc, char *argv[])
{
int *myAwesomeInt = new int[1];
myAwesomeInt[0] = 0;
func(myAwesomeInt);
printf("Value of myAwesomeInt: %i \n",myAwesomeInt[0]);
system("pause");
}

void func( int *myInt )
{
*myInt = 1;
}


That code works like you would expect it to. myAwesomeInt prints out as 1. However, for C++ classes, this is not the case. If you supply to a function a pointer to an object, the changes made to that object get reverted once you exit the function. To make matters more confusing, STRUCTS in C work exactly you would expect them to. In that regard, C++ is inconsistent. I can bag on C++ all day, but hopefully you guys can see how stupid this is. Topple on the confusion of many inexperienced programmers thinking that strings are primitive types, and you've got a very shitty starting programming experience.

Working C code for C "classes":
+ Show Spoiler +

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

// The real way to declare a "class" in C. Note, C programmers don't need to cruch of
// public,private,friends... etc to protect their objects from getting messed up.
typedef struct
{
int kittyInt;
}kitty;

void kittyKiller(kitty*); //We don't have to give it a name in the declaration!

int main(int argc, char *argv[])
{
kitty *myAwesomeKitty = malloc(sizeof(kitty)*1); //malloc is the not-trash version of new...
myAwesomeKitty[0].kittyInt = 0;
kittyKiller(myAwesomeKitty);
printf("myAwesomeKitty kittyInt Value: %i \n",myAwesomeKitty[0].kittyInt);
system("PAUSE");
return 0;
}

void kittyKiller(kitty* myAwesomeKitty)
{
// For the love of dear god.. do not ever pass a pointer to an array without a size.
// I do this simply as a demonstration, keeping things simple.
myAwesomeKitty[0].kittyInt = 1;
}

cowsrule
Profile Joined February 2010
United States80 Posts
November 15 2012 08:59 GMT
#3850
On November 15 2012 17:46 Abductedonut wrote:
Show nested quote +
On November 15 2012 10:51 Nausea wrote:
Hey, I got a big problem. If anyone know why this is happening, please let me know.
I´m passing a pointer to a function and for some reason when inside the function, it got the wrong address. How is this even possible?

http://i.imgur.com/SaWNt.jpg
http://i.imgur.com/p5JGl.jpg

The program works, but getting real undefined behavior.

Hope someone can help, thanks.


HAHAHAHA, I knew someone would eventually run into this problem. C++ is the most trash language in the history of trash languages, and now I shall show you why. Observe:

Code #1:
+ Show Spoiler +


#include <cstdlib>
#include <iostream>
#include <stdio.h>

class kitty
{
int cppisTrash;
};

void kittyKiller(kitty *kittyBeingKilled);

using namespace std;

int main(int argc, char *argv[]
{
kitty *kittiesAreAwesome;
printf("Pointer to kitty: %p \n",kittiesAreAwesome);
printf("Address of kitty: %p \n",&kittiesAreAwesome);
kittyKiller(kittiesAreAwesome);
system("PAUSE");
return EXIT_SUCCESS;
}

void kittyKiller(kitty *kittyBeingKilled)
{
printf("Entered function! \n");
printf("Address of kitty: %p \n",&kittyBeingKilled);
printf("Pointer to kitty: %p \n",kittyBeingKilled);
}



Results:
+ Show Spoiler +

Pointer to kitty: 75F59E34
Address of kitty: 0028FF44
Entered function!
Address of kitty: 0028FF20
Pointer to kitty: 75F59E34


Modified Code #2:
+ Show Spoiler +


#include <cstdlib>
#include <iostream>
#include <stdio.h>

class kitty
{
int cppisTrash;
};

void kittyKiller(kitty &kittyBeingKilled);

using namespace std;

int main(int argc, char *argv[]
{
kitty kittiesAreAwesome;
printf("Address of kitty: %p \n",&kittiesAreAwesome);
kittyKiller(kittiesAreAwesome);
system("PAUSE");
return EXIT_SUCCESS;
}

void kittyKiller(kitty &kittyBeingKilled)
{
printf("Entered function! \n");
printf("Address of kitty: %p \n",&kittyBeingKilled);
}



Results:
+ Show Spoiler +

Address of kitty: 0028FF44
Entered function!
Address of kitty: 0028FF44


Thehehehe. Can you figure out what went wrong! Ruh roh!


Care to elaborate on why those examples demonstrate the language is crap? I'd be interested in hearing that perspective based around the existence of pointers and references.
Tobberoth
Profile Joined August 2010
Sweden6375 Posts
November 15 2012 09:13 GMT
#3851
On November 15 2012 02:43 white_horse wrote:
How hard is it to learn SQL, PHP, C# after you learn C++? I'm an intermediate c++ programmer. It looks like it would be easier since you know a little bit about programming already.

I would think a C++ programmer would have no problem learning PHP and C#, though they might write bad code in those languages because the way you code in C++ compared to PHP and C# is very different.

SQL is a different story though since it isn't a programming language, and you have to think completely differently when writing SQL than when writing programs. For example, using loops etc is generally a bad idea in SQL, but if you come from a programming background, constructs such as loops might come to you more easily than the query solutions (which are far simpler and faster if you know them).
Abductedonut
Profile Blog Joined December 2010
United States324 Posts
November 15 2012 09:33 GMT
#3852
On November 15 2012 17:59 cowsrule wrote:
Care to elaborate on why those examples demonstrate the language is crap? I'd be interested in hearing that perspective based around the existence of pointers and references.


Sure! I updated my post actually explaining the error instead of letting people figure it out for themselves for the newer programmers who come to this thread looking for advice/etc.

Basically the idea is this. When you're learning C++ from the beginning, you're told that passing by reference (the &) for primitive types (int/char/float/double) is one way to make sure that you do not pass a copy of the variable to a function. That way, changes inside that function will actually change the variable permanently.

Later on, you're introduced to pointers (the *) and you're told that passing by reference is really just pointers "in the background." And if you pass pointers (again, the *) of primitive types to a function, then the changes remain permanent.

Now objects come along. Well, if I pass to a function a pointer to an object, shouldn't the changes I make to the object be permanent? That only makes sense, since passing pointers to primitives types made the changes permanent. However, that's not the case. Passing a pointer of an object passes that pointer by value (which does not even make sense).

To make matters worse, structs are treated differently.

So C++ in inconsistent in that sense. Why are my pointers to my objects being passed by value?

Once you learn that, you "know" how to get around it but it still does not make logical sense. With C - C "classes" and primitive types are all treated the same way. You pass a pointer to a "class" or a primitive type, the changes are permanent.
mxw
Profile Joined November 2012
Germany1 Post
November 15 2012 09:44 GMT
#3853
On November 15 2012 17:46 Abductedonut wrote:
...


There's no argueing about C++ being confusing, but in my opinion this makes it interesting and challenging.
There are so many ways of doing stuff with slight distinctions being a huge deal.


C++ was my first language, together with Visual Studio, a vast amount of libraries and ASM I think it's the most powerful tool. Powerful yet time intensive.
At university we have to use Java and it's no doubt useful to have your code being able to run everywhere but when coding the exercises it feels like my hands are tight.
Tobberoth
Profile Joined August 2010
Sweden6375 Posts
November 15 2012 09:50 GMT
#3854
On November 15 2012 18:33 Abductedonut wrote:
Show nested quote +
On November 15 2012 17:59 cowsrule wrote:
Care to elaborate on why those examples demonstrate the language is crap? I'd be interested in hearing that perspective based around the existence of pointers and references.


Sure! I updated my post actually explaining the error instead of letting people figure it out for themselves for the newer programmers who come to this thread looking for advice/etc.

Basically the idea is this. When you're learning C++ from the beginning, you're told that passing by reference (the &) for primitive types (int/char/float/double) is one way to make sure that you do not pass a copy of the variable to a function. That way, changes inside that function will actually change the variable permanently.

Later on, you're introduced to pointers (the *) and you're told that passing by reference is really just pointers "in the background." And if you pass pointers (again, the *) of primitive types to a function, then the changes remain permanent.

Now objects come along. Well, if I pass to a function a pointer to an object, shouldn't the changes I make to the object be permanent? That only makes sense, since passing pointers to primitives types made the changes permanent. However, that's not the case. Passing a pointer of an object passes that pointer by value (which does not even make sense).

To make matters worse, structs are treated differently.

So C++ in inconsistent in that sense. Why are my pointers to my objects being passed by value?

Once you learn that, you "know" how to get around it but it still does not make logical sense. With C - C "classes" and primitive types are all treated the same way. You pass a pointer to a "class" or a primitive type, the changes are permanent.

Wait, are you saying objects are passed by value by default in C++? (I haven't programmed C++ in a very long time). It seems to be logical that if you pass an object to a function, you would pass the pointer by value but that would be the same as passing the "object" by reference, so changes should be permanent. It seems weird to me that C++ would copy the whole object by default, just because a pointer to the object was sent by value (especially since it doesn't work like that in C#).
heishe
Profile Blog Joined June 2009
Germany2284 Posts
Last Edited: 2012-11-15 10:07:38
November 15 2012 10:06 GMT
#3855
On November 15 2012 18:50 Tobberoth wrote:
Show nested quote +
On November 15 2012 18:33 Abductedonut wrote:
On November 15 2012 17:59 cowsrule wrote:
Care to elaborate on why those examples demonstrate the language is crap? I'd be interested in hearing that perspective based around the existence of pointers and references.


Sure! I updated my post actually explaining the error instead of letting people figure it out for themselves for the newer programmers who come to this thread looking for advice/etc.

Basically the idea is this. When you're learning C++ from the beginning, you're told that passing by reference (the &) for primitive types (int/char/float/double) is one way to make sure that you do not pass a copy of the variable to a function. That way, changes inside that function will actually change the variable permanently.

Later on, you're introduced to pointers (the *) and you're told that passing by reference is really just pointers "in the background." And if you pass pointers (again, the *) of primitive types to a function, then the changes remain permanent.

Now objects come along. Well, if I pass to a function a pointer to an object, shouldn't the changes I make to the object be permanent? That only makes sense, since passing pointers to primitives types made the changes permanent. However, that's not the case. Passing a pointer of an object passes that pointer by value (which does not even make sense).

To make matters worse, structs are treated differently.

So C++ in inconsistent in that sense. Why are my pointers to my objects being passed by value?

Once you learn that, you "know" how to get around it but it still does not make logical sense. With C - C "classes" and primitive types are all treated the same way. You pass a pointer to a "class" or a primitive type, the changes are permanent.

Wait, are you saying objects are passed by value by default in C++? (I haven't programmed C++ in a very long time). It seems to be logical that if you pass an object to a function, you would pass the pointer by value but that would be the same as passing the "object" by reference, so changes should be permanent. It seems weird to me that C++ would copy the whole object by default, just because a pointer to the object was sent by value (especially since it doesn't work like that in C#).


No, he just doesn't understand how C++ works. The things he complains about make perfect sense and follow some very simple consistent rules. The behavior when passing references in C++ is exactly the same as passing references in most other languages. And pointers are just references that you can change.
If you value your soul, never look into the eye of a horse. Your soul will forever be lost in the void of the horse.
Abductedonut
Profile Blog Joined December 2010
United States324 Posts
Last Edited: 2012-11-15 10:20:28
November 15 2012 10:15 GMT
#3856
On November 15 2012 18:50 Tobberoth wrote:
Show nested quote +
On November 15 2012 18:33 Abductedonut wrote:
On November 15 2012 17:59 cowsrule wrote:
Care to elaborate on why those examples demonstrate the language is crap? I'd be interested in hearing that perspective based around the existence of pointers and references.


Sure! I updated my post actually explaining the error instead of letting people figure it out for themselves for the newer programmers who come to this thread looking for advice/etc.

Basically the idea is this. When you're learning C++ from the beginning, you're told that passing by reference (the &) for primitive types (int/char/float/double) is one way to make sure that you do not pass a copy of the variable to a function. That way, changes inside that function will actually change the variable permanently.

Later on, you're introduced to pointers (the *) and you're told that passing by reference is really just pointers "in the background." And if you pass pointers (again, the *) of primitive types to a function, then the changes remain permanent.

Now objects come along. Well, if I pass to a function a pointer to an object, shouldn't the changes I make to the object be permanent? That only makes sense, since passing pointers to primitives types made the changes permanent. However, that's not the case. Passing a pointer of an object passes that pointer by value (which does not even make sense).

To make matters worse, structs are treated differently.

So C++ in inconsistent in that sense. Why are my pointers to my objects being passed by value?

Once you learn that, you "know" how to get around it but it still does not make logical sense. With C - C "classes" and primitive types are all treated the same way. You pass a pointer to a "class" or a primitive type, the changes are permanent.

Wait, are you saying objects are passed by value by default in C++? (I haven't programmed C++ in a very long time). It seems to be logical that if you pass an object to a function, you would pass the pointer by value but that would be the same as passing the "object" by reference, so changes should be permanent. It seems weird to me that C++ would copy the whole object by default, just because a pointer to the object was sent by value (especially since it doesn't work like that in C#).


No, that's not what I'm saying. Look, the POINTER ITSELF is passed by value. So modifications to the POINTER ( if the pointer is pointing to the object that originally called the function, changes WILL remain permanent in the object ). However, any changes made to the pointer will not be changed. Let me provide an example.

Example:
+ Show Spoiler +

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class kitty
{
public:
int kittySize;
kitty();
kitty (const kitty& myKitty);
};

kitty::kitty ()
{
kittySize = 10;
}

kitty::kitty (const kitty& myKitty)
{
kittySize = -10;
}

void func(kitty*);

int main(int argc, char *argv[])
{
kitty *myAwesomeKitty = new kitty[1];
myAwesomeKitty->kittySize = 0;
func(myAwesomeKitty);
// Should this print as 0 or 10?
cout<<myAwesomeKitty->kittySize;
system("pause");
}

void func( kitty *myAwesomeKitty )
{
kitty myKitty;
myAwesomeKitty = &myKitty;
myAwesomeKitty->kittySize = 10;
}


Note in this case.. kittySize WILL BE PRINTED AS 0! Because we changed the VALUE of the pointer, any changes made to the pointers contents were discarded. Do you see how confusing this can get?

*edit* Going to bed for now... I could see how my other post is confusing. No, the changes to the object itself are preserved if you DO NOT modify the value of the pointer. But any changes made to the pointer are discarded because the pointer itself is being passed by value

Also, to the poster above me... thanks. I obviously don't know C++ when I pointed out the mistake of the original guy I quoted and proceeded to explain stuff to others. It's pretty easy to come in here and say "he doesn't know what he's talking about." If you're so great at C++ why don't you explain things?
Schokomuesli
Profile Joined November 2010
Germany12 Posts
November 15 2012 10:18 GMT
#3857
Thanks heishe, I actually was really doubting myself for a moment there ;-) I was pretty sure the guy was just ranting but with C++ one oftentimes can't be sure if the other guy ist just way more brilliant than you are or just doesn't know what he is talking about...

C++ definitely has some major flaws (for one: it's way too big, especially considering C++11) but this is not one of them.
mcc
Profile Joined October 2010
Czech Republic4646 Posts
November 15 2012 10:23 GMT
#3858
On November 15 2012 18:33 Abductedonut wrote:
Show nested quote +
On November 15 2012 17:59 cowsrule wrote:
Care to elaborate on why those examples demonstrate the language is crap? I'd be interested in hearing that perspective based around the existence of pointers and references.


Sure! I updated my post actually explaining the error instead of letting people figure it out for themselves for the newer programmers who come to this thread looking for advice/etc.

Basically the idea is this. When you're learning C++ from the beginning, you're told that passing by reference (the &) for primitive types (int/char/float/double) is one way to make sure that you do not pass a copy of the variable to a function. That way, changes inside that function will actually change the variable permanently.

Later on, you're introduced to pointers (the *) and you're told that passing by reference is really just pointers "in the background." And if you pass pointers (again, the *) of primitive types to a function, then the changes remain permanent.

Now objects come along. Well, if I pass to a function a pointer to an object, shouldn't the changes I make to the object be permanent? That only makes sense, since passing pointers to primitives types made the changes permanent. However, that's not the case. Passing a pointer of an object passes that pointer by value (which does not even make sense).

To make matters worse, structs are treated differently.

So C++ in inconsistent in that sense. Why are my pointers to my objects being passed by value?

Once you learn that, you "know" how to get around it but it still does not make logical sense. With C - C "classes" and primitive types are all treated the same way. You pass a pointer to a "class" or a primitive type, the changes are permanent.

It makes complete sense if you know what it actually means. Of course the pointer to an object is passed by vlaue, that is completely consistent with how primitive types are passed by reference. func(X *bla) means that pointer is passed to a function by value no matter what X is. And changes to the dereferenced object are permanent in case of primitive types and classes. What you cannot change is the pointer itself, but again that is completely consistent. So you cannot do inside the function : bla = new X(); and expect the change to be permanent as you are trying to change the pointer. If X is int, you also cannot do: int y = 2; bla = &y; and expect it to be permanent. But you can do *bla = 1 or in case of X being a class bla->property = 1; and the changes will be permanent.

C++ has some ugly aspects, but what you are saying is not one of them.
mcc
Profile Joined October 2010
Czech Republic4646 Posts
Last Edited: 2012-11-15 10:53:14
November 15 2012 10:26 GMT
#3859
On November 15 2012 19:15 Abductedonut wrote:
Show nested quote +
On November 15 2012 18:50 Tobberoth wrote:
On November 15 2012 18:33 Abductedonut wrote:
On November 15 2012 17:59 cowsrule wrote:
Care to elaborate on why those examples demonstrate the language is crap? I'd be interested in hearing that perspective based around the existence of pointers and references.


Sure! I updated my post actually explaining the error instead of letting people figure it out for themselves for the newer programmers who come to this thread looking for advice/etc.

Basically the idea is this. When you're learning C++ from the beginning, you're told that passing by reference (the &) for primitive types (int/char/float/double) is one way to make sure that you do not pass a copy of the variable to a function. That way, changes inside that function will actually change the variable permanently.

Later on, you're introduced to pointers (the *) and you're told that passing by reference is really just pointers "in the background." And if you pass pointers (again, the *) of primitive types to a function, then the changes remain permanent.

Now objects come along. Well, if I pass to a function a pointer to an object, shouldn't the changes I make to the object be permanent? That only makes sense, since passing pointers to primitives types made the changes permanent. However, that's not the case. Passing a pointer of an object passes that pointer by value (which does not even make sense).

To make matters worse, structs are treated differently.

So C++ in inconsistent in that sense. Why are my pointers to my objects being passed by value?

Once you learn that, you "know" how to get around it but it still does not make logical sense. With C - C "classes" and primitive types are all treated the same way. You pass a pointer to a "class" or a primitive type, the changes are permanent.

Wait, are you saying objects are passed by value by default in C++? (I haven't programmed C++ in a very long time). It seems to be logical that if you pass an object to a function, you would pass the pointer by value but that would be the same as passing the "object" by reference, so changes should be permanent. It seems weird to me that C++ would copy the whole object by default, just because a pointer to the object was sent by value (especially since it doesn't work like that in C#).


No, that's not what I'm saying. Look, the POINTER ITSELF is passed by value. So modifications to the POINTER ( if the pointer is pointing to the object that originally called the function, changes WILL remain permanent in the object ). However, any changes made to the pointer will not be changed. Let me provide an example.

Example:
+ Show Spoiler +

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class kitty
{
public:
int kittySize;
kitty();
kitty (const kitty& myKitty);
};

kitty::kitty ()
{
kittySize = 10;
}

kitty::kitty (const kitty& myKitty)
{
kittySize = -10;
}

void func(kitty*);

int main(int argc, char *argv[]
{
kitty *myAwesomeKitty = new kitty[1];
myAwesomeKitty->kittySize = 0;
func(myAwesomeKitty);
// Should this print as 0 or 10?
cout<<myAwesomeKitty->kittySize;
system("pause");
}

void func( kitty *myAwesomeKitty )
{
kitty myKitty;
myAwesomeKitty = &myKitty;
myAwesomeKitty->kittySize = 10;
}


Note in this case.. kittySize WILL BE PRINTED AS 0! Because we changed the VALUE of the pointer, any changes made to the pointers contents were discarded. Do you see how confusing this can get?

*edit* Going to bed for now... I could see how my other post is confusing. No, the changes to the object itself are preserved if you DO NOT modify the value of the pointer. But any changes made to the pointer are discarded because the pointer itself is being passed by value

Also, to the poster above me... thanks. I obviously don't know C++ when I pointed out the mistake of the original guy I quoted and proceeded to explain stuff to others. It's pretty easy to come in here and say "he doesn't know what he's talking about." If you're so great at C++ why don't you explain things?

Actually no, he is correct, you do not know how C++ works, or specifically you have rather bad understanding of pointers in general if that is what is confusing you, as that part of C++ is perfectly consistent and works exactly as one would expect if you understand how pointers work.

EDIT: and to explain things.
The rules are simple :
a) if you pass by value, no changes whatsoever to the parameters are permanent : so class x = new class(); int y = 5; and after f(x,y) is called no changes done in the function are retained.
b) if you pass by reference, changes to the memory pointed by the reference are permanent (except if you use const modifier), but any changes done to the pointer (reference) are discarded.
So class *x = new class(); int *y; *y = 5; , now if you inside f(x,y) do x->prop = 5; and *y = 6; changes will be retained. If you do x = new class(); and y = &some_int; changes will not be retained as you are trying to change the pointer and that is passed by value.

That is basically all, the rules are consistent and nearly everything can be deduced from it.

But of course there is a way to retain the changes as you can pass the pointer by pointer. You can have function f(class **x) now if you do class *y = new class(); f(&x) and in f you do *x = new class(), change to the pointer to the class will be retained.
Mstring
Profile Joined September 2011
Australia510 Posts
Last Edited: 2012-11-15 10:44:15
November 15 2012 10:32 GMT
#3860
On November 15 2012 19:15 Abductedonut wrote:
Show nested quote +
On November 15 2012 18:50 Tobberoth wrote:
On November 15 2012 18:33 Abductedonut wrote:
On November 15 2012 17:59 cowsrule wrote:
Care to elaborate on why those examples demonstrate the language is crap? I'd be interested in hearing that perspective based around the existence of pointers and references.


Sure! I updated my post actually explaining the error instead of letting people figure it out for themselves for the newer programmers who come to this thread looking for advice/etc.

Basically the idea is this. When you're learning C++ from the beginning, you're told that passing by reference (the &) for primitive types (int/char/float/double) is one way to make sure that you do not pass a copy of the variable to a function. That way, changes inside that function will actually change the variable permanently.

Later on, you're introduced to pointers (the *) and you're told that passing by reference is really just pointers "in the background." And if you pass pointers (again, the *) of primitive types to a function, then the changes remain permanent.

Now objects come along. Well, if I pass to a function a pointer to an object, shouldn't the changes I make to the object be permanent? That only makes sense, since passing pointers to primitives types made the changes permanent. However, that's not the case. Passing a pointer of an object passes that pointer by value (which does not even make sense).

To make matters worse, structs are treated differently.

So C++ in inconsistent in that sense. Why are my pointers to my objects being passed by value?

Once you learn that, you "know" how to get around it but it still does not make logical sense. With C - C "classes" and primitive types are all treated the same way. You pass a pointer to a "class" or a primitive type, the changes are permanent.

Wait, are you saying objects are passed by value by default in C++? (I haven't programmed C++ in a very long time). It seems to be logical that if you pass an object to a function, you would pass the pointer by value but that would be the same as passing the "object" by reference, so changes should be permanent. It seems weird to me that C++ would copy the whole object by default, just because a pointer to the object was sent by value (especially since it doesn't work like that in C#).


No, that's not what I'm saying. Look, the POINTER ITSELF is passed by value. So modifications to the POINTER ( if the pointer is pointing to the object that originally called the function, changes WILL remain permanent in the object ). However, any changes made to the pointer will not be changed. Let me provide an example.

Example:
+ Show Spoiler +

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class kitty
{
public:
int kittySize;
kitty();
kitty (const kitty& myKitty);
};

kitty::kitty ()
{
kittySize = 10;
}

kitty::kitty (const kitty& myKitty)
{
kittySize = -10;
}

void func(kitty*);

int main(int argc, char *argv[]
{
kitty *myAwesomeKitty = new kitty[1];
myAwesomeKitty->kittySize = 0;
func(myAwesomeKitty);
// Should this print as 0 or 10?
cout<<myAwesomeKitty->kittySize;
system("pause");
}

void func( kitty *myAwesomeKitty )
{
kitty myKitty;
myAwesomeKitty = &myKitty;
myAwesomeKitty->kittySize = 10;
}


Note in this case.. kittySize WILL BE PRINTED AS 0! Because we changed the VALUE of the pointer, any changes made to the pointers contents were discarded. Do you see how confusing this can get?


*shrugs*. All the examples you've shown so far displayed as I expected.

If I dived into a post-box and messed with the addresses, I wouldn't expect the intended recipients to receive the correct letters.

It's only confusing if you know that someone dives into your post-box to change the addresses but you still advertise that they're guaranteed to arrive correctly

If you really want to modify your pointer you can always pass by reference to pointer:

void func( kitty *&myAwesomeKitty )

You can be as indirect as you dare!

I think this falls under the category of "blow your foot off" in the old C vs C++ saying... xD
Prev 1 191 192 193 194 195 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 20m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Serral 3901
RotterdaM 278
TKL 124
StarCraft: Brood War
Britney 43302
Bisu 2169
EffOrt 906
Larva 558
firebathero 550
Hyuk 500
Stork 476
ggaemo 416
Mini 352
BeSt 304
[ Show more ]
actioN 267
ZerO 218
Soulkey 209
hero 185
Killer 177
Rush 167
Hyun 90
Pusan 82
Dewaltoss 75
Zeus 57
Sharp 57
Barracks 55
Backho 54
Sea.KH 48
ToSsGirL 32
soO 27
sorry 22
HiyA 20
Shine 19
scan(afreeca) 18
Rock 16
IntoTheRainbow 15
Terrorterran 13
GoRush 13
ajuk12(nOOB) 10
Noble 9
Sacsri 8
Dota 2
Gorgc4800
qojqva1982
syndereN480
monkeys_forever184
Counter-Strike
fl0m359
Heroes of the Storm
MindelVK1
Other Games
singsing2065
B2W.Neo1104
hiko838
Liquid`RaSZi758
FrodaN643
Beastyqt511
Lowko348
DeMusliM347
ceh9278
ArmadaUGS177
KnowMe176
Mew2King93
QueenE65
Trikslyr31
ZerO(Twitch)15
fpsfer 1
Organizations
Other Games
gamesdonequick1686
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 15 non-featured ]
StarCraft 2
• poizon28 25
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• HerbMon 20
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Nemesis3584
• TFBlade939
Other Games
• Shiphtur163
Upcoming Events
Big Brain Bouts
20m
Fjant vs Bly
Serral vs Shameless
OSC
6h 20m
The PiG Daily
7h 20m
Maru vs Rogue
TBD vs Classic
herO vs Solar
ByuN vs Solar
Replay Cast
8h 20m
CranKy Ducklings
18h 20m
RSL Revival
18h 20m
SHIN vs Bunny
ByuN vs Shameless
WardiTV Invitational
19h 20m
Krystianer vs TriGGeR
Cure vs Rogue
SC Evo League
21h 20m
uThermal 2v2 Circuit
23h 20m
BSL
1d 3h
Artosis vs TerrOr
spx vs StRyKeR
[ Show More ]
Replay Cast
1d 8h
Sparkling Tuna Cup
1d 18h
RSL Revival
1d 18h
Cure vs Zoun
Clem vs Lambo
WardiTV Invitational
1d 19h
BSL
2 days
Dewalt vs DragOn
Aether vs Jimin
GSL
2 days
Afreeca Starleague
2 days
Soma vs Leta
Wardi Open
2 days
Monday Night Weeklies
3 days
OSC
3 days
CranKy Ducklings
3 days
Afreeca Starleague
3 days
Light vs Flash
Replay Cast
4 days
Replay Cast
5 days
The PondCast
5 days
Replay Cast
6 days
RSL Revival
6 days
Liquipedia Results

Completed

Proleague 2026-05-05
WardiTV TLMC #16
Nations Cup 2026

Ongoing

BSL Season 22
ASL Season 21
CSL 2026 SPRING (S20)
IPSL Spring 2026
KCM Race Survival 2026 Season 2
Acropolis #4
Escore Tournament S2: W6
SCTL 2026 Spring
RSL Revival: Season 5
2026 GSL S1
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2

Upcoming

KK 2v2 League Season 1
BSL 22 Non-Korean Championship
YSL S3
Escore Tournament S2: W7
Escore Tournament S2: W8
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
Maestros of the Game 2
2026 GSL S2
BLAST Bounty Summer 2026: Closed Qualifier
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
IEM Atlanta 2026
Asian Champions League 2026
PGL Astana 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.