LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-24-2006, 09:02 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Rep: Reputation: 30
C: malloc arrays- unexpected output


Hi,
i've written this:
Code:
#include <stdio.h>
#include <stdlib.h>

#define CHUNKS 10

int *allocate_int_array(int chunks_num)
{
	return (int *)malloc(chunks_num*sizeof(int));
}



main()
{
	int *exp_array=allocate_int_array(CHUNKS);

	
	
	int i;
	
	for (i=0; i<10; i++)
	{		
		*exp_array=i;
		exp_array+=sizeof(int *);
	}
	
	int j;
	for (j=10;j>0;j--)
	{
		printf("%i \n",*exp_array);
		exp_array-=sizeof(int *);
	}
	
	free(*exp_array);
}
When i execute it i get:
Code:
$ ./c1
0 
9 
8 
7 
6 
5 
4 
663604 
2 
1
Why don't i get this? What is this 663604? (looks like an address)
Code:
9
8
7
6
5
4
3
2
1
0
 
Old 03-24-2006, 10:13 AM   #2
bc8o8
Member
 
Registered: Jun 2003
Location: Boston, MA
Distribution: Debian
Posts: 57

Rep: Reputation: 15
The lines
Code:
===snip===
   exp_array+=sizeof(int *);
===snip===
   exp_array-=sizeof(int *);
===snip===
are incrementing your array location by sizeof(int *) which (on my system) is 4. So you are skipping valid locations and reading/writing other areas in memory. What you want to do instead is actually just increment the pointer -- pointers are smart enough to know their size, so it will increment appropriately.

For example the following should do what you want
Code:
for (i=0; i<10; i++)
{		
	*exp_array=i;
	exp_array++;
}

int j;
for (j=10;j>0;j--)
{
	printf("%i \n",*exp_array);
	exp_array--;
}
although, instead of moving your exp_array pointer around, a better approach would be to just use exp_array[ i ] = i; and either printf("%i \n",exp_array[ i ]); or printf("%i \n",exp_array+i); (which are identical)
 
Old 03-24-2006, 04:49 PM   #3
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally Posted by bc8o8
Code:
int j;
for (j=10;j>0;j--)
{
	printf("%i \n",*exp_array);
	exp_array--;
}
This will still not work; instead:
Code:
{
	exp_array--;
	printf("%i \n",*exp_array);
}
 
Old 03-24-2006, 09:30 PM   #4
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Quote:
Originally Posted by nocturna_gr
Hi,
i've written this:
Code:
#include <stdio.h>
#include <stdlib.h>

#define CHUNKS 10

int *allocate_int_array(int chunks_num)
{
	return (int *)malloc(chunks_num*sizeof(int));
}



main()
{
	int *exp_array=allocate_int_array(CHUNKS);

	
	
	int i;
	
	for (i=0; i<10; i++)
	{		
		*exp_array=i;
		exp_array+=sizeof(int *); // exp_array++; 
	}
	
	int j;
	for (j=10;j>0;j--)
	{
		printf("%i \n",*exp_array);
		exp_array-=sizeof(int *); // exp_array--;
	}
	
	free(*exp_array); // free(exp_array);
}
................
Cheers,
GH.
 
  


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
malloc anoosh Programming 1 03-15-2006 04:41 PM
malloc eagle683 Programming 6 05-22-2005 02:40 PM
malloc() vijeesh_ep Programming 4 08-25-2004 03:50 PM
Unexpected output from 'ls' when using glob expressions psiakr3w Linux - General 7 07-22-2004 03:21 AM
about malloc eshwar_ind Programming 11 02-18-2004 03:41 PM


All times are GMT -5. The time now is 05:42 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration