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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I declared an array in C of size 150X150X150. Upon compiling the program to get an array of same size,the compiler gave no errors or warnings. but when I tried running it, the program stops responding.
void main(){
int i,j,k;
char giv[150][150][50],tar[150][150][50];
for(int i=0;i<150;i++)
{
for(j=0;j<150;j++)
{
for(k=0;k<50;k++)
cin>>giv[i][j][k];
}
}
}
Is there any way that I can create an array of 150*150*150 without causing a run time error?
EDIT: I know multidimensional arrays work. This is not a compilation error. Its a run time error, whose cause was I am not able to pinpoint.
You just declared two arrays on the stack.
Each array has size: 150 * 150 * 50 bytes, or about 1.1MB.
So you are asking for 2.2MB from the stack.
Typical stack size is about 1 or 2MB.
So I expect you're getting a StackOverflow Exception.
(kinda appropriate for this site)
You could allocate the arrays on the heap:
#include <stdlib.h> /* for malloc()/calloc() */
#include <stdio.h> /* for perror() */
...
char (*pgiv)[150][150][50] = malloc(sizeof *giv);
char (*ptar)[150][150][50] = malloc(sizeof *tar);
If you want to have the arrays' elements initialised to all 0s on allocation use calloc() as follows:
char (*pgiv)[150][150][50] = calloc(1, sizeof *giv);
char (*ptar)[150][150][50] = calloc(1, sizeof *tar);
Also test wether the allocation succeed or not:
if (NULL == pgiv)
perror("malloc() failed");
if (NULL == ptar)
perror("malloc() failed");
Address an element by doing for example:
(*pgiv)[0][1][2] = 123;
Note that pgiv and ptar are actually pointers (to an array). That's why they need to be dereferenced (using the dereference operator *) before being used like an array.
It seems that the problem is with the limit of the stack memory.
In C++ you could use for example standard container std::vector.
In C you could allocate these arrays yourself dynamically.
The simplest way is either to declare these arrays globally that is outside any function or specify keyword static that the arrays had static storage duration. For example
static char giv[150][150][50],tar[150][150][50];
As for other languages then for example Java and C# allocate arrays in the managed heap. It keeps in the stack only a reference to the array.
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.
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.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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
Improve this question
I have a little problem with casting int to char* (string)... is it even possible in C?
I'll try to explain why i need this.
I can cast int to char but I need cast int to char*.
I had a int varriable (int number_of_revisions)
and I need convert this number of revisions to char * becouse I need create a name of file and the number of revision is part of the name.... so there is part of code for better imagination of this problem.
int number_of_revision = 970; // 970 just for example
char * version;
char * new_name;
char ch_number_of_rev[4];
version = "0.";
itoa(number_of_revision,ch_number_of_rev,10);
//strcat(version, ch_num_o_rev ); // doesn't work becouse ch_number_of_rev is char and strcat requires char*
please I need quick help... Have anybody any idea how to do it? ...
but I need cast int to char*
Casting only changes the type - it does not change the value within the variable. If you need to convert an int to array of chars (i.e. a string) then use sprintf or snprintf:
char* buffer = ... allocate a buffer ...
int value = 970;
sprintf(buffer, "%d", value);
Converting int to string in c
Also, you have not allocated any memory for version - use malloc and allocate some memory.
strcat here won't work because you haven't allocated any space to store the result in. Your version is probably in read-only memory anyway, so you'd get a segfault, otherwise you'll get memory corruption. So make sure to allocate enough space for it, e.g. by using
char version[10] = "0.";
You may want to read up on pointers first, though.
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 9 years ago.
Improve this question
I have a simple macro:
#define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
and for whatever reason, it seems to work fine when setting something like:
int length = LENGTH(arr)
but not to compare with in a loop
while(i < LENGTH(arr))
For the same arr, the macro will either work or it won't.
The likely problem is that the arr in the loop is a pointer rather than an array. The macro does not work with pointers, it only works with arrays.
Remember that arrays decays to pointer when passed around. So if you pass an array to a function, it's not longer an array but a pointer inside the function. A pointer which have only have information about the type, but not the size of the array.
sizeof(arr) is not the same as what a more high level language gives you when you do arr.Count() or arr.Length.
This gives you the storage space of the arr variable. This does not mean the length of the array.
If you have char *arr = malloc(sizeof(*arr) * 100); and you do sizeof(arr) you will usually (depending on your system) get 4. If you do sizeof(*arr) or sizeof(arr[0]), which is the same thing, you have sizeof(char) which is 1, so the macro gives you 4/1 = 4 : wrong. It works with char arr[100] because sizeof(arr) gives you 100 as the array does not decay to a pointer type for sizeof.