Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 6, 2011, 06:01 PM   #1
bacteriatype4
macrumors newbie
 
Join Date: Jun 2011
multidimensional arrays in C (xcode 3.2.3)

I've been working on a project using my, now, old PC using visual C++.
everything works fine there.

Now i'm attempting to run it on my new imac. however an strange error occurs.

after allocating a simple 2d array

Code:
a = (int**) malloc(sizeof(int)*10);
	for(i=0;i<10;i++){a[i] = (int*) malloc(sizeof(int)*10);}
when the following loop is processed

Code:
for(i=0;i<10;i++){for(j=0;j<10;j++){a[i][j]=0;}}
EXC_BAD_ACCESS occurs. I made some research and i found something like there are holes in the memory or i should use a convention to a 1-dimensional array to deal with it. Another suggestions were around C++ object programing.

it should work on simple C, but i don't know what is happening and i have plans to stay with macos =]
bacteriatype4 is offline   0 Reply With Quote
Old Jun 6, 2011, 06:06 PM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Your code is not 64-bit clean because of a coding error. It worked on 32-bit Window because a pointer is the same size an integer under that system. It's not true under 64-bit Mac OS X.

Your missing an asterisk, highlighted below in red.
Code:
a = (int**) malloc(sizeof(int*)*10);
	for(i=0;i<10;i++){a[i] = (int*) malloc(sizeof(int)*10);}
jiminaus is offline   0 Reply With Quote
Old Jun 6, 2011, 06:16 PM   #3
bacteriatype4
Thread Starter
macrumors newbie
 
Join Date: Jun 2011
amazing!

thank you very much!
bacteriatype4 is offline   0 Reply With Quote
Old Jun 7, 2011, 02:58 AM   #4
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
General observation: When you write code, and it crashes, your very first assumption should be that there is a bug in your code, and you should stay with this assumption until it is proven wrong - and believe me, that is very, very unlikely to happen. Obviously the code would have crashed on _any_ compiler with 32 bit int and 64 bit pointers.
gnasher729 is online now   0 Reply With Quote
Old Jun 7, 2011, 08:30 AM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by gnasher729 View Post
Obviously the code would have crashed on _any_ compiler with 32 bit int and 64 bit pointers.
Just to be a dick, anywhere the sizeof(void *) > sizeof(int).

-Lee
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Please Help with arrays Bigred101 Mac Programming 7 Mar 13, 2014 07:39 PM
XCODE Parse Json with 4 arrays inside joak Mac Programming 6 Mar 10, 2014 03:24 PM
hard drive arrays rdking647 Buying Tips and Advice 2 Dec 6, 2013 08:45 PM
arrays of structs farmerdoug Mac Programming 3 Feb 7, 2013 01:47 PM
Comparing Two Arrays Ides iPhone/iPad Programming 5 Aug 10, 2012 03:56 PM

Forum Jump

All times are GMT -5. The time now is 05:28 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC