Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

I'm learning c++

Name: Anonymous 2010-09-14 8:24

And my compiler is letting me do something weird.


If I do this:

int x; //matrix-size

cin >> x;

int array[x];

I can compile. I don't even get a warning. (I'm using the Codelite IDE for Linux, compiler is g++)

Does this work in c++ or is my compiler just fucking with me?

Name: Anonymous 2010-09-14 8:31

Since you told the compiler what x would be used for with the // preprocessor directive (//matrix-size) it knows what to do with it when you give x to it.

Name: Anonymous 2010-09-14 8:42

It can likely allocate stack space anyway in the function unlike C, thus this is allowed.

Name: Anonymous 2010-09-14 8:52

I guess I could maybe put in a few lines with sizeof at the end of the program to output whether the array is actually x places long, or if it just assigned some random value to x on compile.

Name: Anonymous 2010-09-14 9:02

You could try learning C++.

Name: Anonymous 2010-09-14 9:07

Use a language where you can count on the compiler to not silently do the wrong thing.

Name: Anonymous 2010-09-14 9:29

>> 5: I have no idea if you're trying to be funny or what. I *am* learning C++, and I'm wondering why my compiler is letting me do this thing that would get me shot if I did it in C.

>> 6: No, that would be the smart and logical thing to do. This is school, so we're using C++ or failing.

Name: Sensei 2010-09-14 9:30

>>4
This is the wrong approach to learning about the language and your compiler. Drafts of the C++ standard are available for free, which you can read to find out if variable length arrays are permitted in vanilla C++ (they're not). GCC has documentation that lists its extensions to C++, all of which are enabled by default (variable length array support is one of them, see http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html).

Name: Anonymous 2010-09-14 9:31

I wish my C++ compiler could do this.

Name: Anonymous 2010-09-14 9:37

>>4
sizeof with arrays will likely only work if the array is not variable length. You'd probably just get back the size of a pointer.

Name: Anonymous 2010-09-14 14:52

>>1
No, this should not work.  Most likely, your code isn't even being compiled.  For example, this is in a file that isn't added to your makefile, etc...  You didn't paste the whole function, so we can't tell if this is in main() or somewhere else.

Name: Ren 2010-09-14 17:02

http://www.informit.com/articles/article.aspx?p=31783&seqNum=7

Still, I'm more in favor of a

int *array = malloc(sizeof(int) * x);

But I'm oldfashioned like that.

Name: Anonymous 2010-09-14 17:48

>>12
FTFY
int *a = malloc(sizeof(*a) * x);

Name: Anonymous 2010-09-14 18:20

int array[x] is valid C.

Name: Anonymous 2010-09-14 18:41

>>13
wat

Name: Anonymous 2010-09-14 21:26

>>15
learn decent C

Name: Anonymous 2010-09-15 0:40

>>14
C99 isn't C. It's --C++.

Name: Anonymous 2010-09-15 5:39

>>14
Yes it's valid C. It's not valid C for creating a dynamic array. That's the important difference. The value of x has to be known at compile time, or the compiler will (a) give an error if it's a decent compiler or (b) compile with a random value for x (Typically whatever value is in that RAM block at that very moment, which can lead to hilarious results)

I'm afraid g++ is giving me the (b) option and I'm not happy with it, which is why I made the thread.

Name: Anonymous 2010-09-15 5:39

Incidentally, while I'm at it: I've had people complain when I include conio.h to use the getch() function. Is there some problem with this that I don't know?

Name: Anonymous 2010-09-15 7:04

zawa zawa

Name: Sensei 2010-09-15 8:45

>>18,19
>>1-kun, please read >>8. GCC certainly does not just "use a random value" for x. You will not learn anything by making up answers and assuming they are true. You can use the GCC options -std=c++98 -pedantic to compile according to the C++98 standard and disallow GNU extensions. People don't like conio.h because it's not standard.

Name: Anonymous 2010-09-15 10:18

>>21
SHUTUP

Name: Anonymous 2010-09-15 10:22

IHBT

Name: Anonymous 2010-09-15 11:03

>>22
No, you listen to Sensei, for Sensei has achieved Satori

Name: Anonymous 2010-09-15 15:09

>>18
int array[x] is valid C for creating a dynamic array.

Name: Anonymous 2010-09-15 16:23

>>25
C99 ftl

Name: Anonymous 2010-09-15 18:20

"conio" sounds like "coño" in spanish (means pussy)

Name: Anonymous 2010-09-15 18:26

>>1
I'm learning c++
There's your problem.

Name: Anonymous 2010-09-15 18:54

OP here

>>21
Didn't know about -std=c++98 -pedantic, thank you. I definitely prefer my compiler to read all code as strictly as possible.
RE conio.h not being standard, I thought that was the entire point of libraries, to import non-standard functionality. Have I completely misunderstood something important?

>>25
Are you fucking with me.

>>28
I'm still at the university level, dude. It's "do-as-teacher-says" or "fail-as-expected."

Besides that, I'm not exactly sure what's so bad about C++ - do you just not like OO languages, or is it specifically C++ you don't like?

Name: Anonymous 2010-09-15 19:09

>>29
C++ isn't an OO language.

Name: Anonymous 2010-09-15 19:09

>>29
C++ isn't exactly a prime example of an OO language.

Name: Anonymous 2010-09-15 19:12

So I guess all these classes I'm tripping over are just in my imagination, then?

Name: Alan Kay 2010-09-15 19:21

[m]Just a gentle reminder that I took some pains at the last OOPSLA to try to
remind everyone that Smalltalk is not only NOT its syntax or the class
library, it is not even about classes. I'm sorry that I long ago coined the
term "objects" for this topic because it gets many people to focus on the
lesser idea.

The big idea is "messaging" -- that is what the kernal of Smalltalk/Squeak
is all about (and it's something that was never quite completed in our
Xerox PARC phase). [m]

Name: Anonymous 2010-09-15 19:26

>>29
gcc -std=c++98 -pedantic still accepts code considered invalid in C++98

Name: Anonymous 2010-09-15 19:41

>>26
What's the problem? Too '99 for you?

Name: Sensei 2010-09-15 20:41

>>29
Indeed that is the point of libraries. The important consideration is this: do you really need getch, or is cin good enough? Avoiding unnecessary libraries is always best. If you really do need special terminal functions, note that there are both the conio and ncurses libraries. conio is more of a Windows thing while ncurses is more of a *nix thing.

Name: Anonymous 2010-09-15 21:43

>>32
Your understanding of OO only exists in your imagination.

Name: 2010-09-16 5:14

hurrdurr

Name: Anonymous 2010-09-16 6:01

>>36
Too '99 for you?
Nah. It's just, that it's PIG DISGUSTING.

Name: Anonymous 2010-09-16 7:45

I got 99 problems, but C99 ain't one

Name: Anonymous 2010-09-16 11:35

I got 99 problems, but Java ain't one
Fixed that for you.

Name: Anonymous 2010-09-16 14:25

>>40
I got 99 problems, all of which are C99.

Name: Anonymous 2010-09-16 16:57

>>42
I got C99 problems, of which all are strict aliasing.

Name: Anonymous 2010-09-16 18:34

I got int C = 1; C++ problems, but now C ain't 1.

Name: Anonymous 2010-09-16 18:48

>>44
i feel bad for you son

Name: Anonymous 2010-09-16 19:13

Liking C++ more than C is similar to liking the remake of Psycho more than Hitchcock's version.

Name: Anonymous 2010-09-17 3:20

>>46
The remake was better because they had access to better technologies, and the industry was more mature when it was made.  I forgot which one I was talking about.

Name: Anonymous 2010-11-27 8:58


Don't change these.
Name: Email:
Entire Thread Thread List