How would I do something like this. I tried the new key word, but I always got a size that was larger than I wanted.
Code:int function(int n)
{
int * i[n];
return (i);
}
Printable View
How would I do something like this. I tried the new key word, but I always got a size that was larger than I wanted.
Code:int function(int n)
{
int * i[n];
return (i);
}
Well firstly you'll need to return a pointer to int (int*) not just an integer. Also, you will need to use the new operator (or the malloc() function if it's plain C) to do it. When you have finished with the memory you must also remember to free the memory with delete [] (the [] is because it's an array)
So this should do it:
When you've finished with the array you allocated, you must delete it like this:Code:int* function(int n) // function returns a pointer
{
int *i = new int[n]; // allocate the memory
return (i); // return pointer to allocated memory
}
Code:delete [] i; // i must be a pointer to the allocated memory
hold on a sec here. I thought if you went out of bounds of an array, you get a GP Fault error.
This should crash shouldn't?Code:void main()
{
int i[20];
i[100] = 33;
cout << i[100] << endl;
}
No. You'd get an Access Violation. For i[20] the highest index you can use is 19.
I Don't realy care what kind of error I'm suposed to get. I wan't to know how is it possible for the code to actualy work. I know my program should of crashed. But it didn't
I'm using VC++ 6
outputCode:#include <iostream.h>
void main()
{
int i[20];
i[100] = 33;
cout << i[100] << endl;
}
Why does it work!?!Code:33
Press any key to continue
It works because you're actually changing the memory area containing your program, so you have access rights on it. It's a very bad idea because it corrupts any extra code.
Try replacing 100 with a much higher number and see what happens :)
Array subscripts are just a shorthand way of doing pointer arithmetic. All that happens is that the number in the subscript is added to the pionter to the array. If you use a number that is out of the bounds of the array, you are just referring to a different area of memory. Changing that memory may cause problems, or it may not, you can't tell really because you don't know what's supposed to be in that place, or even if the memory is being used.
ok. but now how come the size of x and i are different.
Code:#include <iostream.h>
void main()
{
int * i[20];
int * x;
cout << sizeof(i) << endl;
x = new int [20];
cout << (sizeof(x)) << endl;
delete x;
}
If you use sizeof(i), you're taking the size of a 20-element array of type int*, which is 4 * 20 (a pointer is 4 bytes). However, sizeof(x) just returns the size of a pointer.
The compiler sees i differently to x. i is declared to be an array (you have actually declared it to be an array of int pointers), whereas x is just a pointer. sizeof() is not exactly a function, it's more like a macro that the compiler replaces with the size.
if you did this:
then you would get the value 4 outputted.Code:cout << sizeof(i[0]) << endl;
This code should yield two identical values:
Code:#include <iostream.h>
void main()
{
int * i[20];
int * x;
cout << sizeof(i[0]) << endl;
x = new int [20];
cout << (sizeof(x[0])) << endl;
delete x;
}
Why are they so different I can get the size of an element of i and x, there both pointers, there both arrays. if sizeof() is just a macro how do I get the size at runtime?
Array != Pointer
If you use:
then the compiler knows that it has 20 elements, and therefore sizeof(x) returns the total size, which is 20*4 = 80 on a 32-bit system.Code:int x[20];
But:
The compiler doesn't know, since the 20 could be any value, as you can also pass another variable as the size:Code:int *x = new int[20];
So in this case, sizeof(x) resolves to sizeof(int*), which is 4.Code:int y = function_returning_int();
int *x = new int[y];
You don't need to get the size of a dynamically allocated block of memory, because you already know it -- you must in order to allocate it in the first place :)
Why is s larger than [5]? How do I make s[5]?
Code:#include <iostream.h>
void main()
{
char * s;
s = new char[5];
s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
cout << s << "<end of string>" << endl;
delete s;
}
output:
I could add the code...Code:hello²²²²<end of string>
Press any key to continue
but I get access violation. But it clears the garbage on the end before it crashes.Code:s[5] = ' ';
s[6] = ' ';
s[7] = ' ';
s[8] = ' ';
You must use delete[] here, since you've allocated an array. Also, in C/C++ all strings end in a null character: '\0':
Code:#include <iostream.h>
void main() {
char *s;
s = new char[6];
s[0] = 'h';
s[1] = 'e';
s[2] = 'l';
s[3] = 'l';
s[4] = 'o';
s[5] = '\0';
cout << s << "<end of string>" << endl;
delete[] s;
}
ok. Thanks for All the info guys.