#include<stdio.h>
#include "amicablenumber.h"
int i,j;
struct amicable
{
int **amicablePair;
int size;
};
main()
{
int startnum = 250;
int endnum = 1000;
struct amicable* ami;
ami = getAmicablePairs(startnum, endnum);
printf("{");
for(int i = 0; i<ami->size; i++)
{
printf("{%d, %d}",ami->amicablePair[i][0], ami->amicablePair[i][1]);
}
printf("}");
}
amicable *getAmicablePairs(int startnum,int endnum)
{
int size=0;
int sumfactors(int);
amicable record;
for(i=startnum;i<=endnum;i++)
{
for(j=endnum;j>=startnum;j--)
{
if((sumfactors(i)==j)&&(sumfactors(j)==i) && (i!=j))
{
record.amicablePair[size][0]=i;
record.amicablePair[size][1]=j;
size++;
}}}
record.size=size;
return record;
}
int sumfactors(int number)
{
int sum=0;
for(i=1;i<number;i++)
{
if(number%i==0)
sum +=i;
}
return sum;
}
in the above code i m getting a error
cannot convert amicable to amicable* in return
getAmicablePairs is declared to return a pointer to an amicable:
amicable *getAmicablePairs(...)
but you then try to return an amicable:
return record;
rather than a pointer to one.
Note that one "obvious" fix, which is to return a pointer to record:
return &record;
won't work, because you'd be returning a pointer to a variable that was about to go away as soon as getAmicablePairs returns. Instead you need to create a record using malloc and return that; something like this:
amicable *record = (amicable*) malloc(sizeof(amicable));
You'll need to change all your record. into record->.
Note also that you're writing into the amicablePair member of your structure without allocating it - that's going to cause a crash. You need to malloc the amicablePair as well as the amicable.
You are returning an (amicable *) - a pointer to an amicable, but your function creates an (amicable) (not a pinter to one).
Instead of declaring
amicable record;
you need to do this (or an equivalent):
amicable *record = (amicable *) malloc(sizeof(amicable));
and then access via "record->" rather than "record."
Note: With the above approach you will need to free() the above allocation when you are finished with it.
Related
The following code works fine without the statement d = *dummy; which is a double pointer dereference. However if this line is present, a segmentation fault occurs. Why so?
The code allocates and initializes memory for data structs dynamically. I was trying to simplify access to the returned pointer-to-pointer.
#include <stdlib.h>
#include <stdio.h>
typedef struct s_dummy {
char dummy_number;
} Dummy;
int mock_read_from_external_source() {
return 4;
}
int load_dummies(Dummy** dummies, int* num_of_dummies) {
*num_of_dummies = mock_read_from_external_source();
*dummies = (Dummy*) calloc(*num_of_dummies, sizeof(Dummy));
if (!dummies) {
return 1; // allocation unsuccessful
}
// Iterate dummies and assign their values...
for (int i = 0; i < *num_of_dummies; i++) {
(*dummies + i)->dummy_number = i;
}
return 0;
}
void main() {
Dummy** dummies;
Dummy* d;
int num_of_dummies = 0;
int *p_num_of_dummies = &num_of_dummies;
int err;
err = load_dummies(dummies, p_num_of_dummies);
// Segmentation fault occurs when dummies is dereferenced
d = *dummies;
if (err) {
exit(err);
}
for (int i = 0; i < num_of_dummies; i++) {
printf("Dummy number: %d\n", (*dummies + i)->dummy_number);
}
}
Thanks in advance.
You are getting the fault because of UB, in part caused by trying to use variable objects without memory. dummies, although created as a Dummies **, has never been provided memory. At the very least, your compiler should have warned you about dummies not being initialized in this call:
err = load_dummies(dummies, p_num_of_dummies);
This is easily addressed by simply initializing the variable when it is created:
Dummy** dummies = {0}; //this initialization eliminates compile time warnings
^^^^^
Then come the run-time errors. The first is called a fatal run-time on my system, which means the OS refused to continue because of a serious problem, in this case an attempt to dereference a null pointer in this line:
dummies = (Dummy) calloc(*num_of_dummies, sizeof(Dummy));
Because you created a Dummy ** called dummies, the first step is to create memory for the pointer to pointers dummies, then create memory for the several instances of dummies[i] that will result. Only then can the members of any of them be written to.
Here is one method illustrating how memory can be created for a Dummies pointer to pointers, ( d ) and several Dummies instances ( d[i] ):
Dummy ** loadDummies(int numPointers, int numDummiesPerPointer)
{
int i;
Dummy **d = {0};
d = malloc(numPointers * sizeof(Dummy *));//Create Dummies **
if(!d) return NULL;
for(i=0;i<numPointers;i++)
{ //Now create Dummies *
d[i] = malloc(numDummiesPerPointer*sizeof(Dummy)); //random size for illustration
if(!d[i]) return NULL;
}
return d;
}
In your main function, which by the way should really be prototyped at a minimum as: int main(void){...}, this version of loadDummies could be called like this:
...
Dummies **dummies = loadDummies(4, 80);
if(!dummies) return -1;//ensure allocation of memory worked before using `dummies`.
...
After using this collection of dummies, be sure to free all of them in the reverse order they were created. Free all instances of dummies[0]-dummies[numPointers-1] first, then free the pointer to pointers, dummies
void freeDummies(Dummy **d, int numPointers)
{
int i;
for(i=0;i<numPointers;i++)
{
if(d[i]) free(d[i]);
}
if(d) free(d);
}
Called like this:
freeDummies(dummies, 4);
dummies was never assigned a value, so de-referencing will attempt to reach some random memory which is almost certainly not going to be part of your program's allocated memory. You should have assigned it to &d.
But you don't even need to do that. Just use &d once when you call the function.
Also, if you return the number of dummies allocated instead of 1/0, you can simplify your code. Something like the below (not tested):
#include <stdio.h>
int mock_read_from_external_source() {
return 10;
}
typedef struct Dummy {
int dummy_number;
} Dummy;
int load_dummies(Dummy** dummies) {
int want, i = 0;
if((want = mock_read_from_external_source()) > 0) {
*dummies = (Dummy*) calloc(want, sizeof(Dummy));
if(*dummies) {
// Iterate dummies and assign their values...
for (i = 0; i < want; i++) {
(*dummies)[i].dummy_number = i;
}
}
}
return i;
}
int main() {
Dummy* d = NULL;
int num_of_dummies = load_dummies(&d); // when &d is de-referenced, changes are reflected in d
if(num_of_dummies > 0) {
for (int i = 0; i < num_of_dummies; i++) {
printf("Dummy number: %d\n", d[i].dummy_number);
}
}
if(d) { // clean up
free(d);
}
return 0;
}
Well, I'm implementing a hash table with array of struct form, like this:
int SIZE = 769;
int entries=0;
typedef struct entry {
long id;
long n_article;
long id_rev;
long uni_rev;
} Entry;
typedef Entry * THash;
THash init_THash ()
{
int i;
THash t = (THash) malloc(SIZE*sizeof(struct entry));
//...
return t;
}
I have a function that add something to the hash table and if the entries is more than 70% of the SIZE, I resize the table.
THash resize_THash (THash h){
int i;
int prime = SIZE*2;
h = (THash) realloc (h,(prime)*sizeof(struct entry));
//...
SIZE = prime;
return h;
}
void add_THash (THash h,long id, long idrevision){
int i,r=0;
if (entries > SIZE * 0.7) h=resize_THash(h);
//...
entries++;
}
The init of the hash table is correct, but the problem is when I realloc/resize 3 times, stops working, giving me segmentation fault; At this point I tried everything and I failed. Anyone can explain me, why this implementation is wrong?
For example: in this main, if the condition is i<3000 it works, but if it's i<6000, doesnt work.
int main()
{
int i;
THash t = init_THash();
for(int i=10;i<3000;i++){
add_THash(t,i,627604899);
}
printf("SIZE:%d\n",SIZE);
printf("ENTRIES: %d\n",entries);
return 0;
}
The add_Thash function doesn't return the new pointer, leaving the caller to use the old, now invalid, one.
So I´m stuck with this sort function because everything seems to work fine when I debug it and there are no errors or warnings what so ever but it somehow gets stuck in an infinite loop.
My struct(if it helps):
typedef struct raeume{
char number[5];
char klasse[6];
int tische;
}raeume;
my start of the qsort function:
void ausgabesortiert(struct raeume *arr[],int used,int size)
{
qsort(*arr,size,sizeof(raeume),cmp);
ausgabesortiert(arr,size,used);
}
my compare function:
int cmp(const void * a, const void * b)
{
raeume *raumA = (raeume *) a;
raeume *raumB = (raeume *) b;
int tempA = raumA->klasse[0] - '0';
int tempB = raumB->klasse[0] - '0';
if(tempA < tempB)
{
return -1;
}
else if(tempA > tempB)
{
return 1;
}
else if(tempA == tempB)
{
if(raumA->tische > raumB->tische)
{
return -1;
}
else if(raumA->tische < raumB->tische)
{
return 1;
}
else if(raumA->tische == raumB->tische)
{
return 0;
}
}
return 0;
}
The declaration of your ausgabesortiert function
void ausgabesortiert(struct raeume *arr[],int used,int size)
clearly suggests that array arr contains pointers to struct raeume objects, not the objects themselves.
But the call to qsort
qsort(*arr,size,sizeof(raeume),cmp);
and the comparison function are written as if you are trying to sort an array of struct raeume objects themselves that begins at arr[0] location.
While there's nothing formally invalid in this, it still looks rather strange. Is this really your intent? What exactly are you trying to sort, again? The arr array or some other array pointed by arr[0]? I suspect that it is the former, in which case you need to fix the qsort call and comparison function.
I'm having some very strange bug in my ANSI C program.
I'm using debugger and I've observed that 'size' variable is corrupted in function 'doSthing.' Outside of 'doSthing' 'size' got a proper value, but inside 'doSthing' I've got a value nothing similar to what it should be, possibly some random data. This would be not be such a mystery but...
In 'doAnotherThing' which is called from 'doSthing' I get the proper value again. I suppose if it passes the correct value, it is not corrupted anyway, am I wrong? But then why does it have a different value?
The pointer in struct does not change inside the functions.
Memory is allocated for both oTV and oTV->oT.
I really don't see what's happening here...
typedef struct{
ownType *oT[] /* array of pointers */
int size;
} ownTypeVector;
void doSthing(ownTypeVector* oTV);
void doAnotherThing(ownTypeVector* oTV);
void doSthing(ownTypeVector* oTV)
{
...
doAnotherThing(oTV);
...
}
Thanks for your comments, I collected all the code that contains control logic and data structures so that it compiles. It runs on in an embedded systems, that can receive characters from multiple sources, builds strings from it by given rules and after the strings are ready, calls a function that needs that string. This can also be a list of functions. This is why I have function pointers - I can use the same logic for a bunch of things simply by choosing functions outside the 'activityFromCharacters' function.
Here I build a data structre with them by adding A-s, B-s and C-s to the AVector.
Of course every one of these separate sources has their own static strings so that they do not bother each other.
The problem again in the more detailed version of the code:
'aV->size' has got a proper value everywhere, except 'handleCaGivenWay.' Before it gets calles, 'aV->size' is ok, in 'addA' 'aV->size' is ok, too. After leaving 'handleCaGivenWay' it is ok again.
#define NUMBER_OF_AS 1
#define NUMBER_OF_BS 5
#define NUMBER_OF_CS 10
typedef struct{
char name[81];
} C;
typedef struct{
C *c[NUMBER_OF_CS]; /* array of pointers */
int size;
int index;
} B;
typedef struct{
B *b[NUMBER_OF_BS]; /* array of pointers */
char name[81];
int size;
} A;
typedef struct{
A *a[NUMBER_OF_AS]; /* array of pointers */
int size;
} AVector;
typedef struct {
char *string1;
char *string2;
} stringBundle;
typedef struct{
void (*getCharacter)(char *buffer);
void (*doSthingwithC)(stringBundle* strings,AVector* aV);
AVector* aV;
} functionBundle;
void getCharFromaGivenPort(char *buffer)
{
//...
}
void addA(AVector * aV, stringBundle* strings)
{
aV->a[aV->size]->size = 0;
++aV->size;
int i = 0;
if(strlen(strings->string2) < 81)
{
for(i;i<81;++i)
{
aV->a[aV->size-1]->name[i] = strings->string2[i];
}
}
else {report("Too long name for A:");
report(strings->string2);}
}
void handleCaGivenWay(stringBundle* strings,AVector* aV)
{
A* a;
a = NULL;
if(aV->size) { a = aV->a[aV->size-1]; }
switch(1)
{
case 1: addA(aV,strings); break;
case 2: //addB()...
default: if (a && aV->size)
{ //addC(a->thr[a->size-1],c);
}
else report("A or B or C invalid");
break;
}
//handleCaGivenWay
}
void activityFromCharacters(stringBundle* strings,functionBundle* funcbundle)
{
/* some logic making strings from characters by */
/* looking at certain tokens */
(* funcbundle->doSthingwithC)(strings,funcbundle->aV);
}
//activityFromCharacters
AVector* initializeAVector(void)
{
AVector* aV;
if (NULL == (aV = calloc(1,sizeof(AVector))))
{ report("Cannot allocate memory for aVector."); }
int i = 0;
int j = 0;
int k = 0;
for(i; i < NUMBER_OF_AS; ++i)
{
if (NULL == (aV->a[i] = calloc(1,sizeof(A))))
{ report("Cannot allocate memory for As."); }
aV->a[i]->size = 0;
aV->a[i]->name[0] = 0;
for(j; j < NUMBER_OF_BS; ++j)
{
if (NULL == (aV->a[i]->b[j] = calloc(1,sizeof(B))))
{ report("Cannot allocate memory for Bs."); }
aV->a[i]->b[j]->size = 0;
for(k; k < NUMBER_OF_CS; ++k)
{
if (NULL == (aV->a[i]->b[j]->c[k] = calloc(1,sizeof(C))))
{ report("Cannot allocate memory for Cs."); }
}
}
}
aV->size = 0;
return aV;
//initializeProgramVector
}
int main (void)
{
AVector* aV;
aV = initializeAVector();
while(1)
{
static stringBundle string;
static char str1[81];
static char str2[81];
string.string1 = str1;
string.string2 = str2;
functionBundle funcbundle;
funcbundle.getCharacter = &getCharFromaGivenPort;
funcbundle.doSthingwithC = &handleCaGivenWay;
funcbundle.aV = aV;
activityFromCharacters(&string,&funcbundle);
}
//main
}
your code shows that it hasn't any error...
But i think you are doing mistake in getting the value of size in doSthing function.
you are printing there its address. so concentrate on some pointer stuff..
Try printing the oTV->size just before the call and as the first statement in doSthing function. If you get the correct value in both print, then the problem is with the function doSthing. Problem could be better understood if you've shown the code that calls doSthing.
Searched a long time to find this. I found 2 problems, but dont know what exactly you are trying to accomplish so i cannot tell for certain that the fix'es i propose are what you intend.
typedef struct{
A *a[NUMBER_OF_AS]; /* array of pointers */
int size;
} AVector;
// and in addA():
aV->a[aV->size]->size = 0;
First: You are inlining the array of pointers in the struct. What i think what you want and need is a pointer to a pointer array so that it can grow which is what you want in addA() i think. The line from addA() aV->a[aV->size]->size = 0; does not communicate your intention very well but it looks like you are trying to change the value beyond the last entry in the array and since it is inlined in the struct it would result to the separate field size by pure coincidence on some alignments; this is a very fragile way of programming. So what i propose is this. Change the struct to contain A** a; // pointer to pointer-array, malloc it initially and re-malloc (and copy) it whenever you need it to grow (in addA()).
I have a C (not C++) struct that goes like this
typedef struct mystruct{
float a,b;
int x, y;
} mystruct;
Then in a function I collect data like this:
mystruct List[MAX];
ListNumber = 0;
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
List[ListNumber].a = masterlist[i].a;
...etc
ListNumber++;
}
}
then I send the array to a function
DoStuff(static int max, mystruct array[max]){
Stuff
}
This works, but when I try to do it like this....
mystruct setter(int i)
{
mystruct TEMP;
TEMP.a = masterlist[i].a;
//......etc
return TEMP;
}
mystruct List[MAX];
ListNumber = 0;
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
List[ListNumber] = setter(i);
ListNumber++;
}
}
It causes a lot of funky errors. Why is this happening?
edit:
#tommieb75 I can't give much detail, the results do not seem to have a pattern. The list is used as a generalized way to draw stuff to the screen, and having the function instead of the direct setting makes odd problems in rendering -and random-, but produce no compiler errors at all. gdb shows some integers as being larger than an integer, that's the only pattern I find.
masterlist is a global array of another struct. The data needs to be converted to the struct in this example.
No compiler warnings or errors at all. I can turn in more sensitive warnings maybe, but I always get reported of any general error I can think.
I am going to try the selected solution, that should suffice. Anyway similar functions returning structs are used in my code and all work perfectly except for this case with an array of structs.
For a simple setting a struct member you need a copy from an entire struct-element?
mystruct List[MAX];
ListNumber = 0;
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
List[ListNumber].a = masterlist[i].a;
ListNumber++;
}
}
If you really need a function, use the destination-memory as parameter like:
void setter(mystruct *dest,const mystruct *src)
{
dest->a = src->a;
}
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
setter( &List[ListNumber], &masterlist[i] );
ListNumber++;
}
}
what is
mystruct setter(i)
{
mystruct TEMP;
TEMP.a = masterlist[i].a;
'i' has any type?
//If you get errors with uninitialized members in struct that could help
http://ideone.com/WRLVG
The first problem is your definition of setter is not a legal function signature. The parameter i must be given a type
mystruct setter(int i) {
...
}
It also uses the variable masterlist which is not defined in the function. This may be legally declared elsewhere as a static. If not though it will need to be accessible to the function in some way
The problem is that within the setter function you have a stack allocated variable TEMP which goes out of scope once the function returns... you might be better to allocate the pointer to my_struct on the heap and return the address of it back out to the calling routine...
Edit:
mystruct *setter(int i){
mystruct *ptr_myStruct;
ptr_myStruct = malloc(sizeof(mystruct));
if (ptr_myStruct != NULL){
ptr_myStruct->a = masterlist[i].a
// etc...
return &ptr_myStruct;
}
return NULL;
}
mystruct List[MAX];
ListNumber = 0;
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
List[ListNumber] = setter(i);
ListNumber++;
}
}
That is what is needed to get the values back out once the routine goes out of scope. That is called return-by-reference