Will we get SEGV if we try to “delete []” un-initialized integer pointer variable.


 
Thread Tools Search this Thread
Top Forums Programming Will we get SEGV if we try to “delete []” un-initialized integer pointer variable.
# 1  
Old 09-29-2004
Will we get SEGV if we try to “delete []” un-initialized integer pointer variable.

I have a class with an integer pointer, which I have not initialized to NULL in the constructor. For example:

class myclass
{
private:
char * name;
int *site;
}

myclass:: myclass(....)
: name(NULL)
{
.....
}

other member function “delete [] “ the variable before initializing it.

Ex:
void myclass:: copy_site ( int **Values, int *from)
{
int *old;

old = *values.

Delete [] old; <<<<<ß here I am getting SEGV
...............
..............
}

But I am getting SEGV at “delete []”.

Surprisingly it is not happening every time. For only some inputs it is certainly working fine.

What could be the reason?
If it is because of initialization, why is working for some inputs and certainly not for other inputs?

I have observed with workshop that, for some inputs it is automatically getting initialized to NULL, and for other inputs it is certainly not NULL (dangling pointer).

Thanks in advance,
Suresh
# 2  
Old 09-29-2004
Though I am not sure of the exact reason but this could be a probable reason:

When we want to delete the contents of an array allocated with new[], we need to use the delete[] operator. Whether we have to use the delete or the delete[] operator - we need to tell this to compiler, cause for example an 'int*' could either point to a single integer, allocated using 'new int', or to an array of integers.

If we allocate an array of integers like int* var = new int[5];
then we should explicitly delete the array by calling delete [] var which would tell the compiler to free all the memory as held by the array.

Similarly there could be a problem if we allocate memory for a array element using new, and later try to de-allocate it using delete[]. We might get memory corruption - since compiler might try to delete whole memory for an entire array - while we have only allocated memory for a single element.
# 3  
Old 10-11-2004
Could following be the reason?

In main() I have two objects of different classes say class1 & class2.

Class1 and class2 both uses pointer variables. Class1 initializes all the pointer variables but class2 doesn't.

My observation is:

If I just instantiate class1 and class2 and then start using it no problem is occurring. (That is class2 pointer variables by default getting initialized to NULL).

But if I instantiate class1 and allocate some memory to its pointer member variables, immediately free it, and then instantiate class2, I am surely getting dangling pointer in class2 members.

Is it the right cause I have analyzed?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

2. Programming

warning: comparison between pointer and integer

Hi guys :D I am still playing with my C handbook and yes, as you can see I have small problem as always :cool: I wrote a C code #include <stdio.h> #define MESSAGE 100 int main(void) { char input_mes - Pastebin.com And when I try to compile it I get following errors from gcc ... (1 Reply)
Discussion started by: solaris_user
1 Replies

3. Solaris

How to Use a Variable as Integer?

hello, i am writing a script that takes the UID from the PASSWD and then i want to increse the Number by one. for the Next user. i cannot get this to work that a variable is as interger example: set i = 0 set $i = $+1 it's in tcsh if it's mather (10 Replies)
Discussion started by: shatztal
10 Replies

4. Shell Programming and Scripting

What's the max integer a variable can hold?

I would like to know the maximum integer that a variable can hold. Actually one of my variable holds value 2231599773 and hence the script fails to process it.Do we have any other data type or options available to handle this long integers? (9 Replies)
Discussion started by: michaelrozar17
9 Replies

5. Programming

warning: passing arg 1 of `inet_addr' makes pointer from integer without a cast

I use solaris10,following is tcp client code: #include "cliserv.h" int main(int argc,char argv){ struct sockaddr_in serv; char request,reply; int sockfd,n; if(argc!=2) err_quit("usage: tcpclient <IP address of server>"); if((sockfd=socket(PF_INET,SOCK_STREAM,0))<0) ... (1 Reply)
Discussion started by: konvalo
1 Replies

6. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

7. Programming

`strcat' makes pointer from integer without a cast

A question to ask. seq1 = "eeeeeeeeeeeeeeeeee"; seq2 = "dddddddddddddddddddd"; char a = '*'; strcat(*seq2, &a); strcat(*seq1, seq2); compilation warning: passing arg 1 of `strcat' makes pointer from integer without a cast thanks (4 Replies)
Discussion started by: cdbug
4 Replies

8. UNIX for Dummies Questions & Answers

Subtracting an Integer from a Variable

Hello, I am in following situation.- COUNT=`ls -l | wc -l` echo $COUNT ---> 26 NO_OF_FILES=$COUNT-1 echo $NO_OF_FILES ---> 26-1 Here, I want the output to be 25. How could I do this. It seems simple, but I am not getting it. Please help me. (2 Replies)
Discussion started by: The Observer
2 Replies

9. Programming

why we never delete a pointer twice

can u tell me the reson that why we should not delete a pointer twice.? if we delete ponter twice then what happen and why this happen Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

10. Programming

comparison between pointer and integer

I received a warning when I tried to compile my program that said: warning: comparison between pointer and integer Could you please explain to me what this means, and in what ways I could possibly fix this? Thanks for your help! (2 Replies)
Discussion started by: sjung10
2 Replies
Login or Register to Ask a Question