|
-
Mar 21st, 2001, 11:00 PM
#1
Thread Starter
Addicted Member
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);
}
-
Mar 22nd, 2001, 01:41 PM
#2
Frenzied Member
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:
Code:
int* function(int n) // function returns a pointer
{
int *i = new int[n]; // allocate the memory
return (i); // return pointer to allocated memory
}
When you've finished with the array you allocated, you must delete it like this:
Code:
delete [] i; // i must be a pointer to the allocated memory
Harry.
"From one thing, know ten thousand things."
-
Mar 22nd, 2001, 07:00 PM
#3
Thread Starter
Addicted Member
hold on a sec here. I thought if you went out of bounds of an array, you get a GP Fault error.
Code:
void main()
{
int i[20];
i[100] = 33;
cout << i[100] << endl;
}
This should crash shouldn't?
-
Mar 23rd, 2001, 02:00 PM
#4
Monday Morning Lunatic
No. You'd get an Access Violation. For i[20] the highest index you can use is 19.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 23rd, 2001, 08:33 PM
#5
Thread Starter
Addicted Member
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
Code:
#include <iostream.h>
void main()
{
int i[20];
i[100] = 33;
cout << i[100] << endl;
}
output
Code:
33
Press any key to continue
Why does it work!?!
-
Mar 24th, 2001, 07:46 AM
#6
Monday Morning Lunatic
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
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 24th, 2001, 10:46 AM
#7
Frenzied Member
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.
Harry.
"From one thing, know ten thousand things."
-
Mar 24th, 2001, 12:50 PM
#8
Thread Starter
Addicted Member
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;
}
-
Mar 24th, 2001, 12:56 PM
#9
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 24th, 2001, 01:02 PM
#10
Frenzied Member
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:
Code:
cout << sizeof(i[0]) << endl;
then you would get the value 4 outputted.
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;
}
Harry.
"From one thing, know ten thousand things."
-
Mar 24th, 2001, 01:42 PM
#11
Thread Starter
Addicted Member
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?
-
Mar 24th, 2001, 01:49 PM
#12
Monday Morning Lunatic
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.
But:
Code:
int *x = new int[20];
The compiler doesn't know, since the 20 could be any value, as you can also pass another variable as the size:
Code:
int y = function_returning_int();
int *x = new int[y];
So in this case, sizeof(x) resolves to sizeof(int*), which is 4.
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
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 24th, 2001, 02:33 PM
#13
Thread Starter
Addicted Member
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:
Code:
hello²²²²<end of string>
Press any key to continue
I could add the code...
Code:
s[5] = ' ';
s[6] = ' ';
s[7] = ' ';
s[8] = ' ';
but I get access violation. But it clears the garbage on the end before it crashes.
-
Mar 24th, 2001, 02:44 PM
#14
Monday Morning Lunatic
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;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 24th, 2001, 04:53 PM
#15
Thread Starter
Addicted Member
ok. Thanks for All the info guys.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|