0
// I need to download data from the (json-format) file net_f:
std::ifstream net_f("filename", std::ios::in | std::ios::binary);
// to a square int array *net of size n:
int n;
int * net;
load_net(net_f, &n, net);

// The size is initially unknown, so I want to do it in the procedure:
void load_net(std::ifstream& f, int *n, int *net)
{
    int size; // # of rows (or columns, it's square) in the array
    int net_size; // the array size in bytes
    /*
        some code here to process data from file
    */
    // Returning values:
    *n = size;
    // Only now I am able to allocate memory:
    *net = (int *)malloc(net_size);
    /*
        and do more code to set values
    */
}

Now: the compiler warns me that 'variable "net" is used before its value is set'. Indeed, it is, since I don't have enough information. It also pops-up during the runtime, and I just ignore it. How should I rework my code to make it more elegant? (BTW it has to be an array, not a vector; I'm copying it then to a CUDA device).

dandan78
  • 13,328
  • 13
  • 64
  • 78
tomekd
  • 23
  • 1
  • 4
  • What is this supposed to mean: `*net = (int *)malloc(net_size);` given that the RHS expression is cast to `int*` and the LHS expression is `int`? – juanchopanza Feb 24 '15 at 07:56
  • http://stackoverflow.com/questions/2838038/c-programming-malloc-inside-another-function – jamesdlin Feb 24 '15 at 08:41

2 Answers2

4

Since you're trying to modify net in the called function, you need to pass net by reference (since you're using C++). Also, this would be preferred for n as well:

void load_net(std::ifstream& f, int &n, int *&net)
{
    // ...

    /* Set output args */
    n = size;
    net = (int*)malloc(net_size);
}

The C way would be to pass a double pointer (and not cast the result of malloc!):

void load_net(FILE* f, int *n, int **net)
{
    // ...

    /* Set output args */
    *n = size;
    *net = malloc(net_size);
}

You seem to be writing a mix of C and C++ code. Don't do this. Pick one, and use its features as they're intended.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • OP is actually trying to modify the thing pointed at by `net` (and `n` too). And since it hasn't been initialized... – juanchopanza Feb 24 '15 at 07:58
  • @juanchopanza That's what his *code* is doing, but I'm fairly confident that's not what he intends. It doesn't make sense any other way. – Jonathon Reinhart Feb 24 '15 at 07:59
  • @juanchopanza Look at his code. He's `malloc`ing a buffer, and trying to assign it to `net`, which is an `int*` in the calling function. Clearly he wants to assign that buffer to that pointer, he just isn't using enough indirection. – Jonathon Reinhart Feb 24 '15 at 08:01
  • @JonathonReinhart, you get it right. I'm coding by use of ready examples, and clearly I need to think it over yet. – tomekd Feb 24 '15 at 17:41
  • @JonathonReinhart, thank you for help. This was exactly what I needed. Now the code is running perfectly well. :) Credits go to you! – tomekd Feb 25 '15 at 16:17
0

you can use double pointer in function argument and pass pointer address in function

// I need to download data from the (json-format) file net_f:
std::ifstream net_f("filename", std::ios::in | std::ios::binary);
// to a square int array *net of size n:
int n;
int *net;
load_net(net_f, &n, &net);

// The size is initially unknown, so I want to do it in the procedure:
void load_net(std::ifstream& f, int *n, int **net)
{
    int size; // # of rows (or columns, it's square) in the array
    int net_size; // the array size in bytes
    /*
        some code here to process data from file
    */
    // Returning values:
    *n = size;
    // Only now I am able to allocate memory:
    **net = (int *)malloc(net_size);
    /*
        and do more code to set values
    */
}
user1627167
  • 339
  • 1
  • 8