Results 1 to 15 of 15

Thread: allocate memory?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Question

    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);
    }

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Exclamation

    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?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    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!?!

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    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;
    }

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    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?

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Array != Pointer

    If you use:
    Code:
    int x[20];
    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

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    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.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Thumbs up

    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
  •  



Click Here to Expand Forum to Full Width