Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Problem with a function

Name: Anonymous 2010-01-17 12:35



    I'm a newfag to programming in C, and I'm having trouble with a function that I've written for a program that does operations with "big integers". Basically, my function runs once, perfectly, but on the second iteration, it crashes when it goes to allocate memory to the temporary structure.

    I've tried a few things, but this one mostly has me stumped. Any help would be greatly appreciated, you don't have to tell me what to do but just a nudge in the right direction would be sufficient.

    This is my function, complete with my debugging lines so you can get a better idea of what I've been doing:

   
    struct integer* read_integer(char* stringInt){

       int length = strlen(stringInt);
      
       //COMMENCING DEBUGGING
       printf("FUNCTION START\n");
       printf("LENGTH VALUE = %d\n", length);
      
       struct integer* temp = (struct integer*)malloc(sizeof(struct integer)*length);
        
       temp->size = length;
      
       //DEBUGGING
       printf("The SIZE value of the structure is currently: %d\n", temp->size);
      
       //DEBUGGING
       printf("stringInt currently contains all of the following:\n");
       for(i=0;i<length;i++){
       printf("%d: %c\n", i, stringInt[i]);
       }
      
       int i = 0;
       int j = length;
       while(stringInt[i]!='\0'){
       temp->digits[j-1]=(int)stringInt[i];
       temp->digits[j-1]-=48;
       i++;
       j--;
       }
      
       //MORE DEBUGGING
       printf("The ARRAY within the STRUCT has the following VALUES:\n");
       for(i=0;i<length;i++){
       printf("%d: %d\n", i, *temp->digits);
       }
       //After DEREFERENCING the struct above by adding an asterisk, it displayed correct values. Function works so far.
      
       //DEBUGGING
       printf("FUNCTION COMPLETE\n\n\n");
      
       return temp;
      
       }

Name: Anonymous 2010-01-17 12:45

printf("%d: %d\n", i, *temp->digits);
Wouldn't this line just print out the same second number each time?

Also, telling us the parameters your passing into the function would probably help.

Name: Anonymous 2010-01-17 12:52

DON'T HELP HIM!!!

Name: Anonymous 2010-01-17 13:01

Why don't you make 10 threads about it?

Name: Anonymous 2010-01-17 13:29

>>3
Why not?

>>2
You're right, the reason I didn't catch that is because my test case was all the same number.

In this function, stringInt consists of 10 numbers as a string. The first case is 8888888888, the second one is 2222222222.

And yes, I made a thread earlier about a somewhat related problem, but I didn't think /prog/ was having bandwidth issues considering most of the threads here are only tangentially related to programming.

Name: Anonymous 2010-01-17 13:33

Oh, and the structure:


struct integer{
       int* digits;
       int size;
       };

Name: Anonymous 2010-01-17 13:41

>>5
Why not?
LURK MORE FAGGET THIS ISN'T PROGGIT

Name: Anonymous 2010-01-17 13:45

>>5
Up at the top you are mallocing an array of struct integers, but later on you are only using one struct integer and treating the pointer to an int digits as an array, but in reality you have only malloced a pointer to a single integer.

Try mallocing the struct first, then the integer array second and assigning the struct_integer->digits to that array.

Name: Anonymous 2010-01-17 13:46

malloced a pointer to a single integer
should read "malloced a pointer to a single int"

These are the confusions that occur when you use very similar names.

Name: Anonymous 2010-01-17 16:28

//COMMENCING
*snicker*

Name: Anonymous 2010-01-17 17:27

>>10
COMMENCE MY ANUS

Name: Anonymous 2010-01-17 19:45

the comments and indentation make my eyes bleed

Name: Anonymous 2010-01-18 12:59

Where does it actually crash?
Also, think about what you're doing in
struct integer* temp = (struct integer*)malloc(sizeof(struct integer)*length);

Name: Anonymous 2010-01-18 15:58

>>13
think about you're doing
YOU ARE NOW DOING MANUALLY

Name: Anonymous 2010-01-18 16:19

DOING MY ANUS

Name: Anonymous 2010-01-18 16:51

This isn't C. This is C++ without class, I mean classes.
In C, we declare all of out variables before there is any other code, and variables are initialized to constant values only.

Name: Anonymous 2010-01-18 18:59

>>16
Hasn't been true for 20 years...

Name: Anonymous 2010-01-19 3:24

>>17
20 years...
:(

Name: Anonymous 2010-01-19 15:40

>>17
Show me your magical C90 standard. It's still compulsory in C89.

(If you meant ten years, nobody uses C99 because for the most part, it's just an attempt at legitimising bad habits that some influential compilers supported.)

Name: Anonymous 2010-01-19 15:48

>>19
Nobody uses any C standard.  They use C compilers, which have supported it forever.

Name: Anonymous 2010-01-20 16:26

>>20
This is what PHP users really believe.

Name: Anonymous 2010-12-22 1:52

Name: Anonymous 2010-12-23 4:30

Don't change these.
Name: Email:
Entire Thread Thread List