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 2 years ago.
Improve this question
I am tring to copy a const char* to a char* and below is what I have written:
int main()
{
const char* string = "Hello";
int size = strlen(string) + 1; // add '/0' in the end
char* data = new char(size);
memcpy(data, string, size);
delete data;
}
unfortunately I am getting a error with "Heap corruption detected: after normal block (#77)".
I have no idea what the problem is.
I am complying my code using MSVC under visual studio 2019.
new char(size) is not allocating an array of characters but allocating single character whose value is size.
You should use new char[size] to allocate an array and delete[] data; to delete the array.
Related
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 working on a C++ implementation of a Binary Heap, but I'm having some issues getting started. Here's a snippet of my code:
class binaryHeap {
public:
// Constructor
binaryHeap(int _capacity)
{
// initializes the binary heap with a capacity, size, and space in memory
_size = 0;
_n = ceil(pow(2, log10(_capacity)/log10(2)));
_heap = new int[_n];
}
~binaryHeap(void)
{
delete[] _heap;
}
/* Omitted: insert, remove, size, capacity functions
Not necessary to the issue I'm having */
private:
int _size;
int _capacity;
int _n;
int *_heap;
};
In the main.cpp file, when I write the following line:
struct BinaryHeap heap(10);
I get the error: Variable has incomplete type 'struct BinaryHeap'. Any ideas what is causing this?
I think this is a typo problem. Your binary heap class is binaryHeap, while in your main function, you are saying struct BinaryHeap heap(10);, which in the compiler's POV is a completely different type.
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 run this code and got this exception in visual studio:
int main ()
{
int * x=new int(23);
for(int i=0;i<9;i++)
{
x[i]=i;
}
delete []x;
return(0);
}
thanks for your help ;)
int * x = new int(23); does not do what you think. It allocates the memory for a int and gives it the value of 23.
What you want to do is this:
int * x = new int[23];
Although, I would recommend you use std::vector if the size change or std::array if the size is fix.
If you use new you need to use delete. If you use new[] then you need to use delete []. new() is not the same as new[]. new(someval) sets the new object to someval. It does not make someval number of objects.
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
#include<cstdio>
#include<cstring>
#include<cstdlib>
int main(){
int t;
scanf("%d",&t);
while(t--){
char *str;
scanf("%s",str);
int l,n,nop=1;
l=strlen(str);
printf("Length %d",l); //causing segmentation fault
if(l%2==0)n=l/2;
else n=(l-1)/2;
for(int i=0;i<n;i++)nop=nop+abs(str[i]-str[l-1-i]);
printf("%d\n",nop);
}
return 0;
}
I am getting error when I print the length of string. If I remove that line the code works but with incorrect output.
The code works fine with DevC++
You need to initialize str by allocating space for it:
char *str = (char *)malloc(SIZE);
Otherwise it will invoke undefined behavior. Also, declare l of size_t type because strlen returns type is size_t and change %d to %zu in printf
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
if char is only 1 byte (8bit, 255 max size), how 65534th char can be saved in char ?
#include <iostream>
int main() {
unsigned wchar_t a = 65534;
char b = (char)a;
std::cout << b << std::endl;
return 0;
}
also in java you can write byte b = 5000;
do anyone knows why ?
For the C++ code, the wchar_t value gets truncated to fit in the char variable. Normally there would be a warning from the compiler about this loss of data, but by using the C-style cast you've told the compiler "don't worry, I know what I'm doing" and taken responsibility for the consequences of your (questionable) actions.
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 am working on a simulator (Computer architecture).
A piece of code read
List freeList;
const int numEntries
registers = new MSHR[numEntries];
for (int i = 0; i < numEntries; ++i) {
registers[i].queue = this;
freeList.push_back(®isters[i]);
}
I changed it to
List freeList;
int numEntries;
registers = new MSHR[numEntries + 100];
for (int i = 0; i < numEntries + 100; ++i) {
registers[i].queue = this;
freeList.push_back(®isters[i]);
}
Just changing the const numEntries has had a drastic effect on the memory usage. If I run the program for long (Code does a lot of push_back, pop_front on the list), I run into this error
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
What can be the difference?
LMGTFY: The documentation says "Exception thrown on failure allocating memory".
You are trying to create more objects than you have memory for.
Since numEntries is not initialized, that is the problem.