Could someone please give me some advice as to why, when I run my program I get a segmentation fault?
The code is posted below:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

typedef float Elem;

struct Vector
{
    unsigned short int size;
    Elem* array;
};


// Instruct user on how to use this program; i.e. the commands it
// supports and the syntax of those commands.
// In: 
//    none
// Out:
//    output produced on standard output
void usage(void) 
{
    puts( " Usage:" );
    puts( "  p   - print vector" );
    puts( "  q,e - quit, end" );
    puts( "  h   - print usage help" );
    puts( "  + <operand> - add <operand> to each element of vector" );
    puts( "  - <operand> - subtract <operand> from each element of vector" );
    puts( "  * <operand> - multiple each element of vector by <operand>" );
    puts( "  / <operand> - divide each element of vector by <operand>" );
    puts( "  a <value> - extend vector by additional value" );
}


// Allocates an empty (zero length vector)
// In: 
//    none
// Out:
//    return -- a pointer to an empty array
Vector *alloc_vec(void)
{
    Vector *vector_ptr;
    vector_ptr->size = 0;
    vector_ptr->array = NULL;
    vector_ptr->array = new Elem[vector_ptr->size];  

    return vector_ptr;
}

// Print the Vector (print each element of the vector)
// In: 
//    Vector * (pointer to a vector)
// Out:
//    output produced on standard output
void print_vec(Vector * vector_ptr)
{
    for (int i=0; i < vector_ptr->size; i++)
    {
        cout << vector_ptr->array[i];
        cout << " ";
    }

    cout << "\n";
}

// deallocate the Vector (delete the Vector)
// In: 
//    Vector * (pointer to a vector)
// Out:
//    none
void dealloc_vec(Vector * vector_ptr)
{
    delete [] vector_ptr;  // When done, free memory pointed to by vector_ptr
    vector_ptr = NULL;
}


// Extend the Vector (append a new element to the vector)
// In: 
//      Vector * (pointer to a vector)
//      Elem (element)
// Out:
//    return -- pointer to a new vector with the element appened to it
Vector *extend_vec(Vector * vector_ptr, Elem element)
{
    Vector *new_vector_ptr;
    new_vector_ptr->size = vector_ptr->size + 1;
    new_vector_ptr->array = NULL;
    new_vector_ptr->array = new float[new_vector_ptr->size];  

    for (int i=0; i < vector_ptr->size; i++)
    {
        new_vector_ptr->array[i] = vector_ptr->array[i];
    }

    new_vector_ptr->array[new_vector_ptr->size - 1] = element;
    dealloc_vec(vector_ptr);

    return new_vector_ptr;
}


// add a scalar to the Vector (add the scalar to each element of the vector)
// In: 
//      Vector * (pointer to a vector)
//      Elem (scalar)
// Out:
//    return -- pointer to a new vector with the scalar added to it
Vector *scalar_plus(Vector * vector_ptr, Elem scalar)
{
    for (int i=0; i < vector_ptr->size; i++)
    {
        vector_ptr->array[i] = vector_ptr->array[i] + scalar;
    }

    return vector_ptr;
}   


// main:
//  Program entry point
// In:
//  
// Out:
//  return -- EXIT_SUCCESS if program terminates normally, 
//            EXIT_FAILURE otherwise
int main() 
{
    int num_errors = 0;
    Vector *my_vector_ptr = NULL;

    my_vector_ptr = alloc_vec();
    my_vector_ptr = extend_vec(my_vector_ptr, 1.0);
    print_vec(my_vector_ptr);
    dealloc_vec(my_vector_ptr);

    return EXIT_SUCCESS;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

In alloc_vec(), new_vector_ptr is just an uninitialized and unallocated pointer that points to some random memory location. You have to allocate memory for it before it can be used for anything.

Vector *alloc_vec(void)
{
Vector *vector_ptr = new Vector; // <<<<<< Here
vector_ptr->size = 0;
vector_ptr->array = NULL;
vector_ptr->array = new Elem[vector_ptr->size]; 

return vector_ptr;
}

in delte_vector() use delete, not delete[] because vector_ptr is not an array.

void dealloc_vec(Vector * vector_ptr)
{
delete vector_ptr; // When done, free memory pointed to by vector_ptr
vector_ptr = NULL;
}

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.