What could be wrong: char * param = new char [200];

I am writing a C ++ program and for some reason I get a segmentation fault on the following line:

char* param= new char[200];

      

I tried different options and even tried to put in front of it

int* param= new int;//for no reason

      

and the same error occurs. What could I have done to cause this problem?

Which can lead to simple memory allocation like this to give problems.

I would include the rest of the program, but that's over 1000 lines. But do not hesitate to ask for more information.

0


a source to share


5 answers


I would say Neil is on the right track - this is probably what you stomped on before, just caught there.

You have verified that:



[Edit] In response to your comment about 4GB of RAM, let's assume you are running the code correctly:

unsigned int amountToAllocate = 0;
amountToAllocate -= 1;

int* someStorage = new int[amountToAllocate];

someStorage[0] = 5;

      

... because it amountToAllocate

is unsigned int

, it rounds the maximum value towards it (4294967295 if unsigned int

- 32 bits on your hardware). So you will be trying to allocate 4294967295 * 4 bytes (again, if int

for you 32 bits) ... which is ~ 4 * 4 GB and will fail and will die from a nasty death.

+2


a source


Are you doing memory allocation and deallocation up to this point? If so, you've probably messed up a bunch. It is impossible to diagnose further without seeing more code.



+8


a source


This is how you can use it, I replaced char with int to save some values ​​for example, but works with char.

#include <iostream>

using namespace std;

int main()
{
    int * param = new int[200];

    for (int i = 0; i < 200; ++i) param[i] = i;
    for (int i = 0; i < 200; ++i) cout << param[i] << endl;

    delete[] param;

    return 0;
}

      

+2


a source


Make sure this line is the problem first. What happens if you comment on this? (or replace it with char *param = NULL

)

Secondly, the only way that can cause a segfault is that something was wrong before. What happens before that?

If you cannot show us the relevant code snippets, then this is your problem. Start commenting out bits of code or otherwise isolate potential problems. Sooner or later, you will be able to roll up the problem to a small sample that reproduces the error. At this point, the problem (and solution) might be obvious, and if it isn't, at least you have a reasonably sized sample code to show us.

+1


a source


I'm the second one that @jalf says. Are you REALLY sure this is the line that is faulty? This is highly unlikely.

The seg error occurs when you are trying to access memory that you do not have permission to. Since you are not asking for any particular memory, but rather letting the "new" memory allocation get it for you, the problem must be elsewhere.

What you should really do is put a bunch of print statements in your code. (I'm assuming you're not comfortable using the debugger?) This will allow you to see how far the program is going before it crashes. Good luck!

0


a source







All Articles