LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2014, 09:46 AM   #1
CincinnatiKid
Member
 
Registered: Jul 2010
Posts: 454

Rep: Reputation: 47
C - malloc multi dim array


Can anyone tell me what is wrong with this code? When I run it I get a seg fault:

Code:
#include <stdlib.h>

int main (int argc, char **argv)
{
	char **board;
	
	board = (char **) malloc (sizeof (char *) * 8);
	
	board [0][0] = 'a';
	
	free (board);

	return 0;
}
 
Old 01-26-2014, 10:08 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

You allocate a vector of pointers, but each pointer in this vector should also point to allocated memory. Something like this:
Code:
char **board;
int N = 8, M = 8;
board = (char **) malloc (sizeof (char *) * N);
int i;
for(i=0; i<N; i++)
    board[i] = malloc(sizeof(char)*M); // NOTE: char, not char* here
To deallocate memory:
Code:
for(i=0; i<N; i++) free(board[i]);
free(board);
Alternative approach would be to allocate an array of length N*M (malloc(sizeof(char)*N*M) and access its ij-th element using index i*M+j, i=0..N-1, j=0..M-1.

Last edited by firstfire; 01-26-2014 at 10:15 AM.
 
Old 01-26-2014, 10:23 AM   #3
CincinnatiKid
Member
 
Registered: Jul 2010
Posts: 454

Original Poster
Rep: Reputation: 47
That worked for me. I think I understand. So what I did was allocate memory for a certain number of pointers, but did not allocate the memory of the data that the pointers would point to right?
 
Old 01-26-2014, 11:36 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Right you are!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi dimensional > 3 array in Python disruptive Programming 2 07-28-2010 12:24 PM
malloc array of pointers for platform independence jiml8 Programming 12 08-27-2008 05:53 PM
how to find extent of 1-dim array in fortran 90? johnpaulodonnell Programming 1 03-21-2007 11:42 AM
Multi-dimension array problem. ArthurHuang Programming 2 05-20-2006 09:36 AM
Working with array of multi-lun troyzeng Linux - General 4 11-15-2003 04:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration