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

Pages: 1-

C++ query

Name: Anonymous 2010-05-06 13:08

  
    int listsize, count;
    cout<<"Define size of list: ";
    cin>>listsize;
    cout<<endl;
    int list[listsize];


Are there any problems with this? It works fine but I just want to make sure I'm not committing a faux pas.

Name: Anonymous 2010-05-06 13:14

ummm I'm assuming list is it's own class.... so yes...

Name: Anonymous 2010-05-06 13:14

Use int *list = malloc( sizeof(int) * listsize );
Static arrays cannot be initialised dynamically like that.

Name: Anonymous 2010-05-06 13:17

int *list, listsize;

printf("Define size of list: ");
scanf("%d\n", &listsize);

list = malloc(listsize * sizeof(int));


Happy to help.

Name: 4 2010-05-06 13:44

Also, don't forget to free() it when you're done with it. Nobody likes memory leaks1.

____________
1 http://en.wikipedia.org/wiki/Memory_leak

Name: Anonymous 2010-05-06 13:48

>>5
I know, I prefer mammary leaks.

Name: Anonymous 2010-05-06 13:59

>>4
>>3
>>5

I see, if I ever wanted the change the array size after it has been initialized what I did would some how break it, even though it seems to work correctly on a small scale?

Name: Anonymous 2010-05-06 14:01

>>7
No, the point of those posts is just that you need to use C instead of Sepples. Take their advice.

Name: Anonymous 2010-05-06 14:05

>>8

C++ has malloc too. I don't mind which I use to be honest but I've gotten used to the C++ syntax because it's what I use in class.

Name: Anonymous 2010-05-06 14:09

>>1
Yes, there are problems with that.

Name: Anonymous 2010-05-06 14:15

>>9
I'd like you to read up on the differences between statically and dynamically allocated memory.

Name: Anonymous 2010-05-06 16:10

>>11

What am I actually doing when I run this and give a higher number the second time then?


#include <iostream>

using namespace std;

int main()
{
    int listsize, count;
    cout<<"Define size of list: ";
    cin>>listsize;
    cout<<endl;
    int list[listsize];

    cout<<"Enter numbers: ";

    for (count=0; count<listsize; count++) {
        cin>>list[count];
    }

    for (count=0; count<listsize; count++) {
        cout<<list[count]<<", ";
    }

    cin>>listsize;

        cout<<"Enter numbers: ";

    for (count=0; count<listsize; count++) {
        cin>>list[count];
    }

    for (count=0; count<listsize; count++) {
        cout<<list[count]<<", ";
    }


    return 0;
}

Name: Anonymous 2010-05-06 16:18

NOT OP, but question about sepples

while using cout and creating a long chain of << for let's say a menu

why the fuck does it eventually ignore the closing bracket connection for any open brackets above it?

Name: Anonymous 2010-05-06 16:24

>>12
You're overrunning the bounds of whatever size memory chunk it was allocated previously. This will end up with undefined behaviour, most likely writing over the next memory thing in your program or crashing with a segmentation fault.
Use malloc and free wherever you need to use arrays, it's easier and less worry once you 'get it' and get used to it.

If you want practice with arrays, try writing a library for them (include functions like allocate(), destroy(), resize(), value_at(n), etc).

Name: Anonymous 2010-05-06 16:30


cin>>listsize;


Never ever EVER do that.1

Do this:

#include <sstream>
#include <string>

and

string line;
getline( cin, line );
stringstream( line ) >> list[count];


Now that's proper input!2

_________________________________________________________________
1 It's bad, it will break cin, it will break your skull, and those of your family and friends.
2 And it will hax your anus, ANUS THE HAXUS

Name: Anonymous 2010-05-06 16:32

>>15
This is true. OP should learn to sanitise inputs.

Name: Anonymous 2010-05-06 16:39

>>15
yes because declaring a larger data type and having to include another file really does miracles for compile time for a simple program

Name: Anonymous 2010-05-06 16:39

Don't use C++ streams.  They are bad for you.

Name: Anonymous 2010-05-06 16:45

>>15

Now why would that be? It seems like extra hassle for what?

Name: Anonymous 2010-05-06 16:53

<<20;

Name: Anonymous 2010-05-06 17:04

>>21
nope I remembered that
 I just had something like
void menu() {
cout << endl << "1." << endl << "2." << endl << "3." << endl << "4." << endl << "5.";

};


error misplaced }

????

WHAT THE FUCK IT STILL GETS ME PISSED

Name: Anonymous 2010-05-06 17:07

>>22
That's probably because of the semicolon at the end of your function definition.

Name: Anonymous 2010-05-06 17:15

>>23
for the sake of it

it main come below it so semi colon required

Name: Anonymous 2010-05-06 17:44

>>24
What are you smoking?

Functions aren't expressions or declarations so that semicolon doesn't belong there.

Name: Anonymous 2010-05-06 20:38

>>25
I'm smoking sepples

Name: Anonymous 2010-05-06 23:03

>>3
sepples really can't do that? wow, it really is years behind c.

>>1,4,12
you probably want do actually output something before waiting for input.
lrn2flush

Name: Anonymous 2010-05-07 0:20

>>25
you can put a semi colon after any block {}; doesn't change anything

Name: Anonymous 2010-05-07 7:14

>>27

What are you talking about?

Name: Anonymous 2010-05-07 7:25

>>19

Have you read the references I left in my post?1
Let me point you to the right direction, a simple C++ FAQ:
http://faqs.cs.uu.nl/na-dir/C++-faq/part07.html
Go and read section 15.2.
I quote:


 #include <iostream>

 int main()
 {
   std::cout << "Enter numbers separated by whitespace (use -1 to quit): ";
   int i = 0;
   while (i != -1) {
     std::cin >> i;        // BAD FORM -- See comments below
     std::cout << "You entered " << i << '\n';
   }
 }


The problem with this code is that it lacks any checking to see if someone
entered an invalid input character.
In particular, if someone enters something that doesn't look like an integer (such as an 'x'), the stream std::cin goes into a "failed state,"
and all subsequent input attempts return immediately
without doing anything.
In other words, the program enters an infinite loop;
if 42 was the last number that was successfully read, the program will print
the message You entered 42 over and over.


And that's why it's bad to do:2

cin >> MyInt;

_________________________________________________________________
1This right here?
2It will also rape puppies, and your anus but honestly who cares about your anus.

Name: Anonymous 2010-05-07 12:00

>>30
while (i!= -1)  {
top:
if (cin>>i)
std::cout << "You entered " << i << '\n';

else
goto top;
}

Name: Anonymous 2010-05-07 12:00

>>31
forgot my code tag.....

Name: Anonymous 2010-05-07 12:18

The thread question wasn't about input sanitisation. It was about dynamically allocating statically allocated memory.

Solution: You can't. Use malloc and free.

Name: Anonymous 2010-05-07 15:28

Spoilers: in Sepples, dynamic array sizes like that are just syntactic sugar for malloc, and perfectly valid. All these people complaining about static versus dynamic memory are idiots.
All of the ones telling you to use C instead are right, though.

Name: Anonymous 2010-05-07 15:34

>>34
telling you to use C instead
And by implication, dynamic memory (explicit malloc) also.

Name: Anonymous 2010-05-07 15:53

>>29
not sure which part of >>27 you're replying to, so here's a nice simple explanation for both parts.
c can do this just fine:
#include <stdio.h>
#include <stdlib.h>

long f(long n)
{ long a[n];
  a[0] = 0;
  a[1] = 1;
  for(long i = 2; i < n; ++i)
    a[i] = a[i - 1] + a [i - 2];
  return a[n - 1]; }

int main(int argc, char *argv[argc])
{ for(int i = 1; i < argc; ++i)
  { long n = strtol(argv[i], NULL, 0);
    printf("%ld\n", f(n)); }
  return 0; }


stdout is usually line buffered. the program might not produce any output until you either send a newline character to it, use fflush(), or use sepples' std::endl or std::flush.

Name: Anonymous 2011-02-04 13:17


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