Thread: Unsized array problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Unsized array problem

    Hi, I just wanted to test my unsized array and populate it with user input. The test I wrote was simple and basic but it does not operate as I expected, it refers to a bus error.

    It may have something to do with the unsized array, as when I come to test it and I hard code a specified size the error does not occur.

    As it is, if I input a value for the gates, say '4'. Then apply a value for the inputs for each of the gate, the system always seems to cut off at 2. I really can't see why a simple bit of code is doing this.

    I am using LCC Win-32 as my C complier.

    Code:
    #include <stdio.h>
    
    int main() {
    int gates;
    int i;
    int inputs[] = {0};
    
    	printf("how many gates: ");
    	scanf("%d", &gates);
    
    
    	for(i = 0; i < gates; i++) {
    		printf("how many inputs for gate %d: ", i);
    		scanf("%d",&inputs[i]);
    	}
    
    }
    Thankyou.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what makes you beleive this code should work and not crash?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The only array I see in your program is inputs, but inputs is an array of size 1, not an "unsized array".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    Hi, I modified your program slightly, inputs[i] is no longer assigned a value within the loop for obvious reasons (you'll see in a second).

    Code:
    #include <stdio.h>
    
    int main() {
        int gates;
        int i;
        int inputs[] = {0};
        
        printf("Size of integer = %d\n", sizeof(int));
        printf("how many gates: ");
        scanf("%d", &gates);
    
        for(i = 0; i < gates; i++) {
            printf("how many inputs for gate %d:\n", i);
            printf("Addr: inputs[i]\t=\t %lu\n", (unsigned long)&inputs[i]);
            printf("Addr: gates\t=\t %lu\n", (unsigned long)&gates);
            printf("Addr: i\t\t=\t %lu\n", (unsigned long)&i);
            printf("Addr: inputs\t=\t %lu\n\n", (unsigned long)&inputs);
            
            //scanf("%d",&inputs[i]);
            
        }
    
    }
    Output:
    saec@saeculum:~$ ./test
    how many gates: 4
    how many inputs for gate 0:
    Addr: inputs[i] = 3214882696
    Addr: gates = 3214882704
    Addr: i = 3214882700
    Addr: inputs = 3214882696

    how many inputs for gate 1:
    Addr: inputs[i] = 3214882700
    Addr: gates = 3214882704
    Addr: i = 3214882700
    Addr: inputs = 3214882696

    how many inputs for gate 2:
    Addr: inputs[i] = 3214882704
    Addr: gates = 3214882704
    Addr: i = 3214882700
    Addr: inputs = 3214882696

    how many inputs for gate 3:
    Addr: inputs[i] = 3214882708
    Addr: gates = 3214882704
    Addr: i = 3214882700
    Addr: inputs = 3214882696
    If you inspect the addresses of the variables, and the address that &input[i] points to you'll notice that it often coincides with the address of another variable.

    When your program runs, the variables in main are placed onto a stack on after the other so the stack may look a little like this:

    Code:
    +--------+
    | inputs | Lowest Addr 
    +--------+
    |   i    |
    +--------+
    | gates  | Highest Addr 
    +--------+
    Remembering that the stack is upside down in the fact that the variable with the highest address on the stack is gates

    This is relevant to your problem in that, at the moment your array has 4 bytes of space defined, it's essentially a pointer to the memory at which those 4 bytes begin.
    When you do an operation such as:
    Code:
    &inputs[i]
    What's actually taking place is pointer arithmetic. So essentially, the address pointed to would be: &inputs + (sizeof(int) * i ) - the size of an int on a system is generally 4 bytes.. sizeof(int) returns the number of bytes that an int is defined as being.

    Your error is assuming that the inputs array is large enough to hold data being read into it by scanf.

    Inspect the output and you'll notice that the address &inputs[i] is incremented by 4 each time i increases. It would then overwrite the 4 bytes of memory starting it &inputs[i]( on the stack below it in the higher memory addresses), and would eventually cause a segmentation fault in the best scenario.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by qwertysingh View Post
    It may have something to do with the unsized array, as when I come to test it and I hard code a specified size the error does not occur.
    The problem is that there is no such thing as an "unsized" array (as you understand it).
    An array with an unspecified size has it's size implicitly calculated by the compiler according to how many elements it is initialised with. You are initialising it with one element, therefore it is an array of length one, and this array length cannot change while your program is running.
    It is the same as if you had written:
    Code:
        int inputs[1] = {0};
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  2. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM