0

I am a getting a segment fault when trying to input data for my array of pointers. Im pretty new to coding so any help would be great. My task was to make an array of pointers then display, swap them around and than sort them

#include <iostream>
using namespace std;

float getValueFromPointer(float* thePointer)
{
   return *thePointer;
}

float* getMinValue(float* a, float* b)
{
   if (*a < *b)
   {
      return a;
   }
   else
   {
      return b;
   }
}

int main()
{
   int arraySize;
   cout << "Enter the array size: ";
   cin >> arraySize;

   float** speed = new float*[arraySize]; // dynamically allocated array

   for(int i = 0; i < arraySize; i++)
   {
      cout << "Enter a float value: ";
      cin >> *speed[i];
   }

    // Core Requirement 2
   for (int i = 0; i < arraySize; i++)
   {
      float value = getValueFromPointer(*speed+i);
      cout << "The value of the element " << i << " is: ";
      cout << value << endl;
   }



   //float *pointerToMin = getMinValue(&speed[0], &speed[arraySize - 1]);
   //cout << *pointerToMin << endl;

   delete [] speed;
   speed = NULL;
   return 0;
}
1
  • Why are using pointers and new? You should prefer using std::vector<float>. You don't need pointers at all; use references. Commented Jun 7, 2018 at 3:53

2 Answers 2

4

You’ve only allocated space for the outer array but you need to also allocate space for each of the internal floats.

So before calling this line:

cin >> *speed[i];

You need to first allocate space for it:

speed[i] = new float;
Sign up to request clarification or add additional context in comments.

5 Comments

Why? Why not just float* speed = new float[arraySize];
He could. But he specifically said the assignment was to create an array of pointers.
On re-read, you are correct. The assignment is insane, but you are correct.
They could at least make the assignment an array of struct pointers so that dynamically allocating the memory would make...a little sense. I guess that significantly complicates the sorting exercise, though.
Thanks for the help I greatly appreciate it!
1

Your problem is that you've allocated an array of float pointers. You need to allocate an array of floats. So currently you have no memory allocated for the actual floats. If you do this, you'll allocate that memory:

float *speed = new float[arraySize];

You don't have any need for a 2D/jagged array that I can see. If you start with the code above, the compiler errors should lead you right in the direction. (Basically you will start removing * from a lot of places in your code.)

EDIT Based on your requirement that I misunderstood, a possible approach is the following. The other answer (the one that isn't mine) makes sense in broader scenarios than this, but hopefully this is sort of another angle to think about that rather arbitrary problem you're trying to solve:

int main()
{
  float *pFloats = new float[10];
  float **ppFloats = new float*[10];
  //assign float values and pointers to them in same loop
  for (int i = 0; i < 10; i++)
  {
    pFloats[i] = i;
    ppFloats[i] = &pFloats[i];
  }
  //swap two arbitrary pointers
  float *pTemp = ppFloats[4];
  ppFloats[4] = ppFloats[5];
  ppFloats[5] = pTemp;

  //print our float array
  for (int i = 0; i < 10; i++)
    printf("%f\n", pFloats[i]);
  //print our float array *through* our pointers
  for (int i = 0; i < 10; i++)
    printf("%f\n", *ppFloats[i]);
  delete[] ppFloats;
  delete[] pFloats;
}

Ignore the hard-coded constants, etc...The point here is that I've created a contiguous memory area for the floats, and then created an array of float pointers on top of it. Note that I can sort my pointer array with zero impact on the original array. There are much smarter ways to do this, but...looks like you're learning about raw pointers, so...

Contrast with the other answer which creates jagged memory for the floats (1 at a time, on demand, not necessarily contiguous).

5 Comments

@KillzoneKid - Yeah, I see that now. I'm gonna hope he misunderstood that requirement, but will kill this answer if he confirms not.
Yea my assignment was to make an array of pointers... Before that I had it like your code and it worked great but the "stretch challenges" were to make an array of pointers. Thanks though!
@Jacobi97 - updated with another way you could allocate the memory for your actual floats.
@zzxyz Awesome thanks for all the help. Programming is tough! but I'm glad for people like you guys!
@Jacobi97 - Absolutely. PS--Your professor is probably looking for the other approach (on-demand), but neither approach particularly makes sense for this problem and slapping "a layer of indirection" on top of an existing data construct (an array of floats in this case) is hardly unheard of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.