Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    dynamic 2d arrays in C

    hi

    i am trying to make as part of a C program, a function that will create a dynamic 2d array (as in a 2d array with no initial set limits on row/column sizes, they will be input when the program is run). This array does not need to be square, but its limits will be 100x100 of chars.

    I made some code, and it works fine, except when the column size (col) is bigger than the rowsize (row). Then it gives me an error : segmentation fault (core dumped). Here it the code -


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>

    int main()
    {
    char **A;
    int row, col, i;
    /*int c=0, d=0;*/

    puts("row=");
    scanf("%d", &row);
    puts("col=");
    scanf("%d", &col);


    A = (char **)malloc(row * sizeof(char *));

    for(i=0; i < col ; i++)
    *(A+i) = (char *)malloc(col * sizeof(char));

    free(A);

    return 0;

    }

    Any help on fixing/explaining why this occurs would be helpful.
    Thanks

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the probelm is that you have to free avery column
    use this
    Code:
    for(i=0;i<col;i++)
    free(*(A+i));

  • #3
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the reply but on my machine it still gives the error..

    and i think it may be i<row
    but that still gives an error

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i<row gives an error when row > col

    what compiler do you use. I us MSCV++

  • #5
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i think its called gcc
    well thats the command i give it to compile programs

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've heard of it, but I don't have it installed

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Netherlands
    Posts
    217
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I compiled the app with GCC,and I got no errors when I ran it and entered a Col bigger than the Row.

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know if gcc can compile C++ code? if so, you can allways try the new and delete operators. they basicly do the same as malloc and free.
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include <ctype.h> 
    #include <math.h> 
    
    int main() 
    { 
    char **A; 
    int row, col, i; 
    /*int c=0, d=0;*/ 
    
    puts("row="); 
    scanf("%d", &row); 
    puts("col="); 
    scanf("%d", &col); 
    
    A=new char *[row];
    
    for(i=0;i<row;i++)
    	A[i]=new char[col];
    
    for(i=0;i<row;i++)
    	delete []A[i];
    
    return 0; 
    
    }

  • #9
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    didnt work

    nah, new and delete come up as errors on gcc (which is what I have to use).

  • #10
    New Coder
    Join Date
    Sep 2002
    Location
    Up North (UK)
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A = (char **)malloc(row * sizeof(char *));

    for(i=0; i < col ; i++)
    *(A+i) = (char *)malloc(col * sizeof(char));
    Is there an error here! In the initial line you allocate "row" number of "slots", then your for loop tries to create "col" number of entries in the "row" number of slots.
    But you only have a "row" number of slots to allocate and hence you get memory access errors when col > row!

  • #11
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks,

    cool it works now
    thanks alot

    this damn thing was driving me crazy, now i can continue my project to create wumpus world ugh,,...


    thx

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Netherlands
    Posts
    217
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by maes
    I don't know if gcc can compile C++ code?
    Sure it can,just make sure that your file has .cpp as extension.Heres an example command for compiling a C++ app:
    gcc -o test test.cpp
    Last edited by Bosko; 09-23-2002 at 08:08 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •