Thread: Would someone mind showing me how to do this dynamically instead?

  1. #1
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11

    Would someone mind showing me how to do this dynamically instead?

    Thank you, in advance, for your time. I have written this bit of code for the Fibonacci sequence.
    Code:
      9         int size=0;
     10         int fib[DWORD];
     11 
     12         printf("Enter size: ");
     13         scanf("%i", &size);
     14         printHistogram(fib, size);
     15         return 0;
     16 }
     17 void generateFibonacci(int array[],int size)
     18 
     19   {
     20           int r, i;
     21 
     22                 for(i=1;i<=size;i++)
     23                 {
     24                   asm volatile("mov $1, %%eax;"
     25                                "mov $0, %%edx;"
     26                                "1:add %%eax, %%edx;"
     27                                "xchg %%eax, %%edx;"
     28                                "loop 1b":"=d"(r):"c"(i):"%eax");
     29                                 array[i]=r;
     30                 }
     31   }
     32 
     33 void printHistogram(int array[], int size)
     34 {
     35         int c,u;
     36         generateFibonacci(array, size);
     37         for(c=1;c<size;c++)
     38         {
     39                 for(u=1;u<array[c];u++)
     40                 {
     41                         printf("*");
     42                 }
     43                 printf("\n");
     44         }
     45 }
    I have tried quite few things and I am trying to change the prototype for generateFibonacci to... int* generateFibonacci(size). I have been trying to use calloc inside generateFibonacci and then declare a pointer in main that points at the address of the pointer that generateFibonacci returns. Would someone mind showing me how to do so? Thanks. -court

  2. #2
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11
    For example, I have tried the following. But, it produces a seg fault.

    Code:
      6 #include<stdio.h>
      7 #include<stdlib.h>
      8 //#define DWORD 64
      9 
     10 int* generateFibonacci(int size);
     11 void printHistogram(int array[], int size);
     12 int main(void)
     13 {
     14         int size=0;
     15         int *fib={0};
     16 
     17         printf("Enter size: ");
     18         scanf("%i", &size);
     19         printHistogram(fib, size);
     20         free(fib);
     21         return 0;
     22 }
     23 int* generateFibonacci(int size)
     24 {
     25         int r, i;
     26         int* array = calloc(size, sizeof(int));
     27         for(i=1;i<=size;i++)
     28         {
     29                 asm volatile("mov $1, %%eax;"
     30                              "mov $0, %%edx;"
     31                              "1:add %%eax, %%edx;"
     32                              "xchg %%eax, %%edx;"
     33                              "loop 1b":"=d"(r):"c"(i):"%eax");
     34                               array[i]=r;
     35                 } return array;
     36   }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    int *fib={0};
    If you want to initialise a pointer to be a null pointer, write:
    Code:
    int *fib = NULL;
    Next, printHistogram should just print a histogram. It should not be generating a Fibonacci sequence, unless that is somehow part of the process of printing a histogram. Either way, the call to generateFibonacci in printHistogram must change since you changed generateFibonacci's signature and return type.

    I suggest that you do not call assembly language code at the moment. In all likelihood even your loop is wrong, i.e.,
    Code:
    for(i=1;i<=size;i++)
    should start from i = 0 to i < size instead since you access array[i]. If you cannot even get that right, you're in no position to try any kind of special optimisation with inline assembly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11
    The code in the first post runs fine and since generateFibonacci is really returning a pointer anyway my question is in regard to syntax. What I am asking is whether there is a way for me to dynamically allocate the memory, leaving the remainder of the code unchanged, such that the prototype for generateFibonacci is the following int* generateFibonacci(size)?
    Last edited by court; 04-12-2014 at 09:28 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by court
    The code in the first post runs fine and since generateFibonacci is really returning a pointer anyway my question is in regard to syntax.
    In your first post, generateFibonacci is declared with void return type, so no, generateFibonacci is not returning a pointer. In your second post generateFibonacci is indeed returning a pointer, but if you have not changed printHistogram to incorporate that fact, then you have a memory leak. Worse still, your modified code should not even compile since generateFibonacci is called with two arguments (correct for post #1) whereas it is declared as taking just one argument (in post #2).

    Furthermore, just because it "runs fine" does not mean that it is correct. Unless I see evidence to the contrary, I am certain that the bounds of your for loop in generateFibonacci is wrong in both posts #1 and #2.

    Quote Originally Posted by court
    What I am asking is whether there is a way for me to dynamically allocate the memory, leaving the remainder of the code unchanged, such that the prototype for generateFibonacci is the following int* generateFibonacci(size)?
    Impossible. You have to change printHistogram.

    What you could do is to have another function that generateFibonacci (with its original signature and return type) calls, but the problem is that you also have to free the memory in that other function, hence you cannot use it in printHistogram.

    That said, you are passing the user supplied size to printHistogram. Therefore, if you want to dynamically allocate memory, do so in printHistogram and then use the original version of generateFibonacci.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11
    You are correct, I forgot to modify the declaration upon changing the code. However, the setting of i to zero in the for loop (per one of your copious suggestions) would simply cause the assembly loop to spin before finally returning the sequence with garbage in front of it would be my guess.

  7. #7
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11
    That worked. Thank you very much for your painful, albeit effective, assistance.

  8. #8
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11
    I'm not entirely sure, however, on which planet "runs fine" is synonymous with memory leakage.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by court
    However, the setting of i to zero in the for loop (per one of your copious suggestions) would simply cause the assembly loop to spin before finally returning the sequence with garbage in front of it would be my guess.
    I have not touched assembly language since a class early on in university, so I am in no position to judge the correctness of your inline assembly. What I can say though is that based on how you use size for your calloc call, you expect size to be the number of elements in the array, upon which array[i] must be wrong when i == size, which can happen when your loop condition is i <= size. So, if array[i], starting from 1 and i <= size is correct, then your array is at least one element short.

    Quote Originally Posted by court
    I'm not entirely sure, however, on which planet "runs fine" is synonymous with memory leakage.
    What do you mean?
    Last edited by laserlight; 04-12-2014 at 09:58 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11
    I was just being humorous. Thanks a lot for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Master mind
    By KaiZ023 in forum C Programming
    Replies: 3
    Last Post: 11-15-2010, 08:47 AM
  2. Just something on my mind
    By face_master in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-04-2002, 05:54 AM
  3. Where is my mind?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-11-2002, 02:28 PM
  4. kid without his mind
    By iain in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-07-2002, 09:25 AM

Tags for this Thread