Enjoy an ad free experience by logging in. Not a member yet?
Register .
09-22-2002, 04:27 PM
PM User |
#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
09-22-2002, 04:48 PM
PM User |
#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));
09-22-2002, 04:54 PM
PM User |
#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
09-22-2002, 05:00 PM
PM User |
#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++
09-22-2002, 05:02 PM
PM User |
#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
09-22-2002, 05:22 PM
PM User |
#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
09-22-2002, 06:29 PM
PM User |
#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.
09-22-2002, 06:35 PM
PM User |
#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;
}
09-23-2002, 05:07 AM
PM User |
#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).
09-23-2002, 09:08 AM
PM User |
#10
New Coder
Join Date: Sep 2002
Location: Up North (UK)
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
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!
09-23-2002, 09:19 AM
PM User |
#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
09-23-2002, 07:06 PM
PM User |
#12
Regular Coder
Join Date: Jun 2002
Location: The Netherlands
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
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 07:08 PM ..
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 01:34 AM .
Advertisement
Log in to turn off these ads.