Thread: question about global variabels

  1. #1
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259

    question about global variabels

    I`m planing to make a little game where I whant you to be abel to choose how big a matrix should be I figurd that I probably coul use maloc to creat it as a globel variabel but can I use realloc to make it bigger/smaller? or is the realloc just for the funktion I called it in?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    malloc:ed and realloc:ed memory can be accessed through your entire program. You can malloc in a function then return a pointer to it and still have access to it outside that function.
    Just remember to free it before exiting the program.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    oh I see I got some vage memory of how you return a pointer to an array... is it?

    Code:
    malloc the matrix (forgot how to do it at the moment but I got it wirten down somewhere)
    int* i;
    *i=matrix[0] [0]
    return *i;

    but if it is like this does it mean that I have to send and return it to each funktion I whant to use it in and how do I make it to a matrix in that funktion again???
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    int *CreateArray(size_t size)
    {
      int *i = malloc(sizeof(int) * size);
      
      if (i == NULL)
        perror("malloc");
      
      return i;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    oh and can I make it like this instad?
    Code:
    #include "alot of things.h"
    
    int x=12;
    int y=12;
    int matrix [x] [y]
    goto someplace
    place
    int matrixx [x] [y]
    goto back
    someplace
    
    int main(){
    and then the rest of the program...
    }
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  6. #6
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    Originally posted by Hammer
    Code:
    int *CreateArray(size_t size)
    {
      int *i = malloc(sizeof(int) * size);
      
      if (i == NULL)
        perror("malloc");
      
      return i;
    }

    but how do I use it later on? and where do I tell it how big it should be?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Some reading for you.

    >>where do I tell it how big it should be?
    >>but how do I use it later on?
    An example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 10
    
    int *CreateArray(size_t size)
    {  
      int *i = malloc(sizeof(int) * size);  
      
      if (i == NULL)    
        perror("malloc");    
      return i;
    }
    
    int main(void)
    {
      int *myarraypointer;
      int i;
      
      myarraypointer = CreateArray(SIZE);
      
      if (myarraypointer != NULL)
      {
        for (i = 0; i < SIZE; i++)
        {
          myarraypointer[i] = i;
          printf ("myarraypointer[%d] is %d\n", i, myarraypointer[i]);
        }
      }
      
      free(myarraypointer);
      return 0;
    }
    
    /* output
    
    myarraypointer[0] is 0
    myarraypointer[1] is 1
    myarraypointer[2] is 2
    myarraypointer[3] is 3
    myarraypointer[4] is 4
    myarraypointer[5] is 5
    myarraypointer[6] is 6
    myarraypointer[7] is 7
    myarraypointer[8] is 8
    myarraypointer[9] is 9
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    okey hammer I think I get it now I`ll print the link you gave him in school tomorrow and read it carefully (I always have a hard time understanding stuff like that when I read it from a sreen.....hmm sounds like I`m really stupid now....awell)
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Shogun
    okey hammer I think I get it now I`ll print the link you gave him in school tomorrow and read it carefully (I always have a hard time understanding stuff like that when I read it from a sreen.....hmm sounds like I`m really stupid now....awell)
    You're not stupid, I have the same trouble. I end up printing loads, and throwing it away the next day
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    hmmm but what if I wanna make an matrix instad?
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a global linked list?
    By TwistedJester in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 03:23 AM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. Replies: 5
    Last Post: 07-09-2007, 05:24 AM
  4. Global Structure question.
    By tdep in forum C Programming
    Replies: 6
    Last Post: 07-04-2007, 05:59 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM