LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-22-2006, 05:36 PM   #1
hold_breal
Member
 
Registered: Dec 2005
Distribution: suse/lfs/ubuntu
Posts: 46

Rep: Reputation: 15
c compiler problem in 64-bit version.


can someone try this out on a 64bit machine and tell me if it also goes wrong if you increase the size of the matrix?


#include <stdio.h>
#include <stdlib.h>

int main()
{
int i, j, zeile, spalte;

int **matrix;

printf("wie viele zeilen?: ");
scanf("%d",&zeile);
printf("wie viele spalten?: ");
scanf("%d",&spalte);

matrix = (int **)malloc(zeile*sizeof(int));
if(NULL == matrix)
{
printf("pwnd!\n");
exit(0);
}

for(i=0; i < zeile; i++)
{
matrix[i] = (int *)malloc(spalte*sizeof(int));
if(NULL == matrix[i])
{
printf("pwnd!\n");
exit(0);
}
}

for(i=0; i < zeile; i++)
for(j=0; j < spalte; j++)
matrix[i][j] = i+j;

for(i=0; i < zeile; i++)
{
for(j = 0; j < spalte; j++)
printf("%d ", matrix[i][j]);
printf("\n");
}

for(i =0; i < zeile; i++)
free(matrix[i]);

free(matrix);
return 0;
}


i also compiled it on my 32 bit machine, and it didn't have any troubles, so unless i've done something stupid, there seems to be a problem with malloc on my 64 bit machine.

both machines are running gcc 4.0.2

much obliged...

hold
 
Old 04-22-2006, 06:15 PM   #2
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
Works here on Debian Sid AMD64 with gcc 4.0.3:
Code:
~ % uname -a                                                             7:13PM
Linux firiesen 2.6.16-1-amd64-k8 #2 Thu Apr 20 20:10:46 CEST 2006 x86_64 GNU/Linux

~ % gcc -v                                                               7:13PM
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre --enable-mpfr --disable-werror --enable-checking=release x86_64-linux-gnu
Thread model: posix
gcc version 4.0.3 (Debian 4.0.3-1)

~ % gcc -Wall -o t t.c                                                   7:11PM

~ % ./t                                                                  7:12PM
wie viele zeilen?: 3
wie viele spalten?: 4
0 1 2 3 
1 2 3 4 
2 3 4 5 

~ % ./t                                                                  7:12PM
wie viele zeilen?: 0
wie viele spalten?: 0

~ % ./t                                                                  7:12PM
wie viele zeilen?: r
wie viele spalten?:                                                            

~ % ./t                                                                  7:13PM
wie viele zeilen?: -1
wie viele spalten?: -5
pwnd!
 
Old 04-23-2006, 06:07 AM   #3
hold_breal
Member
 
Registered: Dec 2005
Distribution: suse/lfs/ubuntu
Posts: 46

Original Poster
Rep: Reputation: 15
someone told me the problem which occurs when the number of zeilen(rows?) is increased to above 4.

**matrix is itself an array where every element points to an array. every element has to be large enough to store an address, which on a 64bit system is of course 8-byte long. so the line:

matrix=(int **)malloc(zeile*sizeof(int))

is too small to hold all the addresses. (sizeof(int) is only 4-bytes large). this can be cured by
a/ finding something 8bytes large to go in sizeof(int)
or
b/ writing it explicitly: matrix=(int **)malloc(zeile*8);
or
c/ being crafty and making sure the computer checks how long the pointer has to be, for example:
i/ matrix=(int **)malloc(zeile*sizeof(int*));
or
ii/

int main()
{
...
int *ptr;
...

matrix=(int **)malloc(zeile*sizeof(ptr));
...
}

which would make pretty general code one could use everywhere, i suppose.

thanks for your help

hold
 
Old 04-23-2006, 10:17 AM   #4
ataraxia
Member
 
Registered: Apr 2006
Location: Pittsburgh
Distribution: Debian Sid AMD64
Posts: 296

Rep: Reputation: 30
Ah, I didn't try it for big (or even medium-sized) numbers.

I suggest you use
Code:
matrix=(int **)malloc(zeile*sizeof(void*));
It's general enough that it won't confuse somebody like the int* version might - void* looks more like "size of pointer" than int* to me.

This code reminds me of tensor analysis...
 
Old 04-23-2006, 04:45 PM   #5
hold_breal
Member
 
Registered: Dec 2005
Distribution: suse/lfs/ubuntu
Posts: 46

Original Poster
Rep: Reputation: 15
good idea, ataraxia, though for the utter beginner (which i am) i think my idea of defining another trivial pointer and then taking sizeof(ptr) may be even clearer

don't worry, it's not tensor analysis (yet), though i am gradually working out numerical methods to do lots of interesting things (for example, laws of motion through using small steps in time to find out where something is, or charge distributions by placing charges singularly in a large 3d-matrix).

in case you or someone else has been playing about with it, i'm trying to think up methods to solve for the path of least effect (hamiltonian stuff) numerically, the aim being of course to build it up gradually to the path of most proper time, so i can calculate the course of a space probe on my system, but i'm struggling to think of good algorithms to maximize (minimize) an integral. i think the best method would be to have the program make many passes, where each pass already regards the path as being made up of n-elements, then letting it gradually adjust to fit. maybe i should find a specialist forum for this sort of thing or even *gasp* *shock* buy_a_book!

thanks again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
stupid q: how to determine version 64-bit or 32-bit? noknow Linux - Software 29 09-28-2012 04:53 AM
How does the 64 bit version handle interacting with 32 bit programs? purelithium Mandriva 1 11-13-2005 05:16 PM
Which version of 32 bit redhat will install on IBM xSeries 366 (64 bit)? Hello123 Linux - Hardware 2 09-14-2005 05:50 AM
64-bit Assembly Compiler for Linux? ace_bandit_1 Linux - Software 1 05-13-2005 03:52 PM
problem with compiler version?? miha Linux - General 4 01-03-2004 12:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:25 AM.

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