Connecting Tech Pros Worldwide Forums | Help | Site Map

Making an array using Pointer without new and delete

Newbie
 
Join Date: Nov 2007
Posts: 3
#1: Nov 4 '07
Hi, i am trying to initialize a dynamic array and use this array to conduct linear search. I know about new and delete but as a pointer can be incremented by the sizeof its dataype, i wanted to create an array by incrementing a pointer!

This code's working fine just that when i enter the value to be stored in the array for linear search it says " POINTER (2) 0x2487 PROCESSOR FAULT"

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int *a, *b, count, i,x,found,val,n;
  5.     char newN,ch;
  6.     b=a;
  7.  
  8.     printf("Enter the size of array : ");
  9.     scanf("%d", &n);
  10.  
  11.         count=0;
  12.         while(n!=0)
  13.         {    a++;
  14.             count++;
  15.             n--;
  16.         }
  17.         a=b;
  18.  
  19.     for(i=0;i<count;i++)
  20.     {
  21.         printf("Enter value for %d element ", i);
  22.         scanf("%d", & val);
  23.         a[i]=val;           /*Here's the problem lies, i've tried using & and *   operators but no help */
  24.     }
  25.  
  26.  
  27.     do{
  28.     found=0;
  29.     printf("\nEnter the element to be searched :");
  30.     scanf("%d", &x);
  31.  
  32.     for(i=0;i<count;i++)
  33.     {
  34.         if(a[i]==x)
  35.           {found=1;
  36.             break;
  37.           }
  38.     }
  39.     if(found==1)
  40.     printf("\nThe element has been found at position %d!",i+1);
  41.     else
  42.     printf("\n %d was not found!",x);
  43.  
  44.     fflush(stdin);
  45.     printf("\nSearch another element ? (Y/N) : ");
  46.     scanf("%c", &ch);
  47.     found=0;
  48.     }while(ch=='y'||ch=='Y');
  49. }
  50.  
Could anyone please sort it out for me?
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Nov 4 '07

re: Making an array using Pointer without new and delete


You never set a and b to point to anything. You declared them, and then set b equal to a (so they both point nowhere) and try to use them.

You're going to have to malloc some memory space, and let a and b [point to that. But, you run into a logic problem here. When you call malloc to allocate room for 1 int, the function looks for a space in memory big enough for 1 int, allocates it, and returns the address via a void*. But you are assuming that memory location has enough empty room in front of it for an unknown amount of ints. You might get lucky once in a while, and this wouldn't prove to be problematic. But more likely than not, you are going to push your pointer into other, already allocated memory, and mess up some possibly vital information.

I don't see any way you can accomplish what you want to do without malloc-ing a space big enough for n integers.

As a side note, your main should be declared

Expand|Select|Wrap|Line Numbers
  1. int main()
and return a 0 at the end of your program. void main() is non-standard and theoretically could do anything.
Familiar Sight
 
Join Date: Mar 2007
Posts: 148
#3: Nov 4 '07

re: Making an array using Pointer without new and delete


Quote:

Originally Posted by vaibhavkanwal

Hi, i am trying to initialize a dynamic array and use this array to conduct linear search. I know about new and delete but as a pointer can be incremented by the sizeof its dataype, i wanted to create an array by incrementing a pointer!

How and why?
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int *a, *b, count, i,x,found,val,n;
  5.     char newN,ch;
  6.     b=a;
Assigning b to a is meaningless when a hasn't yet been initialized to any value.
Expand|Select|Wrap|Line Numbers
  1.     printf("Enter the size of array : ");
  2.     scanf("%d", &n);
  3.  
  4.         count=0;
  5.         while(n!=0)
  6.         {    a++;
  7.             count++;
  8.             n--;
  9.         }
  10.         a=b;
Here, you are trying to determine the size of a block of memory to store your "array", right? This doesn't work. You need to ask for memory to be allocated. You do that like this:
Expand|Select|Wrap|Line Numbers
  1. int a[20];
Or like this:
Expand|Select|Wrap|Line Numbers
  1. int* a = new int[n];
Expert
 
Join Date: Aug 2007
Posts: 674
#4: Nov 4 '07

re: Making an array using Pointer without new and delete


i don't think our OP understands. Asking for dynamic memory is a runtime operation. Effectively, the operating system is asked to give up some memory if necessary. Actually, malloc/new have a bit of memory management. So either you use malloc or new, or you write your own whatever to allocate and manage memory.

It is possible to do this, and is done especially in cases where programmers need some really efficient memory allocation that new and malloc can't just do. But I don't think our OP gets this at all.
Reply


Similar C / C++ bytes