User forums > Help
Problem when debugging piece of code while execution works
skirby:
Hello,
As I don't know if it is the good place to post that but here is my problem.
When I debug this piece of code, I have the following error message:
If I click on Yes, here is what I have on the screen:
Otherwise, If I execute it directly, it works perfectly.
--- Code: ---#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void) {
int i;
unsigned char** s;
s = (unsigned char**)malloc(MAX);
if (s == NULL) {
printf("Error allocating s\n");
return -1;
}
for (i = 0; i < MAX; i++) {
s[i] = (unsigned char*)malloc(MAX);
if (s[i] == NULL)
printf("Error allocating s[%d]\n", i);
else
sprintf((char*)s[i], "test %d", i);
}
for (i = 0; i < MAX; i++) {
if (s[i] != NULL)
printf("s[%d] : %s\n", i, s[i]);
free(s[i]);
}
free(s);
return 0;
}
--- End code ---
So, is my code wrong or is it a bug of the debugger?
Thanks and have a nice day.
sethjackson:
Well I really don't know, but umm this doesn't look good to me:
--- Code: (cpp) ---s[i] = (unsigned char*)malloc(MAX);
--- End code ---
when above you:
--- Code: (cpp) ---s = (unsigned char**)malloc(MAX);
--- End code ---
So generally if you get SIGSEV your code has a problem......
Game_Ender:
sethjackson, that is the proper way to dynamically allocate a 2 dimensional array in C. From a single read through, your code looks correct skirby. I would bring up the stack trace window and see where the seg fault was triggered from.
EDIT: if thats straight C code you don't need the casts.
killerbot:
the code is WRONG :
there's a problem in the allocation of the array for the pointers, it has size of MAX bytes, which is 10.
It should be MAX * sizeof(unsigned char*).
Cheers
MortenMacFly:
--- Quote from: skirby on August 09, 2006, 05:42:37 pm ---When I debug this piece of code, I have the following error message:
--- End quote ---
Do you mind telling us the C::B/GDB versions you are using? At what line the debugger crashes? I can debug through this code just fine using revision 2829. I don't see any problem. Please try to be more descriptive, e.g. a step-by-step instrcution how to reproduce this crash.
With regards, Morten.
Navigation
[0] Message Index
[#] Next page
Go to full version