-1

I'm a bit stuck. It appears that on the mere declaration of a char ** array there is a segmentation fault.

Here's the exact order of my code:

printf("%d\n", size_of_array);
char** array;
printf("hello");
array = (char**)malloc(sizeof(char*)*size_of_array);

This gives a result of:

18 //my calculated size_of_array
Segmentation Fault

As you can see, hello does not output, hence my confusion.

In addition, I've also tried the statement:

array = new char*[size_of_array]; 

I don't pretend to be the best at C/C++ but this one is definitely baffling me. In addition (if it is worth noting), I am compiling on a linux machine using g++. Thanks for your help.

Incara
  • 1
  • 4

2 Answers2

1

I think your error must originate somewhere else in your code, above what you posted.

I just ran this:

// I added the line below to define size_of_array since you left out that code: 
int size_of_array = 18;

And then your code:

printf("%d\n", size_of_array);
char** array;
printf("hello");
array = (char**)malloc(sizeof(char*)*size_of_array);

runs fine and outputs:

18
helloProgram ended with exit code: 0

Note that you left off the newline '\n' in the 2nd printf() but thats not your error.

Also you should not cast malloc (but not the source of your error) which you can read more about here, just use:

array = malloc(sizeof(char*)*size_of_array);

However, in C++ the cast is required (hope thats not too confusing) but you didn't say but do you want C or C++? You tagged both, and by the way, your C++ line:

array = new char*[size_of_array];

ran ok too.

Keep in mind that you should not mix memory management functions from C (malloc, calloc, free) with C++ memory management (new, delete). This is bound to cause problems later since you may mix them up at some point and free memory that was allocated with new, or use delete [] on memory that was allocated with malloc, etc.

Community
  • 1
  • 1
Bruce Dean
  • 2,798
  • 2
  • 18
  • 30
  • Thank you for confirming all of this. I just wanted to make sure I wasn't crazy. I was careful to use both the C code and the C++ code in different scenarios. My program has worked fine up to this point so I don't know what else it could be, but I'll keep looking. – Incara Dec 02 '13 at 04:15
0

The hello text is not being output because there's no newline. That is why there are other comments about buffers and flush. If you put an exit() statement after printf("hello") it would probably not show a segfault.

You're best bet to identify the actual segfault would be to single step in a debugger.

woolstar
  • 5,063
  • 20
  • 31
  • Right, gdb was my next stop. Thanks. – Incara Dec 02 '13 at 04:03
  • @Incara: gdb should have been your first stop! And learning to put a newline at the end of every printf() where you want to see the output is crucial. Especially if you're trying to detect where a program is crashing. It simply wastes time (yours and ours) if you don't print stuff sanely. – Jonathan Leffler Dec 02 '13 at 04:09
  • I get that, realized it after I posted it. I threw it together and the printf\n was a minor slip. – Incara Dec 02 '13 at 04:18