1

When I try to print an array contained in an instance of a struct part of the result is what I'm expecting and other parts seem to be gibberish. What is going on here?

Example output:

$./makevector test

NAME: test 16481592918288327671592918096327670000000000100011809530144490000159291832032767

My code is as follows:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

static int vec_length = 30;

typedef struct {
  char* name;
  int* vector;
} word_entry;

static word_entry entry_one = {NULL,NULL}; 

void MakeEntry(char* word, word_entry* entry){
 entry->name = word;
 int i;
 int this_vector[vec_length];
 srand(time(NULL));
 for(i=0;i<vec_length;i++){
   this_vector[i] = rand()%2;
 } 
entry->vector = this_vector;
}

int main(int argc, char* argv[]){
  int i;
  MakeEntry(argv[1], &entry_one);
  printf("NAME: %s\n", entry_one.name); 
  for (i=0;i<vec_length;i++){
    printf("%d",entry_one.vector[i]);
  }
  printf("\n");
}

6 Answers 6

3

this_vector is a local array to MakeEntry(). When that function ends, that array goes out of scope. So entry->vector is pointing to something invalid and you get undefined behavior.

Sign up to request clarification or add additional context in comments.

Comments

2

Mulitple issues here - "this_vector" is local to MakeEntry, and goes out of scope when MakeEntry returns.

Also - I think you want some \n in your printfs

Finally - what output were you expecting?

2 Comments

Thank you, that was it. I guess I have to declare a global array specifically for my struct instance and then modify it? I was looking for a random binary string as the output to associate with the word.
Sure a global would work, or you can use the calloc() functions that others describe. And that should give you a string of 1's and 0's, but I'm not sure how that is useful? Good luck!
2

Try something along these lines:

void MakeEntry(char* word, word_entry* entry)
{

      int i = 0;
      int *this_vector = calloc(vec_length+1, sizeof(int)); //Allocate an array of             Nelements x sizeof(int)

      entry->name = word;

      srand(time(NULL));

     for(i = 0; i < vec_length; i++)
       this_vector[i] = rand()%2;

      entry->vector = this_vector;
}

Comments

1

You create this_vector on the stack. But then assign it to entry->vector, for use outside the function MakeEntry.

Once MakeEntry returns, this_vector is no longer valid, and is likely the source of your garbage.

void MakeEntry(char* word, word_entry* entry){
    int this_vector[vec_length];
    [...]
    entry->vector = this_vector;
}

Comments

0

In my opinion the problem is that you are printing numbers without any spacing, try with

printf("%d ",entry_one.vector[i]);

so that you can identify all the single ints in your array.

Oh well, read chrisaycock answer, I didn't notice you were assigning the whole vector from a stack allocated variabile. It goes out of scope after the local scope, you should dynamically allocate it:

entry->vector = calloc(vec_length,sizeof(int));
for(i=0;i<vec_length;i++){
  entry->vector[i] = rand()%2;
} 

1 Comment

Here's what that did: NAME: test 16 48 60530528 32767 60530336 32767 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 809530144 49 1 0 0 0 60530560 32767
0
entry->vector = this_vector;

this_vector is a local array type variable and is lost as soon as the function returns. But still the struct member hold reference to it.

Instead of allocating the array on stack, use malloc or any other dynamic memory allocater. Remember to free it when no longer needed.

Comments

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.