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

Pages: 1-

Noob question about C/C++

Name: Anonymous 2011-01-29 19:39

If I do this to make a dynamic array:


//======================================
int* a = NULL;   // Pointer to int, initialize to nothing.
int n;           // Size needed for array
cin >> n;        // Read in the size
a = new int[n];  // Allocate n ints and save ptr in a.
for (int i=0; i<n; i++) {
    a[i] = 0;    // Initialize all elements to zero.
}
. . .  // Use a as a normal array
delete [] a;  // When done, free memory pointed to by a.
a = NULL;     // Clear a to prevent using invalid memory reference.
//======================================

And I later want to increase the size of the array (Maybe i received more data) or decrease the size of the array (Memory suddenly more critical than data so I want to delete half the data points) is there any way to do this *other* than what I've been doing which is: Make a new array in the correct size, fill in, delete old array.

Name: Anonymous 2011-01-29 19:46

There's no language called C/C++.

Name: Anonymous 2011-01-29 19:53

realloc

Name: Anonymous 2011-01-29 19:56

use vector or some other shit.

Name: Anonymous 2011-01-29 20:07

>>2
"C/C++" has been a generally accepted name for C++ for quite a while. It's dated now, but you'll still see it in job adverts (if you take one of these jobs you won't be writing ISO C.)

Name: Anonymous 2011-01-29 20:23

>>5
But it's completely undefined!!!
It could even evaluate to Pascal, or FORTRAN!

Name: Anonymous 2011-01-29 20:28

>>6
It returns 1 and increments C. For all C > 1, it gives ``nothing'' and makes C look better.

Name: Anonymous 2011-01-29 20:32

Also, implying C is unsigned, it can't be 0, otherwise the expression would be invalid (0/0).
C must be at least 1, >>7 is always valid. ``C/C++'' sucks and makes C better. QED

Name: Anonymous 2011-01-29 22:21

I concur.
realloc for see
std::vector for seeples

Name: Anonymous 2011-01-29 23:17

>>9
concurrency
C/Sepples

IHBT

Name: Anonymous 2011-01-29 23:41

>>1
If
Stopped reading right there.

Name: Anonymous 2011-01-29 23:45

>>11
Sto
Stopped reading right there.

Name: Anonymous 2011-01-30 7:08

>>10
C++0x

Name: Anonymous 2011-02-04 19:50

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