Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 3.33 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2002, 04:27 PM   PM User | #1
dole_4
New Coder

 
Join Date: Sep 2002
Location: Australia
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
dole_4 is an unknown quantity at this point
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
dole_4 is offline   Reply With Quote
Old 09-22-2002, 04:48 PM   PM User | #2
maes
Regular Coder

 
Join Date: Jul 2002
Location: Belgium
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
maes is an unknown quantity at this point
the probelm is that you have to free avery column
use this
Code:
for(i=0;i<col;i++)
free(*(A+i));
maes is offline   Reply With Quote
Old 09-22-2002, 04:54 PM   PM User | #3
dole_4
New Coder

 
Join Date: Sep 2002
Location: Australia
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
dole_4 is an unknown quantity at this point
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
dole_4 is offline   Reply With Quote
Old 09-22-2002, 05:00 PM   PM User | #4
maes
Regular Coder

 
Join Date: Jul 2002
Location: Belgium
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
maes is an unknown quantity at this point
i<row gives an error when row > col

what compiler do you use. I us MSCV++
maes is offline   Reply With Quote
Old 09-22-2002, 05:02 PM   PM User | #5
dole_4
New Coder

 
Join Date: Sep 2002
Location: Australia
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
dole_4 is an unknown quantity at this point
i think its called gcc
well thats the command i give it to compile programs
dole_4 is offline   Reply With Quote
Old 09-22-2002, 05:22 PM   PM User | #6
maes
Regular Coder

 
Join Date: Jul 2002
Location: Belgium
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
maes is an unknown quantity at this point
I've heard of it, but I don't have it installed
maes is offline   Reply With Quote
Old 09-22-2002, 06:29 PM   PM User | #7
Bosko
Regular Coder

 
Join Date: Jun 2002
Location: The Netherlands
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Bosko is an unknown quantity at this point
I compiled the app with GCC,and I got no errors when I ran it and entered a Col bigger than the Row.
Bosko is offline   Reply With Quote
Old 09-22-2002, 06:35 PM   PM User | #8
maes
Regular Coder

 
Join Date: Jul 2002
Location: Belgium
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
maes is an unknown quantity at this point
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; 

}
maes is offline   Reply With Quote
Old 09-23-2002, 05:07 AM   PM User | #9
dole_4
New Coder

 
Join Date: Sep 2002
Location: Australia
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
dole_4 is an unknown quantity at this point
didnt work

nah, new and delete come up as errors on gcc (which is what I have to use).
dole_4 is offline   Reply With Quote
Old 09-23-2002, 09:08 AM   PM User | #10
fivesidecube
New Coder

 
Join Date: Sep 2002
Location: Up North (UK)
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
fivesidecube is an unknown quantity at this point
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!
fivesidecube is offline   Reply With Quote
Old 09-23-2002, 09:19 AM   PM User | #11
dole_4
New Coder

 
Join Date: Sep 2002
Location: Australia
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
dole_4 is an unknown quantity at this point
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
dole_4 is offline   Reply With Quote
Old 09-23-2002, 07:06 PM   PM User | #12
Bosko
Regular Coder

 
Join Date: Jun 2002
Location: The Netherlands
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Bosko is an unknown quantity at this point
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..
Bosko is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:34 AM.


Advertisement
Log in to turn off these ads.