Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I coded like that, but it gets seg-fault.
What is wrong?
int vector_size = 100000000;
float *rows[vector_size];
for (int i=0; i<vector_size; i++) {
rows[i] = (float *)malloc(sizeof(float)*2); // crashed here
// ...
}
You are trying to allocate a vast amount of memory. If you really need this many floats, using a separate malloc for each one is probably a bad plan. Malloc has it's own overheads as there is a minimum block size that can be allocated, and the memory manager also needs to keep track of all those separately allocated areas of memory, not to mention the size of this list of pointers you are holding.
A better solution might be to allocate in a single block enough space for all floats, and the index as an array, rather then try and keep a list of pointers...
So...
float *rows = malloc(sizeof(float) * vector_size);
might be a better starting point.
Try to allocate memory not in the stack, but in a heap.
float **rows = new float*[vector_size];
And what message you get about crash?
It don't look like memory overflow, because if memory is ended malloc() returns NULL:\
P.S. sorry for my english)
upd. oh, i don't saw comments above
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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.
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.
Improve this question
So I ran into an issue where it seemed like realloc wasn't copying all of the data in a buffer so I decided to run the following code as a test.
#include <stdlib.h>
int main(int argc, char** argv)
{
int* tmp_array = malloc(sizeof(int) * 2);
tmp_array[0] = 1;
tmp_array[1] = 2;
tmp_array = realloc(tmp_array, 4);
return 0;
}
The same issue I originally had was still happening, only part of the data is being copied. The 1 in the buffer gets copied, however the 2 does not. I am allocating enough space for 2 integers at first so both if the assignments to tmp_array should be valid. Then reallocating to 4 seems valid. I even tried explicitly casting the returned realloc pointer to int* but that didn't help.
Unfortunately, I can't show the screenshots of my memory window in the debugger (VS 2017) but it definitely shows the 1 and 2 in the buffer before realloc and only shows the 1 in the buffer after the realloc.
I'm sure I could reimplement realloc myself by just using malloc and memcpy to copy the data over manually, but I still just want to know why this isn't working.
Any help is greatly appreciated!
You are reallocating to 4 bytes, not to 4*sizeof(int), which incidentally copies just first int (and discards other).
realloc, as malloc, works on bytes so you must use them both in the same way.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I'm not quite sure what is wrong with my current program and I have reach a bit of a road block:
(*ptr).Name = (char*)malloc(strlen(record+1));
strcpy((*ptr).Name, record);
free((*ptr).Name); //problem area
*ptr is a pointer that points to a structure that has various fields. After I copy some data into the Name field I want to free my allocated memory. When I step through my program I get no errors rather just a hanging program that will not continue after I try and free the memory. Any ideas? Thank you.
(*ptr).Name = (char*)malloc(strlen(record+1)); //This is the problem!
strcpy((*ptr).Name, record);
free((*ptr).Name); //problem area //Better practice to use free(ptr->Name
Fix:
ptr->Name = (char*)malloc(strlen(record)+1); //(record+1) in previouse code was doing
//the opposite of what it was intended to do
strcpy(ptr->Name, record);
free(ptr->Name);
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 6 years ago.
Improve this question
So the question is, how does the original developer prevent a situation where user/new developer is trying to access a free'd memory element.
int *num=(int *)malloc(n*sizeof(int));
int i;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<n-1;i++){
temp = some_function(x);
}
free(num);
for(i=0;i<n;i++)
{
printf("\nnum[%d]= %d\n",i,num[i]);
}
P.S.: The above code works and actually prints out data in array. Which is not our intention.
[EDIT] Sorry if I was not clear enough. Someone suggested to ask this as a separate question, I thought why not. Here's the original post
The fact that your code "works" is a manifestation of the undefined behaviour that you're experiencing when reading memory that you no longer own.
You could consider setting num to NULL after the first free call. Then writing num[i] will almost certainly crash the program. It can also occasionally be useful: a free called with NULL passed as the pointer is a no-op.
There's little else you can do unfortunately.
But, in general, setting freed pointers to NULL leads to sloppy programming so I tend to avoid it.
The first thing you can do is set num=NULL.
More generally you can add a malloc hook to write garbage to the memory before freeing the memory (probably debug only). This will force a crash if the memory contains pointers or print garbage in this case when accessing freed memory.
Also since you are talking C++, make use of smart pointers.
Edit: fixed up the crash issue.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I don't know if the question makes any sense but I'm new to C and concepts like the Heap so its a bit overwhelming for me right now. I've been reading a few articles about memory allocation using malloc() vs declaring variables That I need to know the amount of data I need in advance when I want to work with variables while with malloc() I don't.
I can allocate data at runtime as much as I need with malloc but how? Lets say I want to input temperature records of 100 consecutive days so I did something
like
int* temps=malloc(100*sizeof(int)).
Now while I was inputting data I realise that I needed to enter 110 days of records. How do I go about adding the additional data at runtime? Since I just allocated space for 100 ints. What difference would it have made if had I done int temps[100], I know I had to initialize the array again changing the int temps[100] to int temps[110] and then recompile the program again and starting the input all over again.
Now while I was inputting data I realise that I needed to enter 110
days of records. How do I go about adding the additional data at
runtime?
Use realloc.
What difference would it have made if had I done int temps[100]
You can't change this size anymore, it is fixed. You have allocated array of 100 integers, and you are done. While with above approach, you still can "resize" the array during run time.
Though note sometimes doing something like int x[100] can be fine depending on your situation, plus it saves you from memory management related issues. That said, use dynamic memory such as malloc, only when it is necessary (exactly because to avoid complications related to memory management).
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 pasted my code here: http://pastebin.com/tPvRHrkW
Compiled with gcc.
It seems the error occurred because I defined a struct too big for the compiler. I took the struct out into another single source file and tested it, no error occurred this time. So why am I getting SIGSEGV and is there any limit on the size of a struct?
There isn't a limit to the size of the struct, the problem is with how you're using it. MGraph is the huge structure type, and in two places you're using it in a manner that places it on the stack; once as a parameter to a function and again as a local variable. Stack space is often not something that is permitted to grow to huge proportions.
I would suggest two changes. First, use dynamic allocation for instances of this type. Second, pass pointers as parameters to it, rather than the actual data.
Generally you're only limited by available memory and the addressing capabilities of your system. However in your case you're declaring a local variable, which will be allocated on the stack. The stack is likely much more limited in capacity.
#define MAXV 20000 .. int edges[MAXV][MAXV];
is 20000 * 20000 * 4 ~ 1.5 Gigs of memory on stack.
You should probably use malloc & dynamically allocate instead.