Malloc an Array

i can't malloc the array perfect

Page 1 of 1

7 Replies - 2081 Views - Last Post: 10 April 2009 - 06:13 PM Rate Topic: -----

#1 pontosanpele  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 09

Malloc an Array

Posted 09 April 2009 - 10:40 AM

Problem: floating point exception
I have to reverse in a special way the array and i have to do with a dynamic arra so with malloc, i think the problem is where i'm using malloc...i think, but i'm not sure.
When i read in the length of the array after then i read in the elements and after that comes the floating point exception.
Somebody can help me?

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

void buildarray(int* size, int* array)	//we build the sequence
{
   int k;
   int i;
   printf("Give me the lenght of the array:");
   scanf("%d", size);
   array= (int*) malloc ((*size)*sizeof(int));
   printf("The allocated memory size is:%d\n",*size);
	for (i=0; i<*size;i++)
	 {
			printf("x[%d]:", i+1);
		 scanf("%d", &array[i]);
	 }
   printf("End of the reading!");
   printf("Give me a number to div the array:");
   scanf(":%d", &k);
}

void reverse(int size, int* array)	  //we revers it

{	 int k;
	 int x;
	 int i;
	 int size1;
	 int start;
   	 i=0;

   size1= size / k;
   if (((size % k)!= 0) || (size<k))
		printf("Can't div the array with the number!");
	else
	   for (x=0;x<k;x++)
	 		{
				  for (i = (size1*x); i < (((size1*x)+size1/2)); i++)
						   {
					 int temp;
					  start=size1*x;
							  temp = array[i];
							  array[i] = array[size1 - 1 - i+2*start];
							  array[size1 - 1 - i+2*start] = temp;
							 }
		 		for (i = (size1*x); i < ((size1*x)+size1); i++)
						printf ("%d ",array[i]);
							   printf ("\n");
			}
}

int main()   //main program
{
int *array;
int size;
buildarray(&size, array);
reverse(size, array);
return 0;
}




This post has been edited by pontosanpele: 09 April 2009 - 10:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Malloc an Array

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Malloc an Array

Posted 09 April 2009 - 07:55 PM

You do realize that you set "k" in one function and never pass it to the other right? So you can't use it when you go to divide.

size1= size / k; //<-- Can't, k isn't the same "k" as in the other function
if (((size % k)!= 0) || (size<k)) //<-- Again, k is null right now.



You need to pass the value of "k" from one function to the other. But that isn't going to be your only problem. Your nested for loop conditions are not right. You might want to think about a center point of the array and how you can use this center of the array as a way to know when to stop switching an element from the beginning with one from the end.

So if our array is 5 elements, our center element will be element 2, switch 0 with 4, 1 with 3 and 2 will hold still in the center and when the start equals the center, then you know you have switched up the entire array.

If the array number is even, then you are going to switch all elements... with an array with 6 elements... 0 switches with 5, 1 switches with 4, 2 switches with 3 and if start is > than the center, you know you can stop switching.

:)
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Malloc an Array

Posted 09 April 2009 - 08:01 PM

Inside the scope of the function buildarray(),. "array" is a LOCAL copy of the array variable that you defined in main. That local pointer "array" ceases to exist at the end of the function, and so does your handle on the memory that was allocated. malloc() returns an address, and you want to assign that address to the main() function's array variable. Just like any other case where you want a called function to change the value of a variable in the calling function, you have to pass a pointer to that variable. So you have to pass a pointer to array, not a copy. Since array is type int*, a pointer to array is type int**.

You will have a similar problem in your reverse() function.

You also have a problem with your variables k, since the k in buildarray() and the k in reverse() are two completely separate and independent variables.
Was This Post Helpful? 0
  • +
  • -

#4 pontosanpele  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 09

Re: Malloc an Array

Posted 09 April 2009 - 10:41 PM

The reversing is good...because i have to reverse like: the length of the array is 6, the elements are 1 2 3 4 5 6 and the k is 3 so the new reversed array will be 2 1 4 3 6 5, if the k is 2 then 3 2 1 6 5 4 ... and i have a solution with static memory where array[100] but here with the dynamic there is the problem...
So i will initialize the k like a global variable and i have to initialize the int * array like a global variable? like here :
#include <stdio.h>
#include <stdlib.h>

int * array;
int k;
void buildarray(int* size, int* array)	//we build the sequence
{
   int i;
   printf("Give me the lenght of the array:");
   scanf("%d", size);
   array= (int*) malloc ((*size)*sizeof(int));
   printf("The allocated memory size is:%d\n",*size);
	for (i=0; i<*size;i++)
	 {
			printf("x[%d]:", i+1);
		 scanf("%d", &array[i]);
	 }
   printf("End of the reading!");
   printf("Give me a number to div the array:");
   scanf(":%d", &k);
}

void reverse(int size, int* array)	  //we revers it

{ 
	 int x;
	 int i;
	 int size1;
	 int start;
		i=0;

   size1= size / k;
   if (((size % k)!= 0) || (size<k))
		printf("Can't div the array with the number!");
	else
	   for (x=0;x<k;x++)
			 {
				  for (i = (size1*x); i < (((size1*x)+size1/2)); i++)
						   {
					 int temp;
					  start=size1*x;
							  temp = array[i];
							  array[i] = array[size1 - 1 - i+2*start];
							  array[size1 - 1 - i+2*start] = temp;
							 }
				 for (i = (size1*x); i < ((size1*x)+size1); i++)
						printf ("%d ",array[i]);
							   printf ("\n");
			}
}

int main()   //main program
{
int size;
buildarray(&size, array);
reverse(size, array);
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Malloc an Array

Posted 09 April 2009 - 10:56 PM

Making the array global won't solve your problem.
Was This Post Helpful? 0
  • +
  • -

#6 pontosanpele  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-April 09

Re: Malloc an Array

Posted 10 April 2009 - 11:58 AM

View Postr.stiltskin, on 9 Apr, 2009 - 09:56 PM, said:

Making the array global won't solve your problem.


ok, then cane you help me a little in the code?
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Malloc an Array

Posted 10 April 2009 - 12:08 PM

I already tried to explain it in post #3. What don't you understand?
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Malloc an Array

Posted 10 April 2009 - 06:13 PM

Alternately if you don't want to deal with **, you can return the pointer/array from the function to be assigned to w/e in main().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1