C question: pointers

General software, Operating Systems, and Programming discussion.
Everything from software questions, OSes, simple HTML to scripting languages, Perl, PHP, Python, MySQL, VB, C++ etc.
Post Reply
trebond

C question: pointers

Post by trebond »

hi,

i'm pretty new to C programming, but i'm trying to learn fast for work. any help would be greatly, greatly appreciated :)

i'm trying to implement a function with a lot of polynomial arithmetic. i'm representing each polynomial as an array of floats-- the floats are the polynomial's coefficients for each term and the array index is the degree of that term. ex: 1.2 x^3 + 3.4 x + 5 is: float poly[4] = {5, 3.4, 0, 1.2}.

In the main program, I'd like to just be able to use float pointers to point to the coefficient arrays. In fact, it'd be simplest for me to allocate memory for each polynomial (coefficient array) inside a polynomial initialization function and not have to worry about that in the main program, either. (I'll leave out my reasons for this for the sake of brevity). Here's an example test program of what I'm doing:

// test code for polyInit
// polyTest.c: output printed to c :( localfolder)/polyTest.txt
#include <stdio.h>

// local function declaration
void polyInit(float *poly_ptr, int n);

main() {

FILE *output;
int i = 0;
int poly_order = 8;
float *poly; // pointer to polynomial coefficients

output = fopen("c :p olyTest.txt","w");

polyInit(poly, poly_order);

fprintf(output,"initial value pointed to by poly pointer: %f\n", *poly);

fprintf(output,"initial values for polynomial coefficients are: \n");
for(i=poly_order; i>=0; i--) {
fprintf(output, "%f x^%i ", *(poly+i), i);
if(i!=0) fprintf(output,"+ ");
}

}

// allocate memory for an array of floats of length n+1 and point poly_ptr at
// this array
void polyInit(float *poly_ptr, int n) {

int i = 0;
float coef[n+1];

for(i=0; i<=n; i++)
coef = 0;

poly_ptr = coef;

return;
}

Unfortunately, there must be some flaw to my logic. This program will compile, but won't run unless I comment out both fprintf's. So obviously the float pointer is not being "initialized", because any reference to what the pointer is pointing at is causing an error. Any thoughts on what I'm doing wrong and/or what I should do differently?

thanks!
Luan
User avatar
Paft
SG Elite
Posts: 5785
Joined: Tue Feb 20, 2001 12:00 am
Location: Richmond VA

Post by Paft »

Uh, you do realize that coef[n+1] goes out of scope at the end of polyInit(), right? You're pointing to a garbage address with *poly.
So trade that typical for something colorful, and if it's crazy live a little crazy!
trebond

oh-- now i know

Post by trebond »

thanks, I didn't know that!!

I was going to ask another question, but I guess I ought to go read up on runtime memory allocation first. maybe I can figure this out myself.

again, thanks
Chandra Prakash

Chandra

Post by Chandra Prakash »

There are few bugs in ur problem.I'm listing them one by one.See if u gain anything from it.
1) I don't know how ur program compiler but the line
float coef[n+1]; gives compiler error since a constant value is required as array size.This is compile time information & the compiler need to have a value ather then a variable.
2) Never allocate a local array & pass its base address outside the scope.When function ends the local array "coef " gets out of scope & the memory is released,so u know what u will get outside this function :thumb:
3) If at all u want memory for poly to be allocated inside polyInit,either pass the address of this pointer or pass the pointer as reference.Dyanamically allocate memory for poly inside the function & make arrangements for deallocating the memory when ue purpose is fulfilled.
User avatar
Paft
SG Elite
Posts: 5785
Joined: Tue Feb 20, 2001 12:00 am
Location: Richmond VA

Post by Paft »

Chandra Prakash wrote:1) I don't know how ur program compiler but the line
float coef[n+1]]

That's utter crap, and wrong. The compiler handles this. Here's code that works fine:

Code: Select all

#include <iostream>

int main()
{
    char array[3] = "Hi.";

    for (int x = 0; x < 3; x++)
    {
        std::cout << array[x];
    }

    return(0);
}
ALSO, if it was compile-time information, how the f(x) would the 'new' operator work?
Chandra Prakash wrote:2) Never allocate a local array & pass its base address outside the scope.When function ends the local array "coef " gets out of scope & the memory is released,so u know what u will get outside this function :thumb:
The only way that this is acceptable is if you're calling a function from within a function, and passing the array that way. Otherwise Chandra is correct.
Chandra Prakash wrote:3) If at all u want memory for poly to be allocated inside polyInit,either pass the address of this pointer or pass the pointer as reference.Dyanamically allocate memory for poly inside the function & make arrangements for deallocating the memory when ue purpose is fulfilled.
malloc() and free() are good ways of doing this.
So trade that typical for something colorful, and if it's crazy live a little crazy!
User avatar
Firestorm ZERO
Member
Posts: 59
Joined: Sat Nov 25, 2000 12:00 am
Location: Toronto, Ontario CANADA

Post by Firestorm ZERO »

[quote="Paft"]That's utter crap, and wrong. The compiler handles this. Here's code that works fine:

Code: Select all

#include <iostream>

int main()
{
    char array[3] = "Hi."];
    }

    return(0);
}
[/QUOTE=Paft]

I think what Chandra meant is. Yes you can use array with a variable for the index in loops and such. But you can't DECLARE an array with a variable.

This is because in C, the memory allocated for an array is put in sequencal (sp?) order. So the compiler must know how much memory to allocate for the array before it can declare it. So...
float coef[5] is ok but
float coef[x] is not

To go around that you have to dynamically allocate the memory. Which Chandra pointed out in (3)
User avatar
Paft
SG Elite
Posts: 5785
Joined: Tue Feb 20, 2001 12:00 am
Location: Richmond VA

Post by Paft »

Ohhh, I get what you're saying. And that's correct. To dynamically allocate the memory, you can look into malloc() and free() (I believe it's free()); or new and delete.

Sorry. :)
So trade that typical for something colorful, and if it's crazy live a little crazy!
Post Reply