GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Jul-2005, 06:57
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

passing tables as arguments to functions.


Again, the assmbler project ...

I have these tables defined :
CPP / C++ / C Code:
//The data table.
int *data_table;
//The instruction table.
struct inode *instruction_table;
with these definitions atop :
CPP / C++ / C Code:
typedef enum {a,e,r} instruction_type;
//This structure is defined to hold an instruction.
struct inode
{
	int					the_instruction:16;
	instruction_type	the_instruction_type;
};

Now, in my main function, I add data and instructions into the tables.
the data and instructions are added o.k. (I can print the data from the main function).

Now, I pass the tables to another function to create output files.

This function is defined as :
CPP / C++ / C Code:
int create_object_file(char *file_name,int *data_table,struct inode *instruction_table,int DC,int IC)
{
	int result = PASSED;
	FILE *objfp;
	//Create object file.
	objfp = fopen(add_suffix(file_name,".ob"),"w");
	if (objfp == NULL)
	{
		fprintf(stderr,"Couldn't create object file : %s.\n",add_suffix(file_name,".ob"));
		result = FAILED;
	}
	else
	{
		int index;
		//File created succesfully.
		//Print header info.
		//Print instruction table.
		for (index = 0;index < IC;index++)
			fprintf(objfp,"%.6o\t%.6o\t%c\n",index,instruction_table[index].the_instruction,instruction_table[index].the_instruction_type);
		//Print data table.
		for (index = 0;index < DC;index++)
			fprintf(objfp,"%.6o\t%.6o\n",index + IC,data_table[index]);
	}
	return(result);

}

and I call to it from main like this :
CPP / C++ / C Code:
//Create object file.
create_object_file(filename,data_table,instruction_table,DC,IC);

Now, after debugging I can tell that the data and instruction tables are not passed o.k. since these calls :
CPP / C++ / C Code:
for (index = 0;index < IC;index++)
   fprintf(objfp,"%.6o\t%.6o\t%c\n",index,instruction_table[index].the_instruction,instruction_table[index].the_instruction_type);
//Print data table.
for (index = 0;index < DC;index++)
   fprintf(objfp,"%.6o\t%.6o\n",index + IC,data_table[index]);

generate access violation ...

What am I doing wrong ? How should I pass the tables correctly ?

Best regards,
Kobi.
  #2  
Old 25-Jul-2005, 08:04
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
CPP / C++ / C Code:
struct inode *instruction_table;
Where do you allocate memory into which you might put stuff?
  #3  
Old 25-Jul-2005, 10:22
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about
Quote:
Originally Posted by Dave Sinkula
CPP / C++ / C Code:
struct inode *instruction_table;
Where do you allocate memory into which you might put stuff?

The memory allocation is done within the function :
CPP / C++ / C Code:
struct inode *add_instruction(struct inode *instruction_table,int instruction
							  ,instruction_type the_instruction_type,int *instruction_counter)
{
	//Increment instruction counter.
	(*instruction_counter)++;
	//(Re)allocate the memory for the instruction table.
	instruction_table = (struct inode*)realloc(instruction_table,
						(*instruction_counter) * sizeof(struct inode));
	if (instruction_table == NULL)
	{
		//Memory allocation failed.
		fprintf(stderr,"Error : Couldn't (re)allocate memory for instruction table.\n");
		return (NULL);
	}
	else
	{
		//Memory (re)allocation success.
		instruction_table[(*instruction_counter) - 1].the_instruction = instruction;
		instruction_table[(*instruction_counter) - 1].the_instruction_type = the_instruction_type;
	}
	return (instruction_table);
}

And :
CPP / C++ / C Code:
int *add_data(int *data_table,int data,int *data_counter)
{
	//Increment data counter.
	(*data_counter)++;
	//(Re)allocate the memory for the data table.
	data_table = (int*)realloc(data_table,(*data_counter) * sizeof(int));
	if (data_table == NULL)
	{
		//Memory allocation failed.
		fprintf(stderr,"Error : Couldn't (re)allocate memory for data table.\n");
		return (NULL);
	}
	else
		//Memory (re)allocation success.
		data_table[(*data_counter) - 1] = data;
	return (data_table);
}

and the call to these functions (an example from the code):
CPP / C++ / C Code:
data_table = add_data(data_table,iterator->token[index],DC);
  #4  
Old 25-Jul-2005, 11:33
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

The question in a different form.


Why does my_table doesn't hold the changes ?

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>
#include "data_table.h"

void add_numbers(int *the_table,int *DC);

void main()
{
	int *my_table = NULL;
	int DC = 0;
	add_numbers(my_table,&DC);
	if (my_table == NULL)
		printf("NULL\n");
}

void add_numbers(int *the_table,int *DC)
{
	the_table = add_data(the_table,1,DC);
}

The function add_data if defined :
CPP / C++ / C Code:
int *add_data(int *data_table,int data,int *data_counter)
{
	//Increment data counter.
	(*data_counter)++;
	//(Re)allocate the memory for the data table.
	data_table = (int*)realloc(data_table,(*data_counter) * sizeof(int));
	if (data_table == NULL)
	{
		//Memory allocation failed.
		fprintf(stderr,"Error : Couldn't (re)allocate memory for data table.\n");
		return (NULL);
	}
	else
		//Memory (re)allocation success.
		data_table[(*data_counter) - 1] = data;
	return (data_table);
}
  #5  
Old 25-Jul-2005, 12:25
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Quote:
Originally Posted by kobi_hikri
Why does my_table doesn't hold the changes ?

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>
#include "data_table.h"

void add_numbers(int *the_table,int *DC);

void main()
{
	int *my_table = NULL;
	int DC = 0;
	add_numbers(my_table,&DC);
	if (my_table == NULL)
		printf("NULL\n");
}

void add_numbers(int *the_table,int *DC)
{
	the_table = add_data(the_table,1,DC);
}

The function add_data if defined :
CPP / C++ / C Code:
int *add_data(int *data_table,int data,int *data_counter)
{
	//Increment data counter.
	(*data_counter)++;
	//(Re)allocate the memory for the data table.
	data_table = (int*)realloc(data_table,(*data_counter) * sizeof(int));
	if (data_table == NULL)
	{
		//Memory allocation failed.
		fprintf(stderr,"Error : Couldn't (re)allocate memory for data table.\n");
		return (NULL);
	}
	else
		//Memory (re)allocation success.
		data_table[(*data_counter) - 1] = data;
	return (data_table);
}

You should pass a pointer to the pointer variable my_table in function add_numbers and not the actual pointer.

CPP / C++ / C Code:
int *my_table = NULL;
int DC = 0;
add_numbers(&my_table,&DC);

CPP / C++ / C Code:
void add_numbers(int **the_table,int *DC)
{
	(*the_table) = add_data((*the_table),1,DC);
}

CPP / C++ / C Code:
int *add_data(int *data_table,int data,int *data_counter)
{
	//Increment data counter.
	(*data_counter)++;
	//(Re)allocate the memory for the data table.
	data_table = (int*)realloc(data_table,(*data_counter) * sizeof(int));
	if (data_table == NULL)
	{
		//Memory allocation failed.
		fprintf(stderr,"Error : Couldn't (re)allocate memory for data table.\n");
		return (NULL);
	}
	else
		//Memory (re)allocation success.
		data_table[(*data_counter) - 1] = data;
	return (data_table);
}
  #6  
Old 25-Jul-2005, 12:36
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
First, with regard to your use of realloc, is this item to note.

It might be easier if you could post a minimal subset of your code, and any necessary input file, that demonstrates the problem. If I were to go about cobbling together something, it might not be what you actually have.
  #7  
Old 27-Jul-2005, 03:28
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

I Understand.


Quote:
Originally Posted by Dave Sinkula
First, with regard to your use of realloc, is this item to note.

It might be easier if you could post a minimal subset of your code, and any necessary input file, that demonstrates the problem. If I were to go about cobbling together something, it might not be what you actually have.

Thanks.
I'll be brief next time.
  #8  
Old 27-Jul-2005, 03:31
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Thanks for the example.


Quote:
Originally Posted by nkhambal
You should pass a pointer to the pointer variable my_table in function add_numbers and not the actual pointer.

I thought that was the answer but I still don't understand why... And I never do things I don't understand
Can you please explain why passing a plain pointer to a variable isn't "good enough" ?
  #9  
Old 27-Jul-2005, 07:29
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by kobi_hikri
I thought that was the answer but I still don't understand why... And I never do things I don't understand
Can you please explain why passing a plain pointer to a variable isn't "good enough" ?
In C, parameters are passed by value. So you give the function a copy of the pointer. You modify the copy. The original remains unchanged. If you want to actually modify the value, you can simulate pass by reference using a pointer and modify what it points to.

[edit]Very much like this
CPP / C++ / C Code:
#include <stdio.h>

void foo(int x)
{
   x += 10;
   printf("x = %d\n", x);
}

void bar(int *x)
{
   *x += 10;
   printf("x = %d\n", *x);
}

int main(void)
{
   int i = 50;
   printf("i = %d\n", i);
   foo(i);
   printf("i = %d\n", i);
   bar(&i);
   printf("i = %d\n", i);
   return 0;
}

/* my output
i = 50
x = 60
i = 50
x = 60
i = 60
*/
Remember that pointers are variables too, and behave much the same.
[edit2]
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

void foo(void *x)
{
   printf("x = %p\n", x);
   x = malloc(10);
   printf("x = %p\n", x);
}

void bar(int **x)
{
   printf("*x = %p\n", *x);
   *x = malloc(10);
   printf("*x = %p\n", *x);
}

int main(void)
{
   void *p = NULL;
   printf("p = %p\n", p);
   foo(p);
   printf("p = %p\n", p);
   bar(&p);
   printf("p = %p\n", p);
   return 0;
}

/* my output
p = 00000000
x = 00000000
x = 007A323C
p = 00000000
*x = 00000000
*x = 007A324C
p = 007A324C
*/
[edit3]The above is an example only -- its foo leaks memory, the bar doesn't, but the memory should be freed.
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing multiple objects in functions crystalattice C++ Forum 3 08-Oct-2004 08:18
passing functions oshiotse C++ Forum 0 27-May-2004 14:51
Help on passing in arrays in functions? nusstu C Programming Language 10 02-Apr-2004 10:42
Passing Pointers To Pointers in Functions elumira C Programming Language 8 05-Mar-2004 21:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:58.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.