Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#define MAX_SPACES 10
#define MAX_SIMPLE_EVENTS 5000
#define MAX_USER_EVENTS 1000
struct {
EventSpace* p_spaces[MAX_SPACES];
SimpleEvent* p_simple_events[MAX_SIMPLE_EVENTS];
UserEvent* p_user_events[MAX_USER_EVENTS];
}* G_manager;
static void add_space(EventSpace* space){
static uint16_t index = 0;
(*G_manager).p_spaces[index] = space;
}
After running in gdb got:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400559 in add_space (space=0x7fffffffdf30)
How to impove it?
At least in the code you're showing, you haven't actually allocated the memory for the structure containing the arrays; you've only created a pointer. So when you dereference that pointer meaning to write into an instance of the structure, you hit a random memory address and got the relatively tame result of your program crashing.
You could change G_manager from a pointer-to-struct to an instance of the struct itself; or you can malloc a buffer big enough for the structure and assign that buffer to G_manager
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'v encountered "Segmentation fault (core dumped)" when compile my C program in *nix. I've narrowed the mistake to this line (without this line my program can run):
strcpy(con[count], "1234");
Before that, I declared con as:
char *con[30];
And count is always smaller than 30.
What's wrong with this line? How should I change it?
char *con[30];
declares an array of 30 pointers to strings. This is not what you need. It fails because you then try to copy to the first string, but did not allocate the first string (only a pointer to it)
You need
char con[30];
and then
strcpy(con, "1234");
Or (as Lee Danial points out) you might have wanted an array , in which case you need
char *con[30];
then
con[count] = strdup("1234")
or
con[count] = "1234"
The first one allocates a string and copies it for you (a combination of malloc and strcpy). The second one just points at the supplied literal, it doesn't make a copy. Hard to say which is 'best' for you.
PS strdup is equivalent to
x = malloc(strlen(str) + 1);
strcpy(x, str);
return x;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm trying to format the given string and printf it. Bu it doesnt work. It gives error Any idea?
char* query_buffer;
sprintf(query_buffer,"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x;%u.%u.%u.%u;%d\n",
node_config->mac_address.addr[0], node_config->mac_address.addr[1], node_config->mac_address.addr[2], node_config->mac_address.addr[3],
node_config->mac_address.addr[4], node_config->mac_address.addr[5], node_config->mac_address.addr[6], node_config->mac_address.addr[7],
ip64_addr->u8[0], ip64_addr->u8[1], ip64_addr->u8[2], ip64_addr->u8[3],
node_config->coap_port);
printf("%s\n",query_buffer);
If I try below printf it works. I couldnt understand what is different between doing these two.
printf("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x;%u.%u.%u.%u;%d\n",
node_config->mac_address.addr[0], node_config->mac_address.addr[1], node_config->mac_address.addr[2], node_config->mac_address.addr[3],
node_config->mac_address.addr[4], node_config->mac_address.addr[5], node_config->mac_address.addr[6], node_config->mac_address.addr[7],
ip64_addr->u8[0], ip64_addr->u8[1], ip64_addr->u8[2], ip64_addr->u8[3],
node_config->coap_port);
The line char* query_buffer; declares a pointer to a char but it the memory it points to might not be declared. So you can get a segmentation fault when you call sprintf to access that memory. Try declaring query_buffer like char *query_buffer = (char*)malloc(256);. That will create a pointer and declare 256 bytes at where it points to.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am making a game in c, and I get an error with a struct array which I have created.
typedef struct{
int type, level, x, y, w, h;
} Tile;
Tile *map[256];
Tile *t;
t->type = 0;
t->level = 0;
t->x = 0;
t->y = 0;
t->w = 0;
t->h = 0;
map[0] = t;
Once compiled, the program prints:
Segmentation fault (core dumped)
Defining a pointer does not automatically make that pointer to point to a valid memory location. A pointer, which is not allocated memory, is called as uninitialized pointer and cannot (shall we say, should not?) be de-referenced.
In your code,
t->type = 0;
and so on, you're de-referencing t which is not allocated memory. Hence, by de-referencing an unitialized pointer, you invoke undefined behavior. The segmentation fault is one of the many side-effects of the UB.
Solution: You need to allocate memory to t before you can actually dereference it. Maybe you can have a look at malloc() and family of functions to get the memory allocation done.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I use C language for a natural language processing project.
I would like to store a dictionary file.
I used the following define statement
#define DICSIZE 46000
The question is about the number 46000 because it is the maximum number I can enter.
If I try a bigger number the program stop running.
How can I solve this problem?
Program stops not because of DICSIZE macro. It's usage.
I guess, some array is allocated locally(i.e In stack) by passing this macro as array size.
int myArray[DICSIZE];
So when the number is increased, you may face problem. I suggest to allocate memory dynamically using malloc().
I suspect you have a large array declared locally in a function like this:
int main()
{
MyRecordType myArray[DICSIZE];
...
return 0;
}
When DICSIZE gets large, myArray gets large, and you run out of stack space.
Use dynamic memory allocation instead:
int main()
{
MyRecordType * myArray = malloc(DICSIZE * sizeof(myArray[0]));
assert(myArray);
...
free(myArray);
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I have a problem in displaying the size of the array correctly.
I know array size is 256000 but it is displaying as 8 when I enter the loop. size will be displayed accurately if dynamic allocation is not used. How do I rectify the bug using dynamic allocation?
This will give you size 10, because the compiler knows it's an array;
char foo[10];
int size = sizeof foo;
This will give you size 4 on a 32-bit architecture, because it's the size of a pointer.
char *foo = malloc(10 * sizeof(char));
int size = sizeof foo;
After this, the usage of foo is identical. You can do foo[2] or *foo or whatever with both versions. But you probably shouldn't take the address of &foo with the 1st variant. And you should free(foo); sometimes with the 2nd.
Always remember: sizeof is not a function, sizeof is always decided in compile time.