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;
}
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 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
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 8 years ago.
Improve this question
I want to create a char foo[] and set the size wit an int but it dosn't work at all!
}else{
int start = match[1].rm_so;
int end = match[1].rm_eo;
char value[end-start];
...
}
The size of value is always 0, why is that so?
Thanks
You should use malloc for that.
char *value = malloc( (end-start) * sizeof(char))
When using malloc, remember to free the memory when your are done using the array.
free(value)
See more here if you need more information please : C Dynamic memory allocation
It doesn't work because you are compiling your C code against the old C90 standard. In that standard, array sizes had to be a compile-time constant. In modern C, there is something called Variable Length Arrays (VLA), which allows you to declare arrays by using variables, as in your example.
Note that VLAs have to be declared at local scope, you cannot declare them at file scope ("global").
The best solution is therefore to get a modern compiler such as GCC, then compile it as standard C code: gcc -std=c99 -pedantic-errors.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
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.
Improve this question
First off the computer I'm running this on is an Intel System running Linux on a 32-bit stack. My professor posed a challenge question to us in class.
Here is the code before I ask the question
// funWithFooAndBar.c
#include <stdio.h>
#include <stdlib.h>
void bar()
{
printf("Now inside bar()!\n");
}
void foo()
{
void *addr[1];
printf("Now inside foo()!\n");
// this is where I need to modify my code,
//I was given the hint that it will only be two lines of code
// So something like:
addr[1] = bar;
addr[5] = addr[4];
addr[4] = bar;;
}
int main (int argc, char *argv[])
{
foo();
printf("Back in main\n");
return 0;
}
The goal is to smash the stack by writing beyond the end of an array, and through that, overwrite the return address so that the function call to foo () returns to bar () on its way back to main. So my output is supposed to look like:
Now inside foo() !
Now inside bar() !
Back in main
In order to do this I have to overflow the array so that the return address is overwritten with the address of the bar.I'm pretty sure that it will have to involve the address of function bar() which will equate to &bar()
The question he posed was what two lines of code could we add (where I commented) to make the output as a shown above.
Thanks!
Edit: I was hoping more for an explanation than a direct answer, I know what I'm supposed to do, just not how to translate that to the c code.
Edit: made an attempt
After reading the article that R M linked:
addr[1] = bar;
addr[5] = addr[4];
addr[4] = bar;
Turns out to work.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Sorry, I'm a bit of a newbie to C and was wondering how you could create an array whose size is not known at compile time before the C99 standard was introduced.
It's very easy. For example, if you want to create a variable length 1D int array, do the following. First, declare a pointer to type int:
int *pInt;
Next, allocate memory for it. You should know how many elements you will need (NUM_INTS):
pInt = malloc(NUM_INTS * sizeof(*pInt));
Don't forget to free your dynamically allocated array to prevent memory leaks:
free(pInt);
Use malloc function from stdlib.h to create a dynamic array object.
the ordinary way would be to allocate the data on the heap
#include <stdlib.h>
void myfun(unsigned int n) {
mytype_t*array = (mytype_t*)malloc(sizeof(mytype_t) * n);
// ... do something with the array
free(array);
}
you could also allocate on the stack (so you don't need to free manually):
#include <alloca.h>
void myfun(unsigned int n) {
mytype_t*array = (mytype_t*)alloca(sizeof(mytype_t) * n);
// ... do something with the array
}
You can do this by dynamic memory allocation. Use malloc function.
malloc or calloc
YourType* ptr = malloc(sizeof(YourType)*NumberOfItemsYouNeed)
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.