Why the program is running without line 14?

#include<stdio.h>
#include<iostream>
//#include<conio.h>
//#include<math.h>

#define FOR(i,a,b) for(int i=a;i<b;i++)

using namespace std;

int main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int *b;
//b=(int *) malloc(sizeof(a));
b=a;
//cout<<b;
FOR(i,0,10) cout<<b[i]<<" ";
//cout<<sizeof(b);

//getch();
return 0;
}

Dani AI

Generated

Short answer: the first version "works" because you made the pointer refer to the existing array storage instead of copying it. As and hinted, assigning a pointer to the array's address makes the pointer an alias for that array — no new memory is created and no copy happens. If you actually want a duplicate of the elements you must provide storage for the destination.

Practical options (no manual per-element loop required)

  • In modern C++ use a container that defines copy semantics. std::array or std::vector will copy all elements with a simple assignment:

    #include <array>
    std::array<int,10> a = { /*...*/ };
    std::array<int,10> b = a; // element-wise copy
  • In plain C/C++ you can wrap the plain array in a struct and assign the struct; that copies the whole array object:

    struct S { int data[10]; };
    S a = {{ /*...*/ }};
    S b = a; // copies the array
  • If you allocate a buffer (malloc/new) you may copy bytes into it (memcpy or std::copy), but the destination must be valid storage first — otherwise you invoke undefined behavior.

Useful small notes and pitfalls

  • sizeof behavior: sizeof(array) gives total bytes of the array, while sizeof(pointer) gives pointer size — use sizeof(a)/sizeof(a[0]) to get element count only when a is an actual array, not a pointer.
  • Do not free or delete memory you did not allocate. Do not return pointers to local (stack) arrays — they become dangling.
  • Prefer RAII containers (std::array, std::vector) in C++ for safety and clarity; they avoid manual allocation and lifetime bugs.

References: std::array and copying semantics (cppreference std::array), and byte-level copy (cppreference memcpy).

Recommended Answers

All 9 Replies

int a[]={1,2,3,4,5,6,7,8,9,10};

You allocate memmory on stack here and a points to that memmory.
Then pointer b points to that memmory location b=a
i mean now b and a point to same location so you can work with it same as with a

here you can try to print it's addresses to check :)

#include <stdio.h>

int main()
{
   char pcArray[3] = {0};
   char *pcToArray = pcArray;
   printf("pcArray == %x and pcToArray == %x",pcArray,pcToArray);
   return 0;
}

#define FOR(i,a,b) for(int i=a;i<b;i++)

Why? What's wrong with just using the for statement? Making it a define simply makes the program confusing.

can you send me the exact question

...how can we use a pointer without allocating memory to it?

..sorry for the inconvenience..actually i have a file in which i have defined all the header file and macros. i just copied all these and make a new program.i actually delete most of them here.

@priya..the question is simple. i have to copy the contents of array a to b without using any loop or inbuit function

#include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<conio.h>
    #include<math.h>

    #define FOR(i,a,b) for(int i=a;i<b;i++)

    using namespace std;

    int main()
    {
    int a[]={1,2,3,4,5,6,7,8,9,10};
    int *b;
    //b=(int *) malloc(sizeof(a));
    //b=a;
    memcpy(b,a,sizeof(a));
    //cout<<b;
    FOR(i,0,10) cout<<b[i]<<" ";
    //cout<<sizeof(b);


    getch();
    return 0;
    }

now i am using memcpy function to copy the bytes. but here i require to allocate memory to b. why is so?

but here i require to allocate memory to b. why is so?

Where did you think the memory would come from? Pointers don't magically point to infinite memory on declaration, you have to point them to a block of memory that's allocated for that purpose, either through dynamic allocation (malloc and friends) or by assigning the address of an existing object.

..then why dont the first code is working without allocating the memory?

my basic ques is do we need to allocate memory to a pointer before using it? i mean in my first code why dont we malloc 'b' before the (b=a) statement?

@deceptikon..then why dont the first code is working without allocating the memory?

The pointer is being assigned the address of an existing array.

my basic ques is do we need to allocate memory to a pointer before using it?

You need to ensure that the pointer points to memory that you own.

i mean in my first code why dont we malloc 'b' before the (b=a) statement?

Because that's unnecessary and would cause a memory leak. The b = a statement is sufficient to meet the requirement of pointing to memory that you own.

..ok got it..thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.