0

I use the code below to set the value of a float array

#include "math.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
class MathCore
{
public:
    MathCore();
    virtual ~MathCore( );                   
    bool dosomething ( );
};

and

#include "MathCore.h"
MathCore::MathCore()
{

}

MathCore::~ MathCore()
{

}

bool MathCore::doSomething ( )
{

   //-------------------------------------------------
   float *xArray;
   xArray=(float*)malloc(sizeof(float)*5);
    float v=0.1;
    xArray[0]=v;

    return 1;
}

it always reports EXC_BAD_ACCESS error at

xArray[0]=v;

your comment welcome

arachide
  • 8,006
  • 18
  • 71
  • 134
  • 5
    First, pick a language. I assume this is C. That said, scroll to the top of your source file... now put `#include ` Finally, [lose the cast on your `malloc()` call.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) If you read through that linked question and answers you'll see why. – WhozCraig Nov 11 '13 at 10:41
  • 1
    You want c-code? Or c++-code? Or c++-code to access a c-library-interface? – Henno Nov 11 '13 at 10:41
  • I am familiar with c, but this is one part of c++ code, I think it should be c++-code to access a c-library-interface – arachide Nov 11 '13 at 10:43
  • Then you should use new instead – fkl Nov 11 '13 at 10:44
  • Did malloc return successfully? – chwarr Nov 11 '13 at 10:44
  • in my machine this code is working !!!!! – Debobroto Das Nov 11 '13 at 10:44
  • it works in my machine as I expect it to! – ZoomIn Nov 11 '13 at 10:45
  • it is on mac osx/xcode+ios, malloc returns 0x000000 – arachide Nov 11 '13 at 10:46
  • Then post a single, *entire* source, including `#include` directives and a `main()` that stand-alone exhibits this issue, because [it runs here, as expected with C++, if setup correctly](http://ideone.com/W8KVMs). Or perhaps a [C version setup as-described in the first comment](http://ideone.com/hcNMub). Either way, post something that reproduces the issue please. – WhozCraig Nov 11 '13 at 10:48
  • So you're saying given this source, creating a `main()` that has one object, an instance of `MathCore obj;` followed by `obj.doSomething();` and `return 0;` faults on a null pointer access ? – WhozCraig Nov 11 '13 at 10:55
  • I call this cpp code from javascript(due to low performance of intensive caculation on spidermonkey), of course I have established the javascript binding and transferred the data without problem – arachide Nov 11 '13 at 10:58
  • This isn't even compliant code. `MathCore` declares a function `dosomething()` in the header, and `doSomething()` in the source file. Fixing that, as expected, [this still works correctly](http://ideone.com/ihDUk3). Your problem is somewhere else, either in this code, but something you're not showing, or something entirely unrelated that is corrupting your heap. I see nothing wrong with this (apart from the use of `malloc()`, which has no business in a C++ program, but thats not entirely related unless somewhere else you're doing the same, but `delete`ing, or `new`'ing and `free()`ing, etc). – WhozCraig Nov 11 '13 at 11:01

2 Answers2

1

Assuming you want a size 5 float array-like object with the first element set to 0.1:

std::vector<float> xArray(5); // contains 5 floats, each set to 0.0f
xArray[0] = 0.1;

If you really must use a raw pointer to a dynamically allocated array, then,

float* xArray = new float[5];   // uninitialized elements.
float* xArray = new float[5](); // elements initialized to 0.0f.
xArray[0] = 0.1;

A better alternative to the raw pointer could be

std::unique_ptr<float[]> xArray(new float[5]);
xArray[0] = 0.1;
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

You should really use C++ means

std::vector<float> xArray;
xArray.push_back(0.1);

And now, when you need to call a C-library-function with that vector, the c-library usually takes a float* and a size. You get this from you vector like this:

cfunc(&xArray[0], xArray.size());

You take the address of the first element of xArray (all elements have to be in a continuous block by the standard) and size() returns the number of elements in xArray.

Henno
  • 397
  • 1
  • 7