LinuxQuestions.org
Help answer threads with 0 replies.
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 03-26-2005, 07:40 PM   #1
huble
LQ Newbie
 
Registered: Feb 2005
Location: Rio de Janeiro, Brazil
Distribution: Slackware
Posts: 7

Rep: Reputation: 0
Question problem using malloc in C


I've always had problems using the malloc function, but this time I'd really appreciate some help.
I'm doing something like this:
Code:
void main (void) {
	int **a; // "a" is used as a matrix
	int i;
	
	a=malloc(SIZE*sizeof(int));

	for (i=0; i<SIZE; i++)
		a[i]=malloc(SIZE*sizeof(int));
	
	damned_function();
}

void damned_function () {
	int **b;
	
	b=malloc(SIZE*sizeof(int));
}
I do this "allocate thing" more than once in main and in the dammed_function as well.
But now, the problem is: there is always a "int **b" which gets the same memory position as "int **a" and my program's matrices get screwed.
Why the hell this happens? Is there a way out of this? Have I been programming for too long? Is there life after death? Do we speak Spanish in Brazil? Will these voices inside my head stop? Will Volta Redonda be Rio's champion this year?
...
Sorry about that... anyway, can anyone help?

Last edited by huble; 03-26-2005 at 10:08 PM.
 
Old 03-26-2005, 08:38 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Well, I would write it slightly differently. After compiling and running this, I get no problems with overlapping memory space. You can check yourself by comparing the printed values for arrays a and b.
Code:
#include <stdio.h>

#define SIZE     100 //arbitrary - you never mentioned it in your code

//================================================

void damned_function()
{
  int **b;

  b = malloc( SIZE * sizeof( int * ) );
  if( b == NULL )
  {
    fprintf( stderr, "not enough memory to reserve space for b!\n" );
    exit( 1 );
  }
  else
  {
    fprintf( stdout, "array b located at: 0x%X\n", b );
  }

  free( b );
}

//================================================

int main( int argc, char *argv[] )
{
  int **a;
  int i;

  a = malloc( SIZE * sizeof( int * ) );
  if( a == NULL )
  {
    fprintf( stderr, "not enough memory to reserve space for a!\n" );
    exit( 1 );
  }
  else
  {
    fprintf( stdout, "array a located at: 0x%X\n", a );
  }


  for( i = 0; i < SIZE; i++ )
  {
    a[i] = malloc( SIZE * sizeof( int ) );
    if( a[i] == NULL )
    {
      fprintf( stderr, "not enough memory to reserve space for a[%d]!\n", i );
      exit( 1 );
    }
    else
    {
      fprintf( stdout, "  a[%d] located at 0x%X\n", i, a[i] );
    }
  }

  damned_function();

  for( i = 0; i < SIZE; i++ )
    free( a[i] );
  free( a );

  return 0;
}
 
Old 03-26-2005, 10:21 PM   #3
huble
LQ Newbie
 
Registered: Feb 2005
Location: Rio de Janeiro, Brazil
Distribution: Slackware
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for the help Dark_Helmet, but my problem still is unsolved. What really happens is that, somehow, when I call malloc for "b", it returns the same pointer that it returned for "a[0]". I've checked every address of my variables and there is always one "b" pointing to the same place an "a[0]" points.
I have 4 "a"s in one function and 4 "b"s in another and the second declared of each are showing this problem.
Anything else?
 
Old 03-26-2005, 10:41 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Just curious, did you try running the code I posted? I'd be curious to see the output if you'd run it. I'm not saying I don't believe you... just that I think it would be interesting to see. If you do want to post it, drop the SIZE #define to something more reasonable

Since you didn't provide all of your code, I can only point out what I changed... Your first calls to malloc should be "sizeof( int * )" rather than "sizeof( int )". Also, I'm not sure how the compiler would handle an implicit declaration of "damned_function()". If you have a prototype declared, then ignore that last bit, but as a matter of habit, I always declare (or prototype) functions before I reference them.

The code I gave was compiled with gcc version 2.96 and glibc 2.2.4. If your problems persist, then perhaps it's an instability between gcc-glibc, an old version of one (or both), using the optimize option (-O) at too high a level, or possibly using a version of gcc or glibc that too new (like a CVS pull or something similar).
 
Old 03-26-2005, 11:07 PM   #5
huble
LQ Newbie
 
Registered: Feb 2005
Location: Rio de Janeiro, Brazil
Distribution: Slackware
Posts: 7

Original Poster
Rep: Reputation: 0
Sorry I forgot to tell, but your code went just fine.
And in the real program, SIZE is not a const, it's the size of the matrix I'm working with, getting to 1024 sometimes. Also, no need to worry about the prototype, they are there, and I've changed the thing about sizeof(int*), it sure makes more sense your way.
The one last chance to find the answer, I'm using gcc 3.3.4, too new maybe? I'll try some older one.
Ah, and thank you about the trouble again.

Last edited by huble; 03-26-2005 at 11:18 PM.
 
Old 03-26-2005, 11:20 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
No problem... I doubt gcc 3.3.4 would be your problem. Most likely it's just a bug hiding in the code somewhere. If you'd like to post the full code, I'd be happy to look at it.

Otherwise, my only other suggestion would be to compile debugging support in (-g option) and use a debugger like the Data Display Debugger. There may be more specific memory-usage/analysis tools available on freshmeat.net. It might be worth a look.
 
Old 03-26-2005, 11:44 PM   #7
huble
LQ Newbie
 
Registered: Feb 2005
Location: Rio de Janeiro, Brazil
Distribution: Slackware
Posts: 7

Original Poster
Rep: Reputation: 0
I'm trying to implement Strassen's algorithm for matrix multiplication, and I just can't get it to be faster than the usual algorithm.
Anyway, here's the code:
http://www.dcc.ufrj.br/~dre_104024390/multi.c
I have the bad habit not to comment the code, and there are plenty things in Portuguese... sorry about that...
Anyway, just take a look at the addresses printed. I've found that this problems happens twice: in the first and in the second variables, here at least.
It might still be some very silly stupid mistake made by myself... Hope it really is my fault...

Last edited by huble; 03-26-2005 at 11:54 PM.
 
Old 03-27-2005, 12:10 AM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You're not allocating memory correctly. a is of type int **, so you need to allocate SIZE * sizeof(int *) bytes to it. The same goes for b. And then a[whatever] and b[whatever] should be allocated SIZE * sizeof(int) bytes.

Last edited by itsme86; 03-27-2005 at 12:11 AM.
 
Old 03-27-2005, 12:13 AM   #9
huble
LQ Newbie
 
Registered: Feb 2005
Location: Rio de Janeiro, Brazil
Distribution: Slackware
Posts: 7

Original Poster
Rep: Reputation: 0
That's right itsme86, I've already fixed this. Thanx.
 
Old 03-27-2005, 12:26 AM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I've been able to poke around in it some wihtout the language causing any serious problems.

Okeydokey, I'm not familiar with the strassen method of matrix multiplication, so this may not be a valid point, but here goes...

In the code you posted, line 133-134:
Code:
x1=malloc(tamanho*sizeof(int*));
x2=malloc(tamanho*sizeof(int*));
line 149:
Code:
x1=matriz1;
line 173:
Code:
x2=matriz2;
line 198:
Code:
printf("%X %X %X %X\n", &x1[0], &x2[0], &x3[0], &x4[0]);
Something to note: there are no intervening calls to free() for x1 or x2. Also, after the re-assignments for x1 and x2, you will definitely see an "overlap" in the address range of matriz1, matriz2, x1, and x2 (because they are pointing to the same memory space ).
 
Old 03-27-2005, 12:45 AM   #11
huble
LQ Newbie
 
Registered: Feb 2005
Location: Rio de Janeiro, Brazil
Distribution: Slackware
Posts: 7

Original Poster
Rep: Reputation: 0

You were great! You've found something I did without even thinking why I was doing it. x1 and x2 lose their original target by doing it.
And I've found two more mistakes similar to these and now it is running smoothly!
Check the same url again in a few moments and you will see how it multiplies a n x n matrix filled with 2's without any problem.
You were a great help, man! Actually, you were THE help! Thanks a lot!


By the way, I don't use free() in order to run strassen as fast as possible, because, in theory, it has the order of n^2.81, while the usual way has n^3, where n is the size of the matrix.

EDIT: The right code is there now.

Last edited by huble; 03-27-2005 at 01:05 AM.
 
  


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
A problem with malloc failing dogbird Linux - Software 1 11-17-2005 05:58 AM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
malloc() vijeesh_ep Programming 4 08-25-2004 03:50 PM
about malloc eshwar_ind Programming 11 02-18-2004 03:41 PM
malloc within a function MadCactus Programming 4 12-26-2003 10:59 AM

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

All times are GMT -5. The time now is 10:32 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