Variable Allocation Problem

This is a discussion on Variable Allocation Problem within the C Programming forums, part of the General Programming Boards category; Hey, I am having trouble with some code. Reduced to just this there still is an error: Code: int main ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Variable Allocation Problem

    Hey, I am having trouble with some code. Reduced to just this there still is an error:

    Code:
    int main () {
    	
    	//************  MAIN PARAMETERS ******************//
    	
    #define dictionary_length 1000000
    #define input_word_max_length 20
    	
    #define num_of_neurons 1000000
    #define num_of_connections_per_neuron 4
    	
    	//************************************************//
    	
    	char dictionary_words[dictionary_length][input_word_max_length];
    	int inputs[dictionary_length];
    	int outputs[dictionary_length];
    	float *brain_energy[num_of_neurons][num_of_connections_per_neuron];
    	float energy[num_of_neurons];
    	unsigned int *brain_conn_strength[num_of_neurons][num_of_connections_per_neuron];
    	unsigned int conn_strength[num_of_neurons*num_of_connections_per_neuron/2];
    	
    	
    	printf("A");
    	
    	return 1;
    }

    It compiles fine but when run (in XCode) it throws an error pointing to the printf statement saying "Cannot access memory at address 0xbbf25e74" (the address changes from build to build).

    Any idea what is going on here. I have heard allocating using malloc manually is the way to go, but would that solve this problem?

    Any ideas would be appreciated

    Thanks!

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    On a typical desktop system, you're trying to create around 70 megs or more of local storage. Odds are your stack is not that large. If you put the variables in static storage (just tack a static to the beginning of each declaration), does your problem go away? If so, it was a stack issue. Either rethink how you're approaching this problem, or don't use local storage.

    Make sure you understand the implications of using static storage (or dynamic, if you choose to use malloc(), which is a possibility, too).

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    Yes, the problem goes away with "static" prepended to each declaration. As I understand this will cause the arrays in this case to be private to the function - is there any other functional implication? For example, will rapidly modifying values in the arrays be slower due to the static declaration?

    I have the Kernighan and Ritchie C book and I can't find any mention to the differences in where variables are stored (heap, etc). Is there an online guide you could recommend?

    Thanks!
    Last edited by bavetta; 11-13-2009 at 08:58 PM.

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    The arrays are private to the function (in the sense that their names cannot be used to access them outside of the function) whether they are tagged static or not. The static keyword is overloaded in C (having 2 meanings in C89 and 3 in C99). When you use static inside of a function, you give the object it refers to static storage duration, which means its lifetime is that of the entire program; it's essentially a global variable but with local scope. These variables retain their values in between function calls, and can affect whether a function is reŽntrant. The other main meaning of static is as you understand it: at file scope (that is, global variables), static makes the object local to that source file only.

    Off the top of my head I don't know any sites that discuss the various types of allocation. In fact, there needn't be a stack, heap, etc; these are just the way things are typically done. But storage duration is relatively easy to understand. There are three types.

    The first is automatic. These are objects local to a block (so they must be inside a function, and may potentially be allocated in a block inside the function). They exist only as long as that block is “active”, so to speak. Once the closing brace of a block is reached, all objects local to that block are destroyed. These are typically stored on the stack which, as you've discovered, is usually relatively small.

    Next is static. These, as I've described above, live for the entire life of the program. They only are destroyed as the program is exiting. Global variables have static storage duration, as do block local variables that are tagged “static”.

    Finally is allocated. This is memory that you explicitly ask for with malloc() or similar. Its lifetime is however long you want it to be. It lives until it is freed with free().

    If you need a lot of memory, you'll want static or allocated memory, depending on the usage. Automatic variables are great in that you don't have to worry about deallocating them, and they are useful for making reŽntrant functions. But of course the stack isn't necessarily large.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation problem, don't know what's wrong
    By Metalmurphy in forum C Programming
    Replies: 10
    Last Post: 04-22-2008, 05:32 AM
  2. While loop with variable problem
    By Todd88 in forum C++ Programming
    Replies: 33
    Last Post: 04-06-2008, 08:36 PM
  3. memory allocation problem
    By ccoder01 in forum C Programming
    Replies: 12
    Last Post: 04-24-2004, 09:22 PM
  4. memory allocation problem with 2 dimensional array
    By nano_nasa in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 12:34 PM
  5. Peculiar Problem with char variable in C Language
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21