I am trying to produce a program which produces a random array of numbers but I am running into a problem in that when the array becomes even a little large, say 10x10, I get the Bus Error or Segmentation Fault (I think I still get the error with even smaller arrays). Can someone point out what I am doing wrong? It's probably really stupid, but I am really new to this so I make a lot of stupid mistakes :$

Here's my code:

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

int main ( void )

{
	int i, j, *randno[i][j], arraysize;

	arraysize = 100;
	srand ( (unsigned)time ( NULL ) );

for(i = 0; i < 10000; i++)
	{
		randno[i][j] = malloc(arraysize * sizeof(int));
	}

	for(i=0;i<10;i++)

	{
		for (j=0;j<10;j++)

		{
			randno[i][j] = rand() / ( RAND_MAX / 100 + 1 );

			printf ( "random number = %d\n", randno[i][j] );

		}
	}
	return 0;

}

I'm also fairly unsure with malloc so if anyone can show what I am doing wrong there it would be greatly appreciated!!!

>>lint 9: *randno[j],

That is not leagal way to declare an array because i and j must be constants. What you have are two uninitialized integers.

If you want to declare a two-dimensional array of integers but you don't know how many rows and columns then declare it with a double star like this: int ** randno = 0; Now, to allocate both dimensions

arraysize = 100; // number of rows
randno = malloc(arraysize * sizeof(int *)); // allocate array of pointers
//
// now allocate the columns for each row
for(i = 0; i < arraysize; i++)
{
    randno[i] = malloc(10 * sizeof(int)); // each row has 10 integers
}
commented: Straight in center +1
for(i = 0; i < arraysize; i++)
{
    [B]randno[i] = malloc(10 * sizeof(int)); [/B]//  [B]TYPECAST THE OUTPUT OF MALLOC[/B]
}arraysize = 100; // number of rows
randno = malloc(arraysize * sizeof(int *)); // allocate array of pointers

And one more thing to add, Always typecast the output of malloc into desired type as malloc returns void* type address...

>>// TYPECAST THE OUTPUT OF MALLOC

Not in C language. C++ requires the typecase but C does not.

I am still having a bit of trouble with this. I don't think I have implemented your corrections properly. Also it is giving me the message:

warning: assignment makes pointer from integer without a cast

But from my understanding this isn't too much to worry about.

Here's my code so far:

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

int main ( void )

{
	int i, j, *randno[i][j], arraywidth, arrayheight;

	arraywidth = 100;
	randno = malloc(arraywidth * sizeof(int *));
	srand ( (unsigned)time ( NULL ) );

for(i = 0; i < arraywidth; i++)
	{
		randno[i] = malloc(10 * sizeof(int));
	}

	for(i=0;i<10;i++)

	{
		for (j=0;j<10;j++)

		{
			randno[i][j] = rand() / ( RAND_MAX / 100 + 1 );

			printf ( "random number = %d\n", randno[i][j] );

		}
	}
	return 0;

}

Can you give me another shove in the right direction please?

>>warning: assignment makes pointer from integer without a cast
You are compiling it as C++. Change the file extension to *.c and you will not get that warning.

you still have not fixed the error on line 8. int i, j, **randno, arraywidth, arrayheight; Go back and re-read the code I previously posted. Your code is still incorrect.

//Not in C language. C++ requires the typecase but C does not.

Sir, i think you are mistaken......
This is the declaration of 'malloc()' as taken from the 'man' page:

void *malloc(size_t size);

It is clear from the declaration that malloc returns 'void*' and hence we need to typecast the return address

And if i am not mistaken sir, C++ uses 'new' operator for dynamic memory allocation

If i am wrong, then i would really appreciate you correcting me

C++ also supports malloc(), although you are right in that new is preferrable.

For typecasing malloc(), please read this. Need to include stdlib.h to correct the warning, not do a typecast.

Thanks for your comment sir.
I tried the following code and i did not get any warnings:

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

int main(void)
{
  char* p = malloc(5);
  free(p);

  return 0;
}

As the link posted by you suggested that if i do not include the header 'stdlib.h' then compiler will give a warning of the form:
assignment of pointer from integer lacks a cast
But, when i tried the same, my compiler gave me the following warnings:
malloc.c:6: warning: implicit declaration of function ‘malloc’
malloc.c:6: warning: incompatible implicit declaration of built-in function ‘malloc’
malloc.c:7: warning: implicit declaration of function ‘free’

So, is the second warning in the list of 3 warnings maps with the warning that the link suggested???

Not entirely. gcc (which I guess you're using) has some built-in functions (as evidenced by the second message). First, turn these off to get a better idea of what's going on. gcc -fno-builtin malloc.c This done, the compiler should be then forced to follow the normal C sematics for undeclared functions, which would be
int malloc();

Since you're assigning to a pointer at that stage, you should end up with the "int to pointer" message.

There is no standard for error messages (except where the standard says an error message is required), so wording and numbers of error messages vary between compilers.

Ya, thanks, now i got the exact warning..

Hi.

Thanks for all your help so far, but I have become stuck again with a bus error.

Here's the code

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

int main ( void )

{
	int i, j, ** lattice = 0, row, col, sum = 0, average, check;
	
	row = 10;
	col = 10;
	
	lattice = malloc (row * sizeof(int *));
	srand ( (unsigned)time ( NULL ) );

	for(i = 0; i < row; i++)
	{
		lattice[i] = malloc(col * sizeof(int));
	}
 
	for(i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			lattice[i][j] = rand() / ( RAND_MAX / 10 + 1 );

			printf ( "random number = %d\n", lattice[i][j] );

		}
	}

	for(i=0; i < row; i++);
	{
		for(j=0; j < col; j++);
		{
	
			sum = sum + lattice [i][j];
	
			printf ( "Total Energy in Lattice = %d\n", sum );
	
		}
	}

	average = sum / (row * col);
	printf ( "Average Energy in each position = %d", average);

	check = 0;

	while (check = 0)
	{
	average = sum / (row * col);
	printf ( "Average Energy in each position = %d", average);
	check = 1;
	
		if (lattice [i][j] != 0)
		{
			i = rand() / (RAND_MAX / row + 1);
			j = rand() / (RAND_MAX / col + 1);
	
			lattice[i][j] = (lattice [i][j])--;
	

			i = rand() / (RAND_MAX / row + 1);
			j = rand() / (RAND_MAX / col + 1);

			lattice[i][j] = (lattice [i][j])++;
		}

		for (i = 0; i < row; i++)
		{
			for (j = 0; j < col; j++)
			{
				if (lattice[i][j] != average)
				{
				check = 0;
				}
			
			}
		}
	
	}
return (0);
}

The creation of the lattice runs fine, but now I have added the while loop I again receive a bus error. I don't think the program gets as far as the while loop though so I am a bit confused.

> for(i=0; i < row; i++);
Yeah, guess what that last ; does

IIRC, something like -Wall as a compiler option would be a good idea.

haha, thanks thats a really stupid error but I didnt see it

Getting a bus error in the while loop now. Arrrgh this code is driving me nuts

> while (check = 0)
So ==

Did you use -Wall ?

It will catch a lot of problems which are legal, but stupid.

No I didn't use -wall because I am compiling with gcc and its not an option on that compiler (as far as I know!)

I am still getting a bus error in the while loop by the way, and as I'm new and rubbish at spotting these I really would be grateful if someone could spot it for me

Never mind I have seen my error and its REALLY stupid. thanks for all your help everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.